Making Things Happen During the Parse

Listeners and visitors are great because they keep application-specific code out of grammars, making grammars easier to read and preventing them from getting entangled with a particular application. For the ultimate flexibility and control, however, we can directly embed code snippets (actions) within grammars. These actions are copied into the recursive-descent parser code ANTLR generates. In this section, we’ll implement a simple program that reads in rows of data and prints out the values found in a specific column. After that, we’ll see how to make special actions, called semantic predicates, dynamically turn parts of a grammar on and off.

Embedding Arbitrary Actions in a Grammar

We can compute ...

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.