Parser analyzes a code syntactically according to the rules of the language's grammar. The parsing phase determines if the input code can be used to form a string of tokens according to the defined grammar. A parse tree is constructed in this phase. Parser defines functions to organize language into a data structure called AST. The parser defined in this recipe uses a recursive decent parser technique which is a top-down parser, and uses mutually recursive functions to build the AST.
We must have the custom-defined language, that is the TOY language in this case, and also a stream of tokens generated by the lexer.
Define some basic value holders in our TOY parser as shown in the following:
toy.cpp
file as follows:$ vi toy.cpp
static int Current_token;
static void next_token() { Current_token = get_token(); }
static BaseAST* Base_Parser() { switch (Current_token) { default: return 0; case IDENTIFIER_TOKEN : return identifier_parser(); case NUMERIC_TOKEN : return numeric_parser(); case '(' : return paran_parser(); } }
The stream of input is tokenized and fed to the parser. Current_token
holds the token to be processed. The type of token is known at this stage and the corresponding parser functions are called to initialize ASTs.
18.118.10.32