My grammar has these rules
expression
: expression EQ conditionalOrExpression #eqExpr
| expression NEQ conditionalOrExpression #neqExpr
| expression LT conditionalOrExpression #ltExpr
| expression GT conditionalOrExpression #gtExpr
| expression LTEQ conditionalOrExpression #lteqExpr
| expression GTEQ conditionalOrExpression #gteqExpr
| conditionalOrExpression #next
;
conditionalOrExpression
: conditionalOrExpression OR conditionalAndExpression #orExpr
| conditionalAndExpression #and
;
conditionalAndExpression
: conditionalAndExpression AND additiveExpression #andExpr
| additiveExpression #add
;
additiveExpression
: additiveExpression PLUS multiplicativeExpression #plusExpr
| additiveExpression MINUS multiplicativeExpression #minusExpr
| multiplicativeExpression #multiplicative
;
multiplicativeExpression
: multiplicativeExpression MULT unaryExpression #multExpr
| multiplicativeExpression DIV unaryExpression #divExpr
| unaryExpression #unary
;
unaryExpression
: MINUS unaryExpression #unaryMinusExpr
| NOT unaryExpression #notExpr
| atom #atomExpr
;
function
: ID OPAR (parameter (',' parameter)*)? CPAR
;
parameter
: STRING #stringParameter
| expression #exprParameter
;
atom
: OPAR expression CPAR #parExpr
| (INT | FLOAT) #numberAtom
| (TRUE | FALSE) #booleanAtom
| ID #idAtom
| function #functionAtom
;
I have implemented the appropriate Visitor.
If I evaluate "40 + 10 - (2*40) + (100/40) + 0.2" the result is -32.7. This is because the expression gets evaluated as
(40+10) - (((2*40) + (100/40)) + 0.2)
which makes sense according to the rules (PLUS before MINUS).
However, if I evaluate the same expression in Excel or e.g. assign it to a double in C#, in both cases the result is -27.3. This is because they evaluate the rule as
(((40+10)-(2*40)) + (100/40)) + 0.2
So which is "correct"? -32.7 is technically correct since that's what the rules dictate. But how should the grammar change to match the results in Excel/C#?