} // NT_OutputStat private static void NT_DeleteStat(out Stat s) { int spix = 0; Symbol sy = null; SrcPos sp = null; s = null; for (;;) { switch (Syn.Interpret()) { case 0: return; case 1: // SEM sp = new SrcPos(); break; case 2: Lex.GETidentAttr(out spix); break; case 3: // SEM sy = SymTab.SymbolOf(spix, Symbol.Kind.varKind, Symbol.Kind.parKind); s = new DeleteStat(sp, new VarOperand(sy)); break; } // switch } // for } // NT_DeleteStat
} // NT_MiniCpp private static void NT_ConstDecl() { Type t = null; Symbol sy = null; Stat s = null; int spix = 0; for (;;) { switch (Syn.Interpret()) { case 0: return; case 1: NT_Type(out t); break; case 2: Lex.GETidentAttr(out spix); break; case 3: // SEM sy = SymTab.Insert(spix, Symbol.Kind.constKind, t, false); break; case 4: NT_Init(out s, ref sy); break; } // switch } // for } // NT_ConstDecl
} // Interpret public static bool Parse() { //-----------------------------------|---------------------------------------- Lex.InitLex(); sp = 0; pc = ROOTPC; altRoot = pc; mustRead = true; errDist = MINERRDIST + 1; leaveOrEnter = false; leaveCnt = 0; enterCnt = 0; Sem.StartSem(); return(Errors.NumOfSynErrors() == 0); } // Parse
} // Pop private static void GetNextToken() { for (;;) { Lex.GetToken(); // updates Lex.token if (Lex.token <= LASTTERM) { return; } #if !CONTPRAGMAACTS if (NumOfSynErrors() == 0) #endif Sem.pragmaMethods[Lex.token - LASTTERM - 1](); } // for } // GetNextToken
} // NT_VarDefOrFuncDeclOrDef private static void NT_VarDef(out Stat s) { Type t = null; Symbol sy = null; int spix = 0; bool ptr = false; s = null; for (;;) { switch (Syn.Interpret()) { case 0: return; case 1: NT_Type(out t); break; case 2: // SEM ptr = true; break; case 3: Lex.GETidentAttr(out spix); break; case 4: // SEM sy = SymTab.Insert(spix, Symbol.Kind.varKind, t, ptr); break; case 5: NT_VarDefRest(out s, t, sy, ptr); break; } // switch } // for } // NT_VarDef
} // NT_InputStat private static void NT_OutputStat(out Stat s) { Expr e = null; String str = null; SrcPos sp = null; System.Collections.ArrayList values = new System.Collections.ArrayList(); s = null; for (;;) { switch (Syn.Interpret()) { case 0: return; case 1: // SEM sp = new SrcPos(); break; case 2: NT_Expr(out e); break; case 3: // SEM values.Add(e); break; case 4: Lex.GETstringAttr(out str); break; case 5: // SEM values.Add(str); break; case 6: // SEM values.Add("\n"); break; case 7: NT_Expr(out e); break; case 8: // SEM values.Add(e); break; case 9: Lex.GETstringAttr(out str); break; case 10: // SEM values.Add(str); break; case 11: // SEM values.Add("\n"); break; case 12: // SEM s = new OutputStat(sp, values); break; } // switch } // for } // NT_OutputStat
} // NT_Stat private static void NT_IncDecAssignOrCallStat(out Stat s) { int spix = 0; Symbol sy = null; Expr lhs = null, e = null; SrcPos sp = null; s = null; for (;;) { switch (Syn.Interpret()) { case 0: return; case 1: Lex.GETidentAttr(out spix); break; case 2: // SEM sy = SymTab.SymbolOf(spix, Symbol.Kind.varKind, Symbol.Kind.parKind, Symbol.Kind.funcKind); break; case 3: // SEM s = new IncStat(sp, new VarOperand(sy)); break; case 4: // SEM s = new DecStat(sp, new VarOperand(sy)); break; case 5: // SEM lhs = new VarOperand(sy); break; case 6: // SEM sp = new SrcPos(); break; case 7: NT_Expr(out e); break; case 8: // SEM lhs = new ArrIdxOperator(sp, new VarOperand(sy), e); break; case 9: // SEM sp = new SrcPos(); break; case 10: NT_Expr(out e); break; case 11: // SEM s = new AssignStat(sp, lhs, e); break; case 12: // SEM sp = new SrcPos(); break; case 13: NT_ActParList(out e); break; case 14: // SEM s = new CallStat(sp, sy, e); break; } // switch } // for } // NT_IncDecAssignOrCallStat
} // NT_VarDefRest private static void NT_FormParList(Symbol funcSy) { int spix = 0; bool ptr = false; Type t = null; for (;;) { switch (Syn.Interpret()) { case 0: return; case 1: NT_Type(out t); break; case 2: // SEM if (t.kind != Type.Kind.voidKind) { SemErr("void expected"); } break; case 3: // SEM ptr = true; break; case 4: Lex.GETidentAttr(out spix); break; case 5: // SEM if (ptr) { SemErr("pointer to array not supported"); } ptr = true; break; case 6: // SEM SymTab.Insert(spix, Symbol.Kind.parKind, t, ptr); ptr = false; break; case 7: NT_Type(out t); break; case 8: // SEM ptr = true; break; case 9: Lex.GETidentAttr(out spix); break; case 10: // SEM if (ptr) { SemErr("pointer to array not supported"); } ptr = true; break; case 11: // SEM SymTab.Insert(spix, Symbol.Kind.parKind, t, ptr); ptr = false; break; } // switch } // for } // NT_FormParList
} // NT_VarDef private static void NT_VarDefRest(out Stat s, Type t, Symbol sy, bool ptr) { int spix = 0; Stat statList = null, s2 = null; s = null; for (;;) { switch (Syn.Interpret()) { case 0: return; case 1: NT_Init(out statList, ref sy); break; case 2: // SEM sy.init = true; break; case 3: // SEM ptr = false; break; case 4: // SEM ptr = true; break; case 5: Lex.GETidentAttr(out spix); break; case 6: // SEM sy = SymTab.Insert(spix, Symbol.Kind.varKind, t, ptr); break; case 7: NT_Init(out s2, ref sy); break; case 8: // SEM sy.init = true; if (statList == null) { statList = s2; } else // append s2 to statList { s = statList; while (s.next != null) { s = s.next; } // while s.next = s2; } // else break; case 9: // SEM ptr = false; break; case 10: // SEM s = statList; break; } // switch } // for } // NT_VarDefRest
} // NT_Init private static void NT_VarDefOrFuncDeclOrDef() { Type t = null; Symbol funcSy = null, sy = null, locSymbols = null; Stat statList = null, s = null; int spix = 0; bool ptr = false; for (;;) { switch (Syn.Interpret()) { case 0: return; case 1: NT_Type(out t); break; case 2: // SEM ptr = true; break; case 3: Lex.GETidentAttr(out spix); break; case 4: // SEM sy = SymTab.Insert(spix, Symbol.Kind.varKind, t, ptr); break; case 5: NT_VarDefRest(out s, /*deliveres s = null*/ t, sy, ptr); break; case 6: // SEM funcSy = SymTab.Lookup(spix); if (funcSy == null) { funcSy = SymTab.Insert(spix, Symbol.Kind.funcKind, t, ptr); } else if (funcSy.kind != Symbol.Kind.undefKind) { if (funcSy.kind == Symbol.Kind.funcKind && funcSy.defined) { SemErr("multiple function decl or def"); } else if (funcSy.kind != Symbol.Kind.funcKind) { SemErr("invalid redefinition"); funcSy.kind = Symbol.Kind.undefKind; } // else } // else SymTab.EnterScope(); ptr = false; break; case 7: NT_FormParList(funcSy); break; case 8: // SEM if (funcSy.kind == Symbol.Kind.funcKind) { if (funcSy.hadFuncDecl) { SemErr("multiple function declaration"); } else { funcSy.hadFuncDecl = true; funcSy.funcDeclParList = SymTab.CurSymbols(); } // else } // if SymTab.LeaveScope(); break; case 9: // SEM funcSy.symbols = SymTab.CurSymbols(); funcSy.FuncDef(); // do decl and def match? curFuncSy = funcSy; loopLevel = 0; break; case 10: NT_Block(out locSymbols, out statList); break; case 11: // SEM funcSy.symbols = locSymbols; funcSy.statList = statList; SymTab.LeaveScope(); break; } // switch } // for } // NT_VarDefOrFuncDeclOrDef
} // NT_NotFact private static void NT_Fact(out Expr f) { Expr e = null; SrcPos sp = null; Symbol sy = null; int spix = 0; int number = 0; Type t = null; f = null; for (;;) { switch (Syn.Interpret()) { case 0: return; case 1: // SEM f = new LitOperand(Type.boolType, 0); break; case 2: // SEM f = new LitOperand(Type.boolType, 1); break; case 3: Lex.GETnumberAttr(out number); break; case 4: // SEM f = new LitOperand(Type.intType, number); break; case 5: Lex.GETidentAttr(out spix); break; case 6: // SEM sp = new SrcPos(); break; case 7: // SEM sy = SymTab.SymbolOf(spix, Symbol.Kind.constKind, Symbol.Kind.varKind, Symbol.Kind.parKind); f = new VarOperand(sy); break; case 8: // SEM sy = SymTab.SymbolOf(spix, Symbol.Kind.varKind, Symbol.Kind.parKind); break; case 9: // SEM sp = new SrcPos(); break; case 10: NT_Expr(out e); break; case 11: // SEM f = new ArrIdxOperator(sp, new VarOperand(sy), e); break; case 12: // SEM sy = SymTab.SymbolOf(spix, Symbol.Kind.funcKind); break; case 13: NT_ActParList(out e); break; case 14: // SEM f = new FuncCallOperator(sp, sy, e); break; case 15: // SEM sp = new SrcPos(); break; case 16: NT_Type(out t); break; case 17: NT_Expr(out e); break; case 18: // SEM f = new NewOperator(sp, t, e); break; case 19: NT_Expr(out e); break; case 20: // SEM f = e; break; } // switch } // for } // NT_Fact
} // NT_ConstDecl private static void NT_Init(out Stat s, ref Symbol sy) { int fact = 1, number = 0; s = null; for (;;) { switch (Syn.Interpret()) { case 0: return; case 1: // SEM if (sy.type != Type.boolType) { SemErr("invalid type"); } sy.val = 0; break; case 2: // SEM if (sy.type != Type.boolType) { SemErr("invalid type"); } sy.val = 1; break; case 3: // SEM fact = -1; break; case 4: Lex.GETnumberAttr(out number); break; case 5: // SEM if (sy.type.IsPtrType()) { if (number != 0) { SemErr("invalid value"); } } else if (sy.type.kind != Type.Kind.intKind) { SemErr("invalid type"); } sy.val = fact * number; break; case 6: // SEM if (sy.kind == Symbol.Kind.varKind && loopLevel > 0) { s = new AssignStat(new SrcPos(), new VarOperand(sy), new LitOperand(sy.type, sy.val)); } break; } // switch } // for } // NT_Init