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

Building a Calculator with Grammar Actions

Let’s revisit the expression grammar from Building a Calculator Using a Visitor to learn about actions. In that section, we built a calculator using a tree visitor that evaluated expressions such as the following:

actions/t.expr
 
x = 1
 
x
 
x+2*3

Our goal here is to reproduce that same functionality, but without using a visitor and without even building a parse tree. Moreover, we’ll employ a little trick to make it interactive, meaning we get results when we hit Return, not at the end of the input. Our examples so far have scarfed up the entire input and then processed the resulting parse trees.

As we go through this section, we’re going to learn how to put generated parsers into packages, define ...

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