The parser module
By using recursive descent parsing, we will arrange the tokens to see whether expressions are valid, and generate the AST out of the input stream with the help of the lexical analyzer.
A recursive descent parser is a top-down parser built from a set of mutually-recursive procedures, where each such procedure usually implements one of the production rules of the grammar. Thus, the structure of the resulting program closely mirrors the grammar that it recognizes:
public class RDParser : Lexer { TOKEN Current_Token; public RDParser(String str): base(str){} public Exp CallExpr() { Current_Token = GetToken(); return Expr(); }
The constructor of the RDParser
class takes the expression string as a parameter, and passes it to the Lexer
class. Whenever the parser requires a token, it asks the Lexer
class to provide one through the GetToken()
method. The whole parsing...