//<过程调用语句> ::= call<标识符> private void CallState(int level, List <LexicalAnalysis.SY> followSet) { Getsym(); if (tmpword.Sym == LexicalAnalysis.SY.IDSY) { int idx = symtable.Position(tmpword.token); if (idx > 0) { SymbolTable.Item item = symtable.table[idx]; if (item.type == 2) //类型为子程序 { Console.WriteLine(item.name + item.address); ipt.AddPcode(Pcode.PC.CAL, level - item.level, item.address); } else { err.AddError(15, tmpword.LineID, tmpword.token); //call不能调用常量或者变量 } } else { err.AddError(11, tmpword.LineID, tmpword.token); //标识符未说明 } Getsym(); } else { err.AddError(14, tmpword.LineID, tmpword.token); //call后应为标识符 } }
//ident赋值模块 private void IdentState(int level, List <LexicalAnalysis.SY> followSet) { int idx = symtable.Position(tmpword.token); if (idx <= 0) { err.AddError(11, tmpword.LineID, tmpword.token); //标识符未说明 } else if (symtable.table[idx].type != 1) { err.AddError(12, tmpword.LineID, tmpword.token); //赋值语句中,赋值号左部标识符属性应是变量 idx = -1; } Getsym(); if (tmpword.Sym == LexicalAnalysis.SY.ASSIGNSY) { Getsym(); } else { err.AddError(13, tmpword.LineID, tmpword.token); // 赋值语句左部标识符后应是赋值运算符\":=\ } Expression(level, followSet); if (idx > 0) { SymbolTable.Item item = symtable.table[idx]; ipt.AddPcode(Pcode.PC.STO, level - item.level, item.address); } }
//read模块 private void ReadState(int level, List <LexicalAnalysis.SY> followSet) { Getsym(); if (tmpword.Sym == LexicalAnalysis.SY.LPARSY) { int idx = -1; do { Getsym(); if (tmpword.Sym == LexicalAnalysis.SY.IDSY) { idx = symtable.Position(tmpword.token); } if (idx < 0) { err.AddError(11, tmpword.LineID, tmpword.token); //标识符未说明 } else { SymbolTable.Item item = symtable.table[idx]; if (item.type == 1) //类型为变量 { ipt.AddPcode(Pcode.PC.RED, level - item.level, item.address); //ipt.AddPcode(Pcode.PC.STO, level - item.level, item.address); } else { err.AddError(31, tmpword.LineID, tmpword.token); // read括号内应该是变量标识符 } } Getsym(); } while (tmpword.Sym == LexicalAnalysis.SY.COMMASY); } else { err.AddError(40, tmpword.LineID, tmpword.token); // 应为左括号 } if (tmpword.Sym == LexicalAnalysis.SY.RPARSY) { Getsym(); } else { err.AddError(22, tmpword.LineID, tmpword.token); //表达式中漏掉右括号 } }
//因子部分 private void Factor(int level, List <LexicalAnalysis.SY> followSet) { Test(factorPre, followSet, 24); //判断表达式开始符号 if (factorPre.Contains(tmpword.Sym) == true) { if (tmpword.Sym == LexicalAnalysis.SY.IDSY) { int idx = symtable.Position(tmpword.token); if (idx >= 0) { SymbolTable.Item item = symtable.table[idx]; switch (item.type) { case 0: //取常量值于栈顶 ipt.AddPcode(Pcode.PC.LIT, 0, item.value); break; case 1: //取变量值于栈顶 ipt.AddPcode(Pcode.PC.LOD, level - item.level, item.address); break; case 2: err.AddError(21, tmpword.LineID, tmpword.token); //表达式中不能有过程标识符 break; } } else { err.AddError(11, tmpword.LineID, tmpword.token); //标识符未说明 } Getsym(); } else if (tmpword.Sym == LexicalAnalysis.SY.INTSY) { int num = int.Parse(tmpword.token); ipt.AddPcode(Pcode.PC.LIT, 0, num); Getsym(); } else if (tmpword.Sym == LexicalAnalysis.SY.LPARSY) //左括号 { Getsym(); List <LexicalAnalysis.SY> next = new List <LexicalAnalysis.SY>(); next.Clear(); next.AddRange(followSet); next.Add(LexicalAnalysis.SY.RPARSY); Expression(level, next); if (tmpword.Sym == LexicalAnalysis.SY.RPARSY) //判断下一个符号是否为右括号 { Getsym(); } else { err.AddError(22, tmpword.LineID, tmpword.token); } } List <LexicalAnalysis.SY> next1 = new List <LexicalAnalysis.SY>(); next1.Clear(); next1.Add(LexicalAnalysis.SY.LPARSY); Test(followSet, next1, 23); //判断是否有非法符号 } }