antlr4
antlr4 copied to clipboard
Left recursive rule with argument gets flagged as mutually left recursive
There's only one rule, so there cannot be mutual recursion of any kind afaik.
grammar Repro;
expr[int a]
:{0 < $a}? 'thing'
|{1 < $a}? expr[1] '!'
;
Expected behavior: the rule is recognized as a form of left recursion that can be transformed and compiled, and the argument is kept as an additional restriction over the rule matching. Actual behavior: error
The following sets of rules are mutually left-recursive [expr]
I think what the error is saying is that you have a left-recursion of a grammar. It's a bane for the LL parsers. Considering about this possibility in your grammar (if expr[1] > 1):
{1 < $a}? expr[1] '!' --> expr[1] '!' --> {1 < $a}? expr[1] '!' -->...
Then, you will get an infinite loop. (BTW, ANTLR version 4 has the ability to deal with left-recursion within the same rule but not in your situation.)
Looks like it should be another error: rule is left recursive but doesn't conform to a pattern ANTLR can handle
instead of The following sets of rules are mutually left-recursive [expr]
. ANTLR is not able to handle left recursive rules with attributes in square brackets.
Maybe not very clear, the best message is left recursive rules are not allowed to have user-defined argument lists
. But it's an issue for another discussion.