Search code examples
parsingantlr4left-recursion

How to modify grammar to remove left recursive error in ANTLR4?


I'm trying to parse a language. The follow ANTLR4 parser rules are directly copied from the language specification :

physical_value 
 : raw_value DIV factor MUL factor PLUS offset 
 ;

raw_value
 : (physical_value MINUS offset) DIV factor  
 ;

but antlr reports an error:The following sets of rules are mutually left-recursive I don't know how to modify the grammar, Hope someone can help me. Thanks.


Solution

  • You cannot eliminate the left recursion from the rules you posted, because the only string it matches is an infinite sequence.

    • physical_value always starts with a raw_value
    • raw_value always starts with a physical_value

    ...and repeat