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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With