antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

Left recursive rule with argument gets flagged as mutually left recursive

Open atlv24 opened this issue 3 years ago • 3 comments

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]

atlv24 avatar Nov 09 '20 02:11 atlv24

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.)

Tinker1024 avatar Nov 28 '21 14:11 Tinker1024

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.

KvanTTT avatar Nov 28 '21 16:11 KvanTTT

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.

KvanTTT avatar Apr 09 '22 19:04 KvanTTT