public class Parser
static TextWriter output; static Token token; // last recognized token // TOKEN static Token laToken; // lookahead token (not yet recognized) static int la; // shortcut to kind attribute of lookahead token (laToken.kind) |
/* Symbol table object of currently compiled method. */ internal static Symbol curMethod; /* Special Label to represent an undefined break destination. */ static readonly Label undef; |
/* Reads ahead one symbol. */ static void Scan () { /*---------------------------------*/ /*----- insert your code here -----*/ /*---------------------------------*/ } |
/* Verifies symbol and reads ahead. */ static void Check (int expected) { /*---------------------------------*/ /*----- insert your code here -----*/ /*---------------------------------*/ } |
////////////////////////////////////////////////////////////////////////// /* Program = "class" ident { ConstDecl | VarDecl | ClassDecl } * "{" { MethodDecl } "}" . */ static void Program () { /*---------------------------------*/ /*----- insert your code here -----*/ /*---------------------------------*/ } |
/*======================================*/ /*===== insert parsing method here =====*/ /*======================================*/ /* ConstDecl = "const" Type ident "=" ( number | charConst ) ";" . */ /* VarDecl = Type ident { "," ident } ";" . */ /* ClassDecl = "class" ident "{" { VarDecl } "}" . */ ..... |
////////////////////////////////////////////////////////////////////////// /* Starts the analysis. * Output is written to System.out. */ public static void Parse () { Parse(Console.Out); } /* Starts the analysis. * Initializes the parser and all its components. * Output is written to TextWriter provided in output. */ public static void Parse (TextWriter output) { Parser.output = output; /*------------------------------------------------------*/ /*----- insert additional initialization code here -----*/ /*------------------------------------------------------*/ Tab.Init(); Errors.Init(); curMethod = null; token = null; laToken = new Token(1, 1); // avoid crash when 1st symbol has scanner error Scan(); // scan first symbol // SCANNER Program(); // start analysis Check(Token.EOF); } |
/* Handles all compiler errors. */ public class Errors { /* Minimal number of tokens between errors. * Errors are only reported if error distance is greater than this. */ const int minDist = 3; /* Current distance from last syntax error. */ public static int dist; /* Error count. */ static int cnt; /* Print error message to output and count reported errors. */ public static void Error (string msg) { /*---------------------------------*/ /*----- insert your code here -----*/ /*---------------------------------*/ } public static int Count { get { return cnt; } } public static void Init () { cnt = 0; Reset(); } public static void Reset () { dist = minDist; // don't skip errors any more } } |