Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANTLR mismatched input '<EOF>'

Tags:

antlr

antlr4

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?

like image 621
Jared Beck Avatar asked Oct 04 '13 03:10

Jared Beck


1 Answers

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.

like image 147
Sam Harwell Avatar answered Nov 08 '22 00:11

Sam Harwell