Grammar Definition
In my understanding ANTLR4 support left recursion to respect order of precedence for arithmetic. With that said here's the grammar:
grammar Arithmetic;
arithmetic: arithmeticExpression;
arithmeticExpression:
LPARAN inner = arithmeticExpression RPARAN # Parentheses
| left = arithmeticExpression POW right = arithmeticExpression # Power
| left = arithmeticExpression MUL right = arithmeticExpression # Multiplication
| left = arithmeticExpression DIV right = arithmeticExpression # Division
| left = arithmeticExpression ADD right = arithmeticExpression # Addition
| left = arithmeticExpression SUB right = arithmeticExpression # Subtraction
| arithmeticExpressionInput # ArithmeticInput;
arithmeticExpressionInput: NUMBER;
number: NUMBER;
/* Operators */
LPARAN: '(';
RPARAN: ')';
POW: '^';
MUL: '*';
DIV: '/';
ADD: '+';
SUB: '-';
/* Data Types */
NUMBER: '-'? [0-9]+;
/* Whitespace & End of Lines */
EOL: '\r'? '\n';
WS: [ \t]+ -> channel(HIDDEN);
Note: I've simplified the grammar for testing.
Input
5 + 21 / 7 * 3
Output Parse Tree
Problem
In the outputted parse tree starting at the arithmetic. You can see that the Order of Precedence is not following PEMDAS even though it's defined via left recursion in the grammar. This is also observed when debugging the visitor code generated by Antlr with the function call being VisitAddition.
I've google this and I can't see what I'm doing wrong compared to examples as they all look the same.
Environment
ANTLR Version: 4.11.1
Build Target: CSharp
.NET Project Packages:
- Antlr4BuildTasks@11.1.0
- Antlr4.Runtime.Standard@4.11.1

