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:
â | 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 ...
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.