/// <summary> /// 构造函数 /// </summary> public SymbolTable(PL0Compiler C) { this.lev = 0; this.tx[0] = 0; SymTabItem nul = new SymTabItem(); nul.name = ""; SymTable.Add(nul); this.compiler = C; }
/// <summary> /// 字符表登录函数 /// </summary> /// <param name="K">类型</param> public void Enter(ObjectK K) { GrammarAndSemanticAna GS = compiler.GSA; if (GS.getLevel() > lev) { lev++; tx[lev] = tx[lev - 1]; } else if (GS.getLevel() < lev) { lev--; } tx[lev]++; SymTabItem item = new SymTabItem(); item.kind = K; item.name = compiler.SA.tname; switch (K) { case ObjectK.constant: item.value = compiler.SA.tnumber; SymTable.Add(item); break; case ObjectK.variable: item.level = compiler.GSA.getLevel(); item.address = compiler.GSA.getAddress(); SymTable.Add(item); break; case ObjectK.procedure: item.level = compiler.GSA.getLevel(); SymTable.Add(item); break; } }
/// <summary> /// 因子模块 /// </summary> /// <param name="factSym">开始与后继符号集</param> private void factor(SymbolSet factSym) { int i; ErrorHandle err = compiler.EH; SymbolTable table = compiler.ST; PCodeGenetate pCode = compiler.PCG; SymbolSet sfb = new SymbolSet(); sfb.AddRange(factorbegs.SymSet); ErrorTest(sfb, factSym, 24); while (factorbegs.isInSet(sym)) { switch (sym) { case symbol.ident: i = table.getPosition(compiler.SA.tname); if (i == 0) { err.adderr(11); } else { SymTabItem STI = table.SymTable[i]; switch (STI.kind) { case ObjectK.constant: pCode.Gen(oprCode.lit, 0, STI.value); break; case ObjectK.variable: int fa = level - STI.level; int la = STI.address; pCode.Gen(oprCode.lod, fa, la); break; case ObjectK.procedure: err.adderr(21); break; } } getsym(); break; case symbol.number: pCode.Gen(oprCode.lit, 0, compiler.SA.tnumber); getsym(); break; case symbol.LParenthesis: getsym(); factSym.Add(symbol.RParenthesis); expression(factSym); if (sym == symbol.RParenthesis) { getsym(); } else { err.adderr(22); } break; } ErrorTest(factSym, factorbegs, 23); } }