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.

​ file ​:​ prop​+​ ​;​
​ prop ​:​ ID ​'='​ STRING ​'\n'​ ​;​

Here’s a sample property file: ...

Get The Definitive ANTLR 4 Reference, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.