I'm new to ANTLR and I'm attempting to generate a relatively simply parser for a calculator application, using ANTLR4 and C#. Here is my grammar file.
parser grammar CalculatorExpressionParser;
options{
language = CSharp2;
}
expr: FUNC expr? (COMMA expr)* RIGHTPAREN #CallFunction
| LEFTPAREN expr RIGHTPAREN #Parens
| expr POW<assoc=right> expr #Pow
| expr op=(MULTIPLY | DIVIDE)? expr #MultDivide
| expr op=(ADD | SUBTRACT) expr #AddSubtract
| SUBTRACT expr #Negative
| NUMBER #Number
;
I wrote a custom lexer to generate the tokens in order to support implicit multiplication and conversion of variables to their numerical equivalent before parsing.
But upon input I get the following results.
2+6/3 => 4 (correct)
6/3+2 => 1.2 (should be 4)
6/(3+2) => 4 (also correct)
1+2*3 => 7 (correct)
2*3+1 => 8 (should be 7 too)
(2*3)+1 => 7 (correct)
Note that I tried turning off the error recovery capabilities and setting it to report all ambiguity errors and I don't appear to be getting any.
Anyways, if I change the grammar to the following by removing the ? after the division/multiplication operator, then it appears to work great, except implicit multiplication is no longer supported.
parser grammar CalculatorExpressionParser;
options{
language = CSharp2;
}
expr: FUNC expr? (COMMA expr)* RIGHTPAREN #CallFunction
| LEFTPAREN expr RIGHTPAREN #Parens
| expr POW<assoc=right> expr #Pow
| expr op=(MULTIPLY | DIVIDE) expr #MultDivide
| expr op=(ADD | SUBTRACT) expr #AddSubtract
| SUBTRACT expr #Negative
| NUMBER #Number
;
I was wondering why putting the ? breaks the matching order? And is there an easy way to fix this?