ANTLR mismatched input ''

2019-06-21 19:42发布

问题:

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?

回答1:

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.



标签: antlr antlr4