ANTLR mismatched input '<EOF>'

这一生的挚爱 提交于 2019-12-10 14:40:37

问题


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.



来源:https://stackoverflow.com/questions/19172841/antlr-mismatched-input-eof

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!