Parsers Implementation

Learn how a parser can be generated with the ANTLR tool and also see the implementation details of the generated parser.

In the lessons “ANother Tool for Language Recognition (ANTLR),” “ANTLR v4 Grammar,” and “ANTLR Parse Tree,” we learned about different constructs of ANTLR grammar. We have also applied the ANTLR tool to a simple grammar (Welcome). We have noticed that the ANTLR tool automatically generates parsers. However, it would be worth noticing what a parser looks like. Moreover, it would be helpful to debug the generated parsers. In this section, we will learn how the ANTLR tool generates parser functions for each grammar rule. Moreover, we will learn about the helper functions that are used inside the parse function.

Recursive-descent parsers

The ANTLR tool generates recursive-descent parsers from grammar rules. Recursive descent parsers are a collection of recursive methods, one per grammar rule. The term “descent” means parsing starts at the root of the parsing tree and moves towards the leaf nodes, where a rule becomes the root node and the tokens are the leaf nodes. To get an idea of what recursive-descent parsers look like, we take the grammar (ABC.g4) defined in the previous chapter as an example:

Press + to interact
grammar ABC;
stmt : assign // Assignement Statement
| conditional // Conditional Statement
| loop // While loop
| stmt ; stmt // Sequence of statements
;
assign : ID '=' exp ';'; // x = 5, y = 10, ...
conditional : 'if' '('exp')' stmt 'else' stmt;
loop : 'while' '('exp')' stmt;
exp : INT;
INT : [0-9]+; // Integer values
ID : [a-z]+; // a, abc, xyz, ....
WS : [ \t\n] -> skip; // Skip spaces, tabs and newlines.

Now let’s see what the parser methods for the above grammar rules look like. First of all, we will start with the starting rule, stmt.

Parser for rule stmt

Below is the code of the parser functions ...