3
votes

For given input:

hello hello

And grammar:

grammar test;

foo: bar ;
bar   : 'hello' bar | EOF;

WS  : [ \t\r\n]+ -> skip ;

I get a valid parse tree for rule bar.

But when I remove foo rule:

grammar test;

bar   : 'hello' bar | EOF;

WS  : [ \t\r\n]+ -> skip ;

I get an error "line 1:11 no viable alternative at input '< EOF>". What is going on here? I am using intellij plugin for ANTLR4 for rule testing.

1
Smells like a bug. :-\Lucas Trzesniewski
It really looks like a bug. I found out they had issues with EOF and first rule like three years ago. I guess it was hard to fix.Tomas Bruckner
This is another case of github.com/antlr/antlr4/issues/118.Sam Harwell
@xfrog Fixing it wasn't too hard. Fixing it without making ANTLR very slow turned out to be very hard.Sam Harwell

1 Answers

0
votes

As confirmed by the co-author of ANTLR4 Sam Harwell, it is bug in ANTLR4. It can be tracked here: https://github.com/antlr/antlr4/issues/118

Workaround is as suggested in the question (use extra root rule foo).