O'Reilly logo

The Definitive ANTLR 4 Reference, 2nd Edition by Terence Parr

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing Applications with Parse-Tree Listeners

To build language applications without entangling the application and the grammar, the key is to have the parser create a parse tree and then walk it to trigger application-specific code. We can walk the tree using our favorite technique, or we can use one of the tree-walking mechanisms that ANTLR generates. In this section, we’re going to use ANTLR’s built-in ParseTreeWalker to build a listener-based version of the property file application from the previous section.

Let’s start with a denuded version of the property file grammar.

listeners/PropertyFile.g4
 
file : prop+ ;
 
prop : ID ​'='​ STRING ​'\n'​ ;

Here’s a sample property file:

listeners/t.properties
 
user="parrt"
 
machine="maniac" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required