Parse-Tree Listeners and Visitors

ANTLR provides support for two tree-walking mechanisms in its runtime library. By default, ANTLR generates a parse-tree listener interface that responds to events triggered by the built-in tree walker. The listeners themselves are exactly like SAX document handler objects for XML parsers. SAX listeners receive notification of events like startDocument and endDocument. The methods in a listener are just callbacks, such as we’d use to respond to a checkbox click in a GUI application. Once we look at listeners, we’ll see how ANTLR can also generate tree walkers that follow the visitor design pattern.[13]

Parse-Tree Listeners

To walk a tree and trigger calls into a listener, ANTLR’s runtime provides class ...

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.