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

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.