//初始化过程栈 public void Process_Stack_Initialize(ref Stack<Node> a) { //每次进入match函数,树根都要初始化 headfore = new Node("", "$", "", "", ""); treeroot = new Node("", "program", "", "", ""); headfore.Add(treeroot);//list初始化 //stack初始化 a.Push(headfore); a.Push(treeroot); }
//过程栈和输入栈进行怼的过程,treestack为过程栈,b为输入栈 public void match(Stack <string[]> b, GrammarSheet c, int nResult /*判定是否是文件结束*/) { int[] errorshowrow = new int[100]; //显示错误所在的行 c.outcount = -1; //每次进入match函数,序号初始化为-1 //p,q用于输出两个栈的内容 Stack <Node> p = new Stack <Node>(); Stack <string[]> q = new Stack <string[]>(); Stack <Node> r = new Stack <Node>();//用于反向输出的保存 while (!(treestack.Count == 0)) { c.outcount++;//process,inout,action的计数+1 //清空process,input,action使得每多读一个token都不是在原来的输出中写内容 c.process[outcount] = ""; c.input[outcount] = ""; c.action[outcount] = ""; //p,q用于输出两个栈的内容 p = new Stack <Node>(treestack); q = new Stack <string[]>(b); while (!(p.Count == 0)) { if (p.Peek().type != "empty") { r.Push(p.Peek()); } p.Pop(); } while (!(r.Count == 0)) { if (r.Peek().type != "") { c.process[c.outcount] = c.process[c.outcount] + r.Peek().type + " ";//用字符串m获取过程栈中字符串 } r.Pop(); } c.process[c.outcount] = c.process[c.outcount];//空一行 // printf("%-30s", m.c_str()); while (!(q.Count == 0)) { if (q.Peek()[2] != "empty") { c.input[c.outcount] = c.input[c.outcount] + q.Peek()[2] + " ";//用字符串n获取输入栈中字符串 } q.Pop(); } c.input[c.outcount] = c.input[c.outcount]; // printf("%-30s", n.c_str()); //把empty符号删除,因为empty表示空 if (treestack.Peek().type == "empty") { treestack.Pop(); } if (b.Peek()[2] == "empty") { b.Pop(); } //如果某个栈只剩$了,而另一个不可能变成$,那么就结束并报错 if ((treestack.Peek().type == "$" && b.Peek()[2] != "$") || (treestack.Peek().type != "$" && b.Peek()[2] == "$")) { c.action[c.outcount] = c.action[c.outcount] + "该语句出错"; c.programright = false; break; } //当两个栈的栈顶不同时 if (treestack.Peek().type != b.Peek()[2]) { Production k = Grammarsheet_Return_Production(treestack.Peek().type, b.Peek()[2], c); //假如能在符号表中找到生成式,说明分析过程还是正确的 if (k.productionSubstring[0] != "error" && k.productionSubstring[0] != "synch") { //构建临时list临时存放生成式的node,用于反向放入treestack中 List <Node> temp = new List <Node>(); //输出动作 c.action[c.outcount] = c.action[c.outcount] + "输出 "; c.action[c.outcount] = c.action[c.outcount] + Grammarsheet_Return(treestack.Peek().type, b.Peek()[2], c); //构建list Production currentproduction = Grammarsheet_Return_Production(treestack.Peek().type, b.Peek()[2], c); for (int i = 2; i < 10; i++) { if (currentproduction.productionSubstring[i] != "" && currentproduction.productionSubstring[i] != null) { //获取当前孩子节点中的某个 Node currentnode = new Node("", currentproduction.productionSubstring[i], "", "", ""); temp.Add(currentnode);// //取栈顶作为父节点来加入他的孩子 Node tmpnode = treestack.Peek(); tmpnode.Add(currentnode); //计算当前节点所在的层 currentnode.layer = tmpnode.layer + 1; //计算list的高 if (currentnode.layer >= treeroot.layerheight) { treeroot.layerheight = currentnode.layer; } } } treestack.Pop(); //treestack放入生成式节点 for (int i = temp.Count - 1; i >= 0; i--) { if (k.productionSubstring[i] != "" && k.productionSubstring[i] != null) { treestack.Push(temp[i]); } } //清空临时list temp.Clear(); } //如果没有对应的生成式,则弹出输入栈中最上方的字符并报错 else if (k.productionSubstring[0] == "error") { //输出动作 c.action[c.outcount] = c.action[c.outcount] + "出错"; c.errorshow = c.errorshow + "第" + b.Peek()[4] + "行 " + "缺少或多余" + found(b.Peek()[2], c) + "\r\n" + "\r\n"; c.programright = false; b.Pop(); } //如果对应的生成式为错误处理,则弹出过程栈中最上方的非终结符并报错 else if (k.productionSubstring[0] == "synch") { //输出动作 c.action[c.outcount] = c.action[c.outcount] + "出错"; c.errorshow = c.errorshow + "第" + b.Peek()[4] + "行 " + "缺少或多余" + found(b.Peek()[2], c) + "\r\n" + "\r\n"; c.programright = false; treestack.Pop(); } } //两个栈的栈顶相同 else if (treestack.Peek().type == b.Peek()[2]) { treestack.Peek().name = b.Peek()[1]; treestack.Peek().symbolname = b.Peek()[1]; treestack.Peek().value = b.Peek()[3]; treestack.Peek().symbolvalue = b.Peek()[3]; treestack.Peek().lineNo = b.Peek()[4]; treestack.Peek().columnNo = b.Peek()[5]; if (treestack.Peek().type == "}") { c.action[c.outcount] = c.action[c.outcount] + "匹配 " + treestack.Peek().type + "该句子正确"; } else { c.action[c.outcount] = c.action[c.outcount] + "匹配 " + treestack.Peek().type; } treestack.Pop(); b.Pop(); } } }
public void constructTree(string symbol, List <string> list)//symbol是产生式左边的符号,list存放产生式右边的符号 { bool finish = false; while (!finish) { if (!currentNode.HasChild) //如果没有子节点 { if (currentNode.Symbol == symbol) //当前node就是要找的,那么直接增加子节点 { foreach (string s in list) { if (!nonTerminalSet.Contains(s)) //如果s是终结符 { if (s == "ID") //记录下产生ID的产生式,放入栈中,当下次匹配到ID时,就可以找到这个节点的位置 { IDNodes.Push(currentNode); Node tmp = new Node(s); tmp.row = row; tmp.col = col; currentNode.Add(tmp); } else if (s == "NUM") { NUMNodes.Push(currentNode); Node tmp = new Node(s); tmp.row = row; tmp.col = col; currentNode.Add(tmp); } else { Node tmp = new Node(s, s); tmp.row = row; tmp.col = col; currentNode.Add(tmp); } } else { currentNode.Add(new Node(s)); } } finish = true; } else { currentNode.IsChecked = true; currentNode = currentNode.Parent; } } else { bool find = false; foreach (Node n in currentNode.Children) { if (!n.IsChecked && n.Symbol == symbol) { currentNode = n; find = true; break; } } if (!find) { currentNode.IsChecked = true; currentNode = currentNode.Parent; } } } }