Jared Beck
Jared Beck

Reputation: 17538

ANTLR mismatched input '<EOF>'

Given the following ANTLR 4.1 grammar, with one line intentionally commented out ...

grammar Foobar;

//whyDoesThisRuleHelp : expression ;
expression : operand | binaryOperation ;
binaryOperation : operand WS BINARY_OPERATOR WS expression ;
operand : LETTER ;

BINARY_OPERATOR : 'EQ' ;
LETTER : [a-z] ;
WS : [ \n]+ ;

.. why does echo -n "a EQ b" | grun Foobar expression produce

line 1:6 mismatched input '<EOF>' expecting WS

.. but if we uncomment the block : expression ; line above then grun produces no errors?

Upvotes: 5

Views: 11175

Answers (1)

Sam Harwell
Sam Harwell

Reputation: 100039

You are seeing the effects of a rare but known bug:
No viable alternative can be incorrectly thrown for start rules without explicit EOF

The performance implications of properly fixing this are currently staggering, so we have no intention of applying the patch for the foreseeable future. The workaround is to create a rule that ends with an explicit EOF, and start parsing there.

Upvotes: 6

Related Questions