ANTLR 不匹配输入“<EOF>" [英] ANTLR mismatched input '<EOF>'
问题描述
鉴于以下 ANTLR 4.1 语法,有意注释掉一行...
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]+ ;
.. 为什么 echo -n "a EQ b" |grun Foobar 表达式
生成
line 1:6 mismatched input '<EOF>' expecting WS
.. 但是如果我们取消注释上面的 block : expression ;
行,那么 grun
不会产生错误?
.. but if we uncomment the block : expression ;
line above then grun
produces no errors?
推荐答案
您看到了一个罕见但已知的错误的影响:
没有明确的 EOF 的启动规则不会错误地抛出任何可行的替代方案
You are seeing the effects of a rare but known bug:
No viable alternative can be incorrectly thrown for start rules without explicit EOF
正确修复此问题对性能的影响目前惊人,因此我们无意在可预见的未来应用此补丁.解决方法是创建一个以显式 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 不匹配输入“<EOF>"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!