Odd behavior of parser when trying sample grammar
-
16-09-2019 - |
Question
I'm trying to get the feel for antlr3, and i pasted the Expression evaluator into an ANTLRWorks window (latest version) and compiled it. It compiled successfully and started, but two problems:
- Attempting to use a input of
1+2*4/3;
resulted in the actual input for the parser being1+2*43
. - One of the errors it shows in it's graphical parser tree is
MissingTokenException(0!=0)
.
As i'm new to antlr, can someone help?
Solution
The example you linked to doesn't support division (just look at the code, you'll notice there's no division here:
expr returns [int value]
: e=multExpr {$value = $e.value;}
( '+' e=multExpr {$value += $e.value;}
| '-' e=multExpr {$value -= $e.value;}
)*
OTHER TIPS
We often get
MissingTokenException(0!=0)
when we make mistakes. I think it means that it cannot find a token it's looking for, and could be produced by an incorrect token. It's possible for the parser to "recover" sometimes depending on the grammar.
Remember also that the LEXER operates before the parser and your should check what tokens are actually passed to the parser. The AntlrWorks debugger can be very helpful here.