When I attempt to compile my antlr4 calculator grammar, it turns out it is left recursive. I need to change it to make it correct.
I have tried rewriting the rules and using different parentheses locations but they all don't work. Here's my latest version of the error rules:
Parser:
expression: INT | DECIMAL | arithmetic;
arithmetic: expression OPERATION expression;
Lexer:
OPERATION: SUB | ADD | MULT | DIV;
SUB: '-';
ADD: '+';
MULT: '*';
DIV: '/';
DPOINT: '.';
INT: SUB? NUMBER+;
DECIMAL: SUB? NUMBER+ DPOINT NUMBER+;
I expect the compilation to be successful, but the following error occurs:
ANTLR Tool v4.4 (/tmp/antlr-4.4-complete.jar)
hZH.g4 -o /home/heng/workspace/Ultimate ZH Compiler/target/generated-sources/antlr4 -listener -no-visitor -encoding UTF-8
error(119): hZH.g4::: The following sets of rules are mutually left-recursive [expression, arithmetic]
1 error(s)
BUILD FAIL
How can I change my rules for the build to be successful?