//条件部分 private void Condition(int level, List <LexicalAnalysis.SY> followSet) { if (tmpword.Sym == LexicalAnalysis.SY.ODDSY) { Getsym(); Expression(level, followSet); ipt.AddPcode(Pcode.PC.OPR, 0, 6); //OPR 0 6;栈顶元素的奇偶判断 } else { List <LexicalAnalysis.SY> next = new List <LexicalAnalysis.SY>(); next.Clear(); next.AddRange(followSet); next.Add(LexicalAnalysis.SY.EQUSY); next.Add(LexicalAnalysis.SY.UNEQUSY); next.Add(LexicalAnalysis.SY.LESSSY); next.Add(LexicalAnalysis.SY.LESY); next.Add(LexicalAnalysis.SY.GRTSY); next.Add(LexicalAnalysis.SY.GESY); Expression(level, next); //当前符号是否为=,<>,<,<=,>,>=中的一种 if (tmpword.Sym == LexicalAnalysis.SY.EQUSY || tmpword.Sym == LexicalAnalysis.SY.UNEQUSY || tmpword.Sym == LexicalAnalysis.SY.LESSSY || tmpword.Sym == LexicalAnalysis.SY.LESY || tmpword.Sym == LexicalAnalysis.SY.GRTSY || tmpword.Sym == LexicalAnalysis.SY.GESY) { LexicalAnalysis.SY op = tmpword.Sym; Getsym(); Expression(level, followSet); ipt.AddPcode(Pcode.PC.OPR, 0, GetOperator(op)); } else { err.AddError(20, tmpword.LineID, tmpword.token); } } }
//项部分 private void Term(int level, List <LexicalAnalysis.SY> followSet) { List <LexicalAnalysis.SY> next = new List <LexicalAnalysis.SY>(); next.Clear(); next.AddRange(followSet); next.Add(LexicalAnalysis.SY.STARSY); next.Add(LexicalAnalysis.SY.DIVISY); Factor(level, next); while (tmpword.Sym == LexicalAnalysis.SY.STARSY || tmpword.Sym == LexicalAnalysis.SY.DIVISY) { LexicalAnalysis.SY op = tmpword.Sym; Getsym(); Factor(level, next); ipt.AddPcode(Pcode.PC.OPR, 0, GetOperator(op)); } }
//表达式部分 private void Expression(int level, List <LexicalAnalysis.SY> followSet) { if (tmpword.Sym == LexicalAnalysis.SY.PLUSSY || tmpword.Sym == LexicalAnalysis.SY.MINUSSY) { LexicalAnalysis.SY op = tmpword.Sym; Getsym(); List <LexicalAnalysis.SY> next = new List <LexicalAnalysis.SY>(); next.Clear(); next.AddRange(followSet); next.Add(LexicalAnalysis.SY.PLUSSY); next.Add(LexicalAnalysis.SY.MINUSSY); Term(level, next); if (op == LexicalAnalysis.SY.MINUSSY) { ipt.AddPcode(Pcode.PC.OPR, 0, 1); //OPR 0 1;栈顶元素取反 } } else { List <LexicalAnalysis.SY> next = new List <LexicalAnalysis.SY>(); next.Clear(); next.AddRange(followSet); next.Add(LexicalAnalysis.SY.PLUSSY); next.Add(LexicalAnalysis.SY.MINUSSY); Term(level, next); } while (tmpword.Sym == LexicalAnalysis.SY.PLUSSY || tmpword.Sym == LexicalAnalysis.SY.MINUSSY) { LexicalAnalysis.SY op = tmpword.Sym; Getsym(); List <LexicalAnalysis.SY> next = new List <LexicalAnalysis.SY>(); next.Clear(); next.AddRange(followSet); next.Add(LexicalAnalysis.SY.PLUSSY); next.Add(LexicalAnalysis.SY.MINUSSY); Term(level, next); ipt.AddPcode(Pcode.PC.OPR, 0, GetOperator(op)); } }
//转换为pcode中对应的操作码值 private int GetOperator(LexicalAnalysis.SY x) { switch (x) { case LexicalAnalysis.SY.PLUSSY: return(2); case LexicalAnalysis.SY.MINUSSY: return(3); case LexicalAnalysis.SY.STARSY: return(4); case LexicalAnalysis.SY.DIVISY: return(5); case LexicalAnalysis.SY.EQUSY: return(8); case LexicalAnalysis.SY.UNEQUSY: return(9); case LexicalAnalysis.SY.LESSSY: return(10); case LexicalAnalysis.SY.GESY: return(11); case LexicalAnalysis.SY.GRTSY: return(12); case LexicalAnalysis.SY.LESY: return(13); } return(0); }