I am struggling to understand the ANTLR4 algorithm and how it handles left-recursion. Hoping someone can educate my a bit.
Take the below left recursive grammar:
grammar Dummy;
TOK1 : 'foo';
TOKE_OPT : 'bar';
TOK2 : 'baz';
TOKDERP : 'derp';
SPACES
: [ \u000B\t\r\n] -> channel(HIDDEN)
;
rr
: rr TOK1 rr TOKE_OPT?
| '(' TOK2 ')'
| TOKDERP
;
And the following input string:
derp foo derp foo derp
When run through TestRig -diagnostics
ANTLR concludes the grammar to be ambiguous and I don't understand why:
line 1:5 reportAttemptingFullContext d=2 (rr), input='foo'
line 1:9 reportContextSensitivity d=2 (rr), input='foo derp'
line 1:14 reportAttemptingFullContext d=2 (rr), input='foo'
line 2:0 reportAmbiguity d=2 (rr): ambigAlts={1, 2}, input='foo derp
'
It would be greatly appreciated if someone can explain why this grammar is ambiguous and how one would get rid of the ambiguity. It's also possible that I don't understand why ambiguity means :).
If I remove the TOKE_OPT?
clause the warnings go away.
I am using ANTLR version 4.7.2
rr (TOK1 rr)
– Danielno viable alternative at input '<EOF>'
error? That's the result of a long-standing Antlr bug which can be worked around by adding a start symbol. – rici