Parsers Implementation
Explore how ANTLR v4 generates recursive-descent parsers from grammar definitions. Understand the structure and function of parser methods for grammar rules, including lookahead token decision-making. Gain insights into debugging and modifying ANTLR-generated parsers by applying changes to grammar files and observing their impact on parser code.
We'll cover the following...
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:
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 ...