1
votes

This is my trimmed down ANTLR4 grammar (note I'm using a constant false to replace my method that returns false ):

grammar AnnotProcessor;
    cppCompilationUnit:  content+ EOF;

    content: anything  
           | {false}? .; 

    anything: ANY_CHAR;

    ANY_CHAR: [_a-zA-Z0-9];

My test file contains only 1 word "hello" and the test results are like these:

D:\work\antlr4\work>java org.antlr.v4.runtime.misc.TestRig AnnotProcessor cppCompilationUnit -tree in.cpp
line 1:5 no viable alternative at input '<EOF>'
(cppCompilationUnit (content (anything h)) (content (anything e)) (content (anything l)) (content (anything l)) (content (anything o)))

D:\work\antlr4\work>java org.antlr.v4.runtime.misc.TestRig AnnotProcessor cppCompilationUnit -tokens in.cpp
[@0,0:0='h',<1>,1:0]
[@1,1:1='e',<1>,1:1]
[@2,2:2='l',<1>,1:2]
[@3,3:3='l',<1>,1:3]
[@4,4:4='o',<1>,1:4]
[@5,5:4='<EOF>',<-1>,1:5]
line 1:5 no viable alternative at input '<EOF>'

Why it keeps saying "line 1:5 no viable alternative at input '< EOF >'" when I add a semantic predicate (although a dummy here) as an alternative? If I remove the alternative with the false semantic predicate, the error disappears as expected.

PS: I'm using antlr-4.0-complete.jar

1

1 Answers

1
votes

Yes, this is a bug. In ANTLR 4, the introduction of an alternative starting with {false}? [almost] anywhere in the grammar should not affect the parse result for any [valid] input.

Can you report the issue here:
https://github.com/antlr/antlr4/issues

Edit: This is issue #218, now fixed
https://github.com/antlr/antlr4/issues/218

PS: Your use of the non-greedy operator +? is either unnecessary or incorrect in the cppCompilationUnit rule. If you meant to require at least one content element, you can simply use +. However, what I think you meant to write is zero-or-more content elements: (content+)?, which can be simplified to just content*.