MSDN Magazine - February 2008 - (Page 65) The core of the parsing work is done by the ParseStmt method, as shown in Figure 5. It returns a Stmt node, which serves as the root node of the tree and matches the language syntax definition’s toplevel node. The parser traverses the list of tokens using an index Figure 4 AST for the Good for Nothing Compiler public abstract class Stmt { } // var = public class DeclareVar : Stmt { public string Ident; public Expr Expr; } // print public class Print : Stmt { public Expr Expr; } // = public class Assign : Stmt { public string Ident; public Expr Expr; } // for = to do end public class ForLoop : Stmt { public string Ident; public Expr From; public Expr To; public Stmt Body; } // read_int public class ReadInt : Stmt { public string Ident; } // ; public class Sequence : Stmt { public Stmt First; public Stmt Second; } as the current position while identifying tokens that are subservient to the Stmt node in the language syntax (variable declarations and assignments, for loops, read_ints, and prints). If a token can’t be identified, an exception is thrown. /* := * | * | * | */ public abstract class Expr { } // := “ * “ public class StringLiteral : Expr { public string Value; } // := + public class IntLiteral : Expr { public int Value; } // := * // := | public class Variable : Expr { public string Ident; } // := public class ArithExpr : Expr { public Expr Left; public Expr Right; public BinOp Op; } // := + | - | * | / public enum ArithOp { Add, Sub, Mul, Div } Figure 5 ParseStmt Method Identifies Tokens private Stmt ParseStmt() { Stmt result; if (this.index == this.tokens.Count) { throw new Exception(“expected statement, got EOF”); } if (this.tokens[this.index].Equals(“print”)) { this.index++; } else if (this.tokens[this.index].Equals(“var”)) { this.index++; } else if (this.tokens[this.index].Equals(“read_int”)) { this.index++; } } else if (this.tokens[this.index].Equals(“for”)) { this.index++; } else if (this.tokens[this.index] is string) { this.index++; } else { throw new Exception(“parse error at token “ + this.index + “: “ + this.tokens[this.index]); } .NET Compiler february2008 65
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.