Why is antlr4 complaining of mutually left-recursive ?

559 views
Skip to first unread message

Peter Li

unread,
Apr 15, 2018, 3:14:50 AM4/15/18
to antlr-discussion
Hi,

I am trying to develop an equivalent functionality of regex's "(x){n,m}", i.e. x is repeated n to m times.
This is my initial version, for "x{2,3}" (The actual grammar will be generated by a preprocessor, so that x, min, and max can be arbitrary values):

grammar Test;
@parser::members { int count = 0; }
note: line | note line;
line: repeatX '\n' {System.out.println("repeat");}
   
| anyX '\n' {System.out.println("any");}
   
| '\n' {System.out.println("none");}
   
;
repeatX
@init { count = 0; }
     : X X {count = 2;}
     | {count < 3}? repeatX X {count++;}
    ;
anyX : X+;
X: 'x';

antlr4 complains with:

error(119): Test.g4::: The following sets of rules are mutually left-recursive [repeatX]


Which seems odd, since the exact same pattern is found in "note: ...". Can someone tell me why ?

I can fix it by flipping "repeatX X" to "X repeatX", then with a test file like:


x
xx
xxx
xxxx
xxxxx

I get the output:

none

any

repeat

repeat

any

any

any



But I can't figure out why antlr4 would complain in the first place?

Thanks in advance.
ps. I am also trying to move the declaration of count in the "@parser::members{}" to be rule specific, i.e. inside "@init{ }", but that's another story.
Reply all
Reply to author
Forward
0 new messages