//输出token值 public void printTokens() { Console.WriteLine("Tokens:"); foreach (MyLexicalToken token in tokens) { Console.WriteLine("[" + CLUtility.getTokenValue(token) + "]:" + token.Name); } }
//进行LL1文法分析 public void LL1Analysis(string root) { SResult rForm = new SResult(); rForm.Show(); printTokens(); //ArrayList proSq = new ArrayList(); Stack <string> pdChars = new Stack <string>(); pdChars.Push("#"); pdChars.Push(root); int index = 0; int No = 1; //未到结束 rForm.Update0(); while (!"#".Equals(pdChars.Peek())) { //取栈顶 string X = pdChars.Peek(); MyLexicalToken a = new MyLexicalToken(); //当前输入符号 if (index < tokens.Count) { a = (MyLexicalToken)tokens[index]; } //终结符或# if (isTChar(X) || "#".Equals(X)) { if (CLUtility.isEqualTT(a, X)) { //弹出并前移指针 if (!"#".Equals(X)) { pdChars.Pop(); index++; } } else { string errorStr = pdChars.Pop(); errorStr = "ERROR: Ignore the input [" + errorStr + "]"; rForm.addListItem(No++, a.LineCount, "ERROR " + a.Name, errorStr, pdChars.Peek(), getStackString(pdChars)); } } else { //ArrayList item = predictionTable[X][CLUtility.getTokenValue(a)]; ArrayList item = new ArrayList(); //predictionTable[X].TryGetValue(CLUtility.getTokenValue(a),out item); if (predictionTable[X].TryGetValue(CLUtility.getTokenValue(a), out item)) { pdChars.Pop(); if (!"$".Equals(item[0])) { for (int i = item.Count - 1; i > -1; i--) { pdChars.Push((string)item[i]); } } string output = "[" + X + "] -> "; foreach (string s in item) { output += ("[" + s + "] "); } Console.WriteLine("" + No + " " + CLUtility.getTokenValue(a) + " " + a.Name + " " + output + pdChars.Count); rForm.addListItem(No++, a.LineCount, a.Name, output, pdChars.Peek(), getStackString(pdChars)); } else { if (ntChars[X].Sync.Contains(a)) { string errorStr = pdChars.Pop(); errorStr = "ERROR: Poped non-terminal char: [" + errorStr + "]"; rForm.addListItem(No++, a.LineCount, "ERROR " + a.Name, errorStr, pdChars.Peek(), getStackString(pdChars)); } else { string errorStr = pdChars.Pop(); errorStr = "ERROR: Ignore the input [" + errorStr + "]"; rForm.addListItem(No++, a.LineCount, "ERROR " + a.Name, errorStr, pdChars.Peek(), getStackString(pdChars)); } } } } rForm.Update1(); }