/* * public int[] layercal(Node n) * { * //用数组k[]来记录每层的节点个数并算出每个节点在该层的位置 * int[] k = new int[n.layerheight + 1]; * k[0] = 1; * //初始化k[] * for (int m = 1; m < n.layerheight + 1; m++) * { * k[m] = 0; * } * Queue<Node> queue = new Queue<Node>(200); * queue.Enqueue(n); * //广度遍历树 * while (queue.Count != 0) * { * Node tmp1 = queue.Dequeue(); * if (tmp1.hasChild()) * { * for (int i = 0; i < tmp1.getChilds().Count; i++) * { * //该层节点数加1 * k[tmp1.getChilds()[i].layer]++; * //每个节点在该层的位置 * tmp1.getChilds()[i].layercount = k[tmp1.getChilds()[i].layer]; * queue.Enqueue(tmp1.getChilds()[i]); * } * } * } * return k; * } * * //等分画树 * public void drawTreeDivide(Node n, Graphics g) * { * int[] k = layercal(tree); * * //------------画树 * if (n.hasChild()) * { * int size = n.getChilds().Count; * for (int i = 0; i < size; i++) * { * //获得每层的间隔 * int x = this.Width / (k[n.getChilds()[i].layer] + 1); * int y = this.Height / (tree.layerheight + 1) * (n.getChilds()[i].layer); * n.getChilds()[i].positionX = x * n.getChilds()[i].layercount; * n.getChilds()[i].positionY = y; * g.DrawString(Convert.ToString(n.getChilds()[i].layer), * new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(0, y)); * g.DrawString(n.getChilds()[i].type, * new Font("Arial", 12), System.Drawing.Brushes.Purple, new Point(x * n.getChilds()[i].layercount, y - 10)); * drawTreeDivide(n.getChilds()[i], g); * g.DrawLine(Pens.Blue, new Point(n.positionX, n.positionY), new Point(x * n.getChilds()[i].layercount, y)); * * } * * } * } */ //先计算叶子节点个数 public void drawLeafFirstCal(ref int totalleafcount, Node n, Graphics g) { if (n.hasChild()) { int size = n.getChilds().Count; for (int i = 0; i < size; i++) { drawLeafFirstCal(ref totalleafcount, n.getChilds()[i], g); } } else { totalleafcount++; n.leafcount = totalleafcount; } }
public void drawLeafFirstLocation(int totalleafcount, Node n, Graphics g) { if (n.hasChild()) { int ntotalX = 0; int size = n.getChilds().Count; for (int i = 0; i < size; i++) { drawLeafFirstLocation(totalleafcount, n.getChilds()[i], g); ntotalX = ntotalX + n.getChilds()[i].positionX; } n.positionX = ntotalX / n.getChilds().Count; n.positionY = this.Height / (tree.layerheight + 1) * n.layer; } else { n.positionX = this.Width / (totalleafcount + 1) * n.leafcount; n.positionY = this.Height / (tree.layerheight + 1) * n.layer; } }
//先画叶子节点的方法画树 public void drawLeafFirst(Node n, Graphics g) { if (n.hasChild()) { int size = n.getChilds().Count; for (int i = 0; i < size; i++) { int m = judge(n.getChilds()[i], treestack); if (m == 1) { drawLeafFirst(n.getChilds()[i], g); //行号 g.DrawString(Convert.ToString(n.getChilds()[i].layer), new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(0, n.getChilds()[i].positionY)); //字符串 g.DrawString(n.getChilds()[i].name + "," + n.getChilds()[i].type + "," + n.getChilds()[i].value, new Font("Arial", 12), System.Drawing.Brushes.Purple, new Point(n.getChilds()[i].positionX, n.getChilds()[i].positionY - 10)); //线 g.DrawLine(Pens.Blue, new Point(n.positionX, n.positionY), new Point(n.getChilds()[i].positionX, n.getChilds()[i].positionY)); } else { drawLeafFirst(n.getChilds()[i], g); //行号 g.DrawString(Convert.ToString(n.getChilds()[i].layer), new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(0, n.getChilds()[i].positionY)); //字符串 /* g.DrawString(n.getChilds()[i].symbolname + "," + n.getChilds()[i].symboltype + "," + n.getChilds()[i].symbolvalue,// + "," + n.getChilds()[i].lineNo + "," + n.getChilds()[i].columnNo + "," + n.getChilds()[i].symboltype, * new Font("Arial", 12), System.Drawing.Brushes.Green, new Point(n.getChilds()[i].positionX, n.getChilds()[i].positionY - 10));*/ //线 g.DrawLine(Pens.Orange, new Point(n.positionX, n.positionY), new Point(n.getChilds()[i].positionX, n.getChilds()[i].positionY)); } } } }
//先画叶子节点的方法画树 public void drawLeafFirst(Node n, Graphics g) { if (n.hasChild()) { int size = n.getChilds().Count; for (int i = 0; i < size; i++) { int m = judge(n.getChilds()[i], treestack); if (m == 1) { drawLeafFirst(n.getChilds()[i], g); //行号 g.DrawString(Convert.ToString(n.getChilds()[i].layer), new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(0, n.getChilds()[i].positionY)); //字符串 g.DrawString(n.getChilds()[i].name + "," + n.getChilds()[i].type + "," + n.getChilds()[i].value, new Font("Arial", 12), System.Drawing.Brushes.Purple, new Point(n.getChilds()[i].positionX, n.getChilds()[i].positionY - 10)); //线 g.DrawLine(Pens.Blue, new Point(n.positionX, n.positionY), new Point(n.getChilds()[i].positionX, n.getChilds()[i].positionY)); } else { drawLeafFirst(n.getChilds()[i], g); //行号 g.DrawString(Convert.ToString(n.getChilds()[i].layer), new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(0, n.getChilds()[i].positionY)); //字符串 /* g.DrawString(n.getChilds()[i].symbolname + "," + n.getChilds()[i].symboltype + "," + n.getChilds()[i].symbolvalue,// + "," + n.getChilds()[i].lineNo + "," + n.getChilds()[i].columnNo + "," + n.getChilds()[i].symboltype, new Font("Arial", 12), System.Drawing.Brushes.Green, new Point(n.getChilds()[i].positionX, n.getChilds()[i].positionY - 10));*/ //线 g.DrawLine(Pens.Orange, new Point(n.positionX, n.positionY), new Point(n.getChilds()[i].positionX, n.getChilds()[i].positionY)); } } } }
public void drawLeafFirstLocation(int totalleafcount, Node n, Graphics g) { if (n.hasChild()) { int ntotalX = 0; int size = n.getChilds().Count; for (int i = 0; i < size; i++) { drawLeafFirstLocation(totalleafcount, n.getChilds()[i], g); ntotalX = ntotalX + n.getChilds()[i].positionX; } n.positionX = ntotalX / n.getChilds().Count; n.positionY = this.Height / (tree.layerheight + 1) * n.layer; } else { n.positionX = this.Width / (totalleafcount + 1) * n.leafcount; n.positionY = this.Height / (tree.layerheight + 1) * n.layer; } }
/* public int[] layercal(Node n) { //用数组k[]来记录每层的节点个数并算出每个节点在该层的位置 int[] k = new int[n.layerheight + 1]; k[0] = 1; //初始化k[] for (int m = 1; m < n.layerheight + 1; m++) { k[m] = 0; } Queue<Node> queue = new Queue<Node>(200); queue.Enqueue(n); //广度遍历树 while (queue.Count != 0) { Node tmp1 = queue.Dequeue(); if (tmp1.hasChild()) { for (int i = 0; i < tmp1.getChilds().Count; i++) { //该层节点数加1 k[tmp1.getChilds()[i].layer]++; //每个节点在该层的位置 tmp1.getChilds()[i].layercount = k[tmp1.getChilds()[i].layer]; queue.Enqueue(tmp1.getChilds()[i]); } } } return k; } //等分画树 public void drawTreeDivide(Node n, Graphics g) { int[] k = layercal(tree); //------------画树 if (n.hasChild()) { int size = n.getChilds().Count; for (int i = 0; i < size; i++) { //获得每层的间隔 int x = this.Width / (k[n.getChilds()[i].layer] + 1); int y = this.Height / (tree.layerheight + 1) * (n.getChilds()[i].layer); n.getChilds()[i].positionX = x * n.getChilds()[i].layercount; n.getChilds()[i].positionY = y; g.DrawString(Convert.ToString(n.getChilds()[i].layer), new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(0, y)); g.DrawString(n.getChilds()[i].type, new Font("Arial", 12), System.Drawing.Brushes.Purple, new Point(x * n.getChilds()[i].layercount, y - 10)); drawTreeDivide(n.getChilds()[i], g); g.DrawLine(Pens.Blue, new Point(n.positionX, n.positionY), new Point(x * n.getChilds()[i].layercount, y)); } } } */ //先计算叶子节点个数 public void drawLeafFirstCal(ref int totalleafcount, Node n, Graphics g) { if (n.hasChild()) { int size = n.getChilds().Count; for (int i = 0; i < size; i++) { drawLeafFirstCal(ref totalleafcount, n.getChilds()[i], g); } } else { totalleafcount++; n.leafcount = totalleafcount; } }
public int threeaddresscount = 1;//三地址行标 public void symboltable(Node n) { switch (n.type) { case "program": symboltable(n.getChilds()[0]); break; case "compoundstmt": symboltable(n.getChilds()[1]); break; case "stmts": if (n.hasChild()) { if (n.getChilds().Count == 2) { symboltable(n.getChilds()[0]); symboltable(n.getChilds()[1]); } else if (n.getChilds().Count == 1) { symboltable(n.getChilds()[0]); } } break; case "stmt": symboltable(n.getChilds()[0]); break; case "decl": symboltable(n.getChilds()[0]); n.symboltype = n.getChilds()[0].getChilds()[0].symboltype; n.getChilds()[1].symboltype = n.symboltype; symboltable(n.getChilds()[1]); break; case "list": if (n.getChilds().Count == 2) { n.getChilds()[0].symboltype = n.symboltype; n.getChilds()[1].symboltype = n.symboltype; } else { n.getChilds()[0].symboltype = n.symboltype; } if (n.getChilds()[0].type == "ID") { bool redesign = false;//判断重复声明,重复为true for (int m = 0; m < symbolTableCount; m++) { if (n.getChilds()[0].symbolname == symbolTableName[m]) { redesign = true; symbolTableType[m] = "重复声明"; symbolTableValue[m] = ""; symbolTableLineNo[m] = ""; symbolTableColumnNo[m] = ""; } } if (!redesign) { symbolTableName[symbolTableCount] = n.getChilds()[0].symbolname; symbolTableType[symbolTableCount] = n.getChilds()[0].symboltype; symbolTableValue[symbolTableCount] = "未赋值"; symbolTableLineNo[symbolTableCount] = n.getChilds()[0].lineNo; symbolTableColumnNo[symbolTableCount] = n.getChilds()[0].columnNo; symbolTableCount++; } } symboltable(n.getChilds()[1]); break; case "list1": if (n.getChilds().Count == 2) { n.getChilds()[0].symboltype = n.symboltype; n.getChilds()[1].symboltype = n.symboltype; symboltable(n.getChilds()[1]); } else { n.getChilds()[0].symboltype = n.symboltype; symboltable(n.getChilds()[0]); } if (n.getChilds()[0].type == "ID") { bool redesign = false; //判断重复声明,重复为true for (int m = 0; m < symbolTableCount; m++) { if (n.getChilds()[0].symbolname == symbolTableName[m]) { redesign = true; symbolTableType[m] = "重复声明"; symbolTableValue[m] = ""; symbolTableLineNo[m] = ""; symbolTableColumnNo[m] = ""; } } if (!redesign) { symbolTableName[symbolTableCount] = n.getChilds()[0].symbolname; symbolTableType[symbolTableCount] = n.getChilds()[0].symboltype; symbolTableValue[symbolTableCount] = "未赋值"; symbolTableLineNo[symbolTableCount] = n.getChilds()[0].lineNo; symbolTableColumnNo[symbolTableCount] = n.getChilds()[0].columnNo; symbolTableCount++; } } break; case "assgstmt": symboltable(n.getChilds()[2]); n.getChilds()[0].symbolvalue = n.getChilds()[2].symbolvalue; bool occur = false; //判断是否为赋值但没声明 for (int m = 0; m < symbolTableCount; m++) { if (n.getChilds()[0].symbolname == symbolTableName[m]) occur = true; } if (!occur) { symbolTableName[symbolTableCount] = n.getChilds()[0].symbolname; symbolTableType[symbolTableCount] = "未声明"; symbolTableValue[symbolTableCount] = ""; symbolTableLineNo[symbolTableCount] = ""; symbolTableColumnNo[symbolTableCount] = ""; symbolTableCount++; } else { //判断赋值类型是否一致 bool judge = false;//判断是否值为小数,小数为true for (int l = 0; l < n.getChilds()[0].symbolvalue.Length; l++) if (n.getChilds()[0].symbolvalue[l] == '.') judge = true; if (n.getChilds()[2].symboltype == "real") judge = true; if (judge) { for (int m = 0; m < symbolTableCount; m++) { if (n.getChilds()[0].symbolname == symbolTableName[m] && symbolTableType[m] == "int") { symbolTableType[m] = "类型不匹配"; symbolTableValue[m] = ""; symbolTableLineNo[m] = ""; symbolTableColumnNo[m] = ""; } if (n.getChilds()[0].symbolname == symbolTableName[m] && symbolTableType[m] == "real") { symbolTableValue[m] = n.getChilds()[0].symbolvalue; } } } else { for (int m = 0; m < symbolTableCount; m++) { if (n.getChilds()[0].symbolname == symbolTableName[m]) { symbolTableValue[m] = n.getChilds()[0].symbolvalue; } } } } break; case "arithexpr": //对直接赋值和运算赋值分别讨论 symboltable(n.getChilds()[0]); symboltable(n.getChilds()[1]); if (n.hasChild()) { if (n.getChilds()[1].getChilds().Count == 1) { n.symboltype = n.getChilds()[0].symboltype; n.symbolvalue = n.getChilds()[0].symbolvalue; } else if (n.getChilds()[1].getChilds().Count == 3) { bool occurleft=false; bool occurright=false; for (int m = 0; m < symbolTableCount; m++) { if (n.getChilds()[0].symbolname == symbolTableName[m]) { n.getChilds()[0].symboltype = symbolTableType[m]; occurleft=true; } if (n.getChilds()[1].symbolname == symbolTableName[m]) { n.getChilds()[1].symboltype = symbolTableType[m]; occurright=true; } } if( occurleft&&occurright) { if (n.getChilds()[0].symboltype == "int" && n.getChilds()[1].symboltype == "int") n.symboltype = "int"; if (n.getChilds()[0].symboltype == "int" && n.getChilds()[1].symboltype == "real") n.symboltype = "real"; if (n.getChilds()[0].symboltype == "real" && n.getChilds()[1].symboltype == "int") n.symboltype = "real"; if (n.getChilds()[0].symboltype == "real" && n.getChilds()[1].symboltype == "real") n.symboltype = "real"; } } } break; case "multexpr": symboltable(n.getChilds()[0]); n.symbolname = n.getChilds()[0].symbolname; n.symboltype = n.getChilds()[0].symboltype; n.symbolvalue = n.getChilds()[0].symbolvalue; break; case "simpleexpr": symboltable(n.getChilds()[0]); n.symbolname = n.getChilds()[0].symbolname; n.symboltype = n.getChilds()[0].symboltype; n.symbolvalue = n.getChilds()[0].symbolvalue; break; case "arithexprprime": if (n.getChilds().Count == 3) { symboltable(n.getChilds()[1]); symboltable(n.getChilds()[2]); n.symbolname = n.getChilds()[1].symbolname; } break; default: break; } }
public void threeaddress(Node n) { switch (n.type) { case "program": threeaddress(n.getChilds()[0]); threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":"; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; break; case "compoundstmt": threeaddress(n.getChilds()[1]); break; case "stmts": if (n.getChilds().Count == 2) { threeaddress(n.getChilds()[0]); threeaddress(n.getChilds()[1]); } else { threeaddress(n.getChilds()[0]); } break; case "stmt": threeaddress(n.getChilds()[0]); tempcount = 0; break; case "assgstmt": threeaddress(n.getChilds()[2]); threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "MOV" + " " + n.getChilds()[0].name + "," + "," + n.getChilds()[2].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; break; case "arithexpr": threeaddress(n.getChilds()[0]); threeaddress(n.getChilds()[1]); if (n.getChilds()[1].getChilds()[0].type=="empty") { n.value = n.getChilds()[0].value; } else { n.value = temppro(); tempcount++; } if (n.getChilds()[1].getChilds()[0].name == "+") { threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "ADD" + " " + n.value + "," + n.getChilds()[0].name + "," + n.getChilds()[1].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } else if (n.getChilds()[1].getChilds()[0].name == "-") { threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "SUB" + " " + n.value + "," + n.getChilds()[0].name + "," + n.getChilds()[1].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } break; case "multexpr": threeaddress(n.getChilds()[0]); threeaddress(n.getChilds()[1]); if (n.getChilds()[1].getChilds().Count == 1) { n.value = n.getChilds()[0].value; n.name = n.getChilds()[0].name; } if (n.getChilds()[1].getChilds().Count == 3) { n.value = temppro(); tempcount++; if (n.getChilds()[1].getChilds()[0].name == "/") { threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "DIV" + " " + n.value + "," + n.getChilds()[0].name + "," + n.getChilds()[1].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } else if (n.getChilds()[1].getChilds()[0].name == "*") { threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "MUL" + " " + n.value + "," + n.getChilds()[0].name + "," + n.getChilds()[1].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } } break; case "arithexprprime": if (n.getChilds().Count == 3) { threeaddress(n.getChilds()[1]); threeaddress(n.getChilds()[2]); n.value = n.getChilds()[1].value; n.name = n.getChilds()[1].name; } else { threeaddress(n.getChilds()[0]); } break; case "simpleexpr": if (n.getChilds().Count == 3) { threeaddress(n.getChilds()[1]); n.value = n.getChilds()[1].value; } else { threeaddress(n.getChilds()[0]); n.value = n.getChilds()[0].name; n.name = n.getChilds()[0].name; } break; case "multexprprime": if (n.getChilds().Count == 3) { threeaddress(n.getChilds()[1]); threeaddress(n.getChilds()[2]); n.name = n.getChilds()[1].name; n.value = n.getChilds()[1].value; } else { threeaddress(n.getChilds()[0]); } break; case "ifstmt": threeaddress(n.getChilds()[2]); threeaddress(n.getChilds()[5]); threeaddress(n.getChilds()[6]); threeaddress(n.getChilds()[7]); elseend = threeaddresscount; threeaddressshow[elsestart] = threeaddressshow[elsestart] + Convert.ToString(elseend); break; case "whilestmt": threeaddress(n.getChilds()[2]); threeaddress(n.getChilds()[4]); threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "JMP" + " " + "," + ","+Convert.ToString(whilestart); n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; threeaddressshow[(whilestart + 1)] = threeaddressshow[whilestart + 1] + Convert.ToString(threeaddresscount); break; case "decl": threeaddress(n.getChilds()[0]); threeaddress(n.getChilds()[1]); break; case "type": threeaddress(n.getChilds()[0]); break; case "list": threeaddress(n.getChilds()[1]); break; case "list1": if (n.getChilds().Count == 2) { threeaddress(n.getChilds()[1]); } else { threeaddress(n.getChilds()[0]); } break; case "boolexpr": threeaddress(n.getChilds()[0]); threeaddress(n.getChilds()[1]); threeaddress(n.getChilds()[2]); if(n.getChilds()[1].getChilds()[0].type=="<") { n.value = temppro(); tempcount++; whilestart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "LES" + " " + n.value + "," + n.getChilds()[0].value + "," + n.getChilds()[2].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; thenstart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "JMPF" + " " + n.value + "," + ","; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } else if (n.getChilds()[1].getChilds()[0].type == ">") { n.value = temppro(); tempcount++; whilestart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "GTR" + " " + n.value + "," + n.getChilds()[0].value + "," + n.getChilds()[2].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; thenstart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "JMPF" + " " + n.value + "," + ","; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } else if (n.getChilds()[1].getChilds()[0].type == "<=") { n.value = temppro(); tempcount++; whilestart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "LEQ" + " " + n.value + "," + n.getChilds()[0].value + "," + n.getChilds()[2].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; thenstart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "JMPF" + " " + n.value + "," + ","; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } else if (n.getChilds()[1].getChilds()[0].type == ">=") { n.value = temppro(); tempcount++; whilestart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "GEQ" + " " + n.value + "," + n.getChilds()[0].value + "," + n.getChilds()[2].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; thenstart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "JMPF" + " " + n.value + "," + ","; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } else if (n.getChilds()[1].getChilds()[0].type == "==") { n.value = temppro(); tempcount++; whilestart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "EQL" + " " + n.value + "," + n.getChilds()[0].value + "," + n.getChilds()[2].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; thenstart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "JMPF" + " " + n.value + "," + ","; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } break; case "boolop": threeaddress(n.getChilds()[0]); break; case "else": threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "JMP" + " " + "," + ","; n.name = threeaddressshow[threeaddresscount]; elsestart = threeaddresscount; threeaddresscount++; threeaddressshow[thenstart] = threeaddressshow[thenstart] + Convert.ToString(threeaddresscount); break; default: break; } }
public void threeaddress(Node n) { switch (n.type) { case "program": threeaddress(n.getChilds()[0]); threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":"; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; break; case "compoundstmt": threeaddress(n.getChilds()[1]); break; case "stmts": if (n.getChilds().Count == 2) { threeaddress(n.getChilds()[0]); threeaddress(n.getChilds()[1]); } else { threeaddress(n.getChilds()[0]); } break; case "stmt": threeaddress(n.getChilds()[0]); tempcount = 0; break; case "assgstmt": threeaddress(n.getChilds()[2]); threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "MOV" + " " + n.getChilds()[0].name + "," + "," + n.getChilds()[2].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; break; case "arithexpr": threeaddress(n.getChilds()[0]); threeaddress(n.getChilds()[1]); if (n.getChilds()[1].getChilds()[0].type == "empty") { n.value = n.getChilds()[0].value; } else { n.value = temppro(); tempcount++; } if (n.getChilds()[1].getChilds()[0].name == "+") { threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "ADD" + " " + n.value + "," + n.getChilds()[0].name + "," + n.getChilds()[1].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } else if (n.getChilds()[1].getChilds()[0].name == "-") { threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "SUB" + " " + n.value + "," + n.getChilds()[0].name + "," + n.getChilds()[1].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } break; case "multexpr": threeaddress(n.getChilds()[0]); threeaddress(n.getChilds()[1]); if (n.getChilds()[1].getChilds().Count == 1) { n.value = n.getChilds()[0].value; n.name = n.getChilds()[0].name; } if (n.getChilds()[1].getChilds().Count == 3) { n.value = temppro(); tempcount++; if (n.getChilds()[1].getChilds()[0].name == "/") { threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "DIV" + " " + n.value + "," + n.getChilds()[0].name + "," + n.getChilds()[1].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } else if (n.getChilds()[1].getChilds()[0].name == "*") { threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "MUL" + " " + n.value + "," + n.getChilds()[0].name + "," + n.getChilds()[1].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } } break; case "arithexprprime": if (n.getChilds().Count == 3) { threeaddress(n.getChilds()[1]); threeaddress(n.getChilds()[2]); n.value = n.getChilds()[1].value; n.name = n.getChilds()[1].name; } else { threeaddress(n.getChilds()[0]); } break; case "simpleexpr": if (n.getChilds().Count == 3) { threeaddress(n.getChilds()[1]); n.value = n.getChilds()[1].value; } else { threeaddress(n.getChilds()[0]); n.value = n.getChilds()[0].name; n.name = n.getChilds()[0].name; } break; case "multexprprime": if (n.getChilds().Count == 3) { threeaddress(n.getChilds()[1]); threeaddress(n.getChilds()[2]); n.name = n.getChilds()[1].name; n.value = n.getChilds()[1].value; } else { threeaddress(n.getChilds()[0]); } break; case "ifstmt": threeaddress(n.getChilds()[2]); threeaddress(n.getChilds()[5]); threeaddress(n.getChilds()[6]); threeaddress(n.getChilds()[7]); elseend = threeaddresscount; threeaddressshow[elsestart] = threeaddressshow[elsestart] + Convert.ToString(elseend); break; case "whilestmt": threeaddress(n.getChilds()[2]); threeaddress(n.getChilds()[4]); threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "JMP" + " " + "," + "," + Convert.ToString(whilestart); n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; threeaddressshow[(whilestart + 1)] = threeaddressshow[whilestart + 1] + Convert.ToString(threeaddresscount); break; case "decl": threeaddress(n.getChilds()[0]); threeaddress(n.getChilds()[1]); break; case "type": threeaddress(n.getChilds()[0]); break; case "list": threeaddress(n.getChilds()[1]); break; case "list1": if (n.getChilds().Count == 2) { threeaddress(n.getChilds()[1]); } else { threeaddress(n.getChilds()[0]); } break; case "boolexpr": threeaddress(n.getChilds()[0]); threeaddress(n.getChilds()[1]); threeaddress(n.getChilds()[2]); if (n.getChilds()[1].getChilds()[0].type == "<") { n.value = temppro(); tempcount++; whilestart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "LES" + " " + n.value + "," + n.getChilds()[0].value + "," + n.getChilds()[2].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; thenstart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "JMPF" + " " + n.value + "," + ","; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } else if (n.getChilds()[1].getChilds()[0].type == ">") { n.value = temppro(); tempcount++; whilestart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "GTR" + " " + n.value + "," + n.getChilds()[0].value + "," + n.getChilds()[2].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; thenstart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "JMPF" + " " + n.value + "," + ","; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } else if (n.getChilds()[1].getChilds()[0].type == "<=") { n.value = temppro(); tempcount++; whilestart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "LEQ" + " " + n.value + "," + n.getChilds()[0].value + "," + n.getChilds()[2].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; thenstart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "JMPF" + " " + n.value + "," + ","; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } else if (n.getChilds()[1].getChilds()[0].type == ">=") { n.value = temppro(); tempcount++; whilestart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "GEQ" + " " + n.value + "," + n.getChilds()[0].value + "," + n.getChilds()[2].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; thenstart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "JMPF" + " " + n.value + "," + ","; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } else if (n.getChilds()[1].getChilds()[0].type == "==") { n.value = temppro(); tempcount++; whilestart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "EQL" + " " + n.value + "," + n.getChilds()[0].value + "," + n.getChilds()[2].value; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; thenstart = threeaddresscount; threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "JMPF" + " " + n.value + "," + ","; n.name = threeaddressshow[threeaddresscount]; threeaddresscount++; } break; case "boolop": threeaddress(n.getChilds()[0]); break; case "else": threeaddressshow[threeaddresscount] = Convert.ToString(threeaddresscount) + ":" + " " + "JMP" + " " + "," + ","; n.name = threeaddressshow[threeaddresscount]; elsestart = threeaddresscount; threeaddresscount++; threeaddressshow[thenstart] = threeaddressshow[thenstart] + Convert.ToString(threeaddresscount); break; default: break; } }
public int threeaddresscount = 1; //三地址行标 public void symboltable(Node n) { switch (n.type) { case "program": symboltable(n.getChilds()[0]); break; case "compoundstmt": symboltable(n.getChilds()[1]); break; case "stmts": if (n.hasChild()) { if (n.getChilds().Count == 2) { symboltable(n.getChilds()[0]); symboltable(n.getChilds()[1]); } else if (n.getChilds().Count == 1) { symboltable(n.getChilds()[0]); } } break; case "stmt": symboltable(n.getChilds()[0]); break; case "decl": symboltable(n.getChilds()[0]); n.symboltype = n.getChilds()[0].getChilds()[0].symboltype; n.getChilds()[1].symboltype = n.symboltype; symboltable(n.getChilds()[1]); break; case "list": if (n.getChilds().Count == 2) { n.getChilds()[0].symboltype = n.symboltype; n.getChilds()[1].symboltype = n.symboltype; } else { n.getChilds()[0].symboltype = n.symboltype; } if (n.getChilds()[0].type == "ID") { bool redesign = false; //判断重复声明,重复为true for (int m = 0; m < symbolTableCount; m++) { if (n.getChilds()[0].symbolname == symbolTableName[m]) { redesign = true; symbolTableType[m] = "重复声明"; symbolTableValue[m] = ""; symbolTableLineNo[m] = ""; symbolTableColumnNo[m] = ""; } } if (!redesign) { symbolTableName[symbolTableCount] = n.getChilds()[0].symbolname; symbolTableType[symbolTableCount] = n.getChilds()[0].symboltype; symbolTableValue[symbolTableCount] = "未赋值"; symbolTableLineNo[symbolTableCount] = n.getChilds()[0].lineNo; symbolTableColumnNo[symbolTableCount] = n.getChilds()[0].columnNo; symbolTableCount++; } } symboltable(n.getChilds()[1]); break; case "list1": if (n.getChilds().Count == 2) { n.getChilds()[0].symboltype = n.symboltype; n.getChilds()[1].symboltype = n.symboltype; symboltable(n.getChilds()[1]); } else { n.getChilds()[0].symboltype = n.symboltype; symboltable(n.getChilds()[0]); } if (n.getChilds()[0].type == "ID") { bool redesign = false; //判断重复声明,重复为true for (int m = 0; m < symbolTableCount; m++) { if (n.getChilds()[0].symbolname == symbolTableName[m]) { redesign = true; symbolTableType[m] = "重复声明"; symbolTableValue[m] = ""; symbolTableLineNo[m] = ""; symbolTableColumnNo[m] = ""; } } if (!redesign) { symbolTableName[symbolTableCount] = n.getChilds()[0].symbolname; symbolTableType[symbolTableCount] = n.getChilds()[0].symboltype; symbolTableValue[symbolTableCount] = "未赋值"; symbolTableLineNo[symbolTableCount] = n.getChilds()[0].lineNo; symbolTableColumnNo[symbolTableCount] = n.getChilds()[0].columnNo; symbolTableCount++; } } break; case "assgstmt": symboltable(n.getChilds()[2]); n.getChilds()[0].symbolvalue = n.getChilds()[2].symbolvalue; bool occur = false; //判断是否为赋值但没声明 for (int m = 0; m < symbolTableCount; m++) { if (n.getChilds()[0].symbolname == symbolTableName[m]) { occur = true; } } if (!occur) { symbolTableName[symbolTableCount] = n.getChilds()[0].symbolname; symbolTableType[symbolTableCount] = "未声明"; symbolTableValue[symbolTableCount] = ""; symbolTableLineNo[symbolTableCount] = ""; symbolTableColumnNo[symbolTableCount] = ""; symbolTableCount++; } else { //判断赋值类型是否一致 bool judge = false; //判断是否值为小数,小数为true for (int l = 0; l < n.getChilds()[0].symbolvalue.Length; l++) { if (n.getChilds()[0].symbolvalue[l] == '.') { judge = true; } } if (n.getChilds()[2].symboltype == "real") { judge = true; } if (judge) { for (int m = 0; m < symbolTableCount; m++) { if (n.getChilds()[0].symbolname == symbolTableName[m] && symbolTableType[m] == "int") { symbolTableType[m] = "类型不匹配"; symbolTableValue[m] = ""; symbolTableLineNo[m] = ""; symbolTableColumnNo[m] = ""; } if (n.getChilds()[0].symbolname == symbolTableName[m] && symbolTableType[m] == "real") { symbolTableValue[m] = n.getChilds()[0].symbolvalue; } } } else { for (int m = 0; m < symbolTableCount; m++) { if (n.getChilds()[0].symbolname == symbolTableName[m]) { symbolTableValue[m] = n.getChilds()[0].symbolvalue; } } } } break; case "arithexpr": //对直接赋值和运算赋值分别讨论 symboltable(n.getChilds()[0]); symboltable(n.getChilds()[1]); if (n.hasChild()) { if (n.getChilds()[1].getChilds().Count == 1) { n.symboltype = n.getChilds()[0].symboltype; n.symbolvalue = n.getChilds()[0].symbolvalue; } else if (n.getChilds()[1].getChilds().Count == 3) { bool occurleft = false; bool occurright = false; for (int m = 0; m < symbolTableCount; m++) { if (n.getChilds()[0].symbolname == symbolTableName[m]) { n.getChilds()[0].symboltype = symbolTableType[m]; occurleft = true; } if (n.getChilds()[1].symbolname == symbolTableName[m]) { n.getChilds()[1].symboltype = symbolTableType[m]; occurright = true; } } if (occurleft && occurright) { if (n.getChilds()[0].symboltype == "int" && n.getChilds()[1].symboltype == "int") { n.symboltype = "int"; } if (n.getChilds()[0].symboltype == "int" && n.getChilds()[1].symboltype == "real") { n.symboltype = "real"; } if (n.getChilds()[0].symboltype == "real" && n.getChilds()[1].symboltype == "int") { n.symboltype = "real"; } if (n.getChilds()[0].symboltype == "real" && n.getChilds()[1].symboltype == "real") { n.symboltype = "real"; } } } } break; case "multexpr": symboltable(n.getChilds()[0]); n.symbolname = n.getChilds()[0].symbolname; n.symboltype = n.getChilds()[0].symboltype; n.symbolvalue = n.getChilds()[0].symbolvalue; break; case "simpleexpr": symboltable(n.getChilds()[0]); n.symbolname = n.getChilds()[0].symbolname; n.symboltype = n.getChilds()[0].symboltype; n.symbolvalue = n.getChilds()[0].symbolvalue; break; case "arithexprprime": if (n.getChilds().Count == 3) { symboltable(n.getChilds()[1]); symboltable(n.getChilds()[2]); n.symbolname = n.getChilds()[1].symbolname; } break; default: break; } }