ANTLR Parse Tree

Explore the ANTLR tool’s abstract syntax tree in depth.

In order to make a language application, we must execute some appropriate code for each input phrase. We can do that by operating on the parse tree created by the parser automatically. As we operate on the tree, we are back in a familiar Java environment. 

ANTLR data structures and classes

To begin, we will examine the data structure and class names ANTLR uses for recognition and parsing. In the previous chapters on lexical and syntactic analysis, we learned that lexers process characters and pass tokens to the parsers, which create a parse tree. In ANTLR, the corresponding classes are CharStreamLexerTokenParser, and ParseTree. The lexer and parser are connected via a pipe called TokenStream.

A simple ANTLR grammar (ABC.g4)

Let us start by defining our new programming language, ABC. The following is the ANTLR grammar of ABC language stored as ABC.g4.

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.
...