-1

I've just begun working with ANTLR and was trying to follow a simple example I found on SO from awhile ago: ANTLR: Is there a simple example?

I want to do a similar type of thing with my ANTLR 4 project. Does anyone know how I could do this?

I have my grammar defined and it can generate correct trees.

Essentially, I want to see the answer generated from a mathematical expression, not just a confirmation that my grammar is correct.

Please help!

Community
  • 1
  • 1
erip
  • 27
  • 5
  • This Q&A contains an example of an expression parser: http://stackoverflow.com/questions/15610183/if-else-statements-in-antlr-using-listeners – Bart Kiers Mar 06 '14 at 07:05

1 Answers1

0

The book goes through all this but the basic idea if you just want to calculator is to return a value from the expression rule:

e returns [int value] : left=e '*' right=e {$value = $left.value * $right.value;} | left=e '+' right=e {$value = $left.value + $right.value;} | INT {$value = $INT.int;} ;

Terence Parr
  • 5,912
  • 26
  • 32