简单语法上的 ANTLR MismatchedTokenException
我对 ANTLR 和 EBNF 语法完全陌生,所以这可能是我根本不理解的一个基本问题。
我有一个规则,例如:与
version_line : WS? 'VERS' WS? '=' WS? '1.0' WS? EOL ;
WS : ' '+ ;
EOL : '\r' | '\n' | '\r\n' | '\n\r' ;
我的输入文件中的语句匹配,如下所示(带有可选空格):
VERSION = 1.0
使用上面的规则形式,我得到了成功的匹配,尽管我得到了这种形式的异常:
version_line : WS? 'VERS' WS? '=' WS? '1' '.0' WS? EOL ;
或这个形式:
version_line : WS? 'VERS' WS? '=' WS? DIGIT '.0' WS? EOL ;
DIGIT : '1' ;
为什么会有所不同?我在尝试进一步分解规则时发现了这个问题,希望最终得到这样的结果:
version_line : WS? 'VERS' WS? '=' WS? DIGIT '.' DIGIT WS? EOL ;
DIGIT : '0'..'9' ;
I'm completely new to ANTLR and EBNF grammars to begin with, so this is probably a basic issue I'm simply not understanding.
I have a rule such as:
version_line : WS? 'VERS' WS? '=' WS? '1.0' WS? EOL ;
WS : ' '+ ;
EOL : '\r' | '\n' | '\r\n' | '\n\r' ;
that matches a statement in my input file that looks like this (with optional whitespace):
VERSION = 1.0
With the rule form above, I'm getting a successful match, although I get an exception with this form:
version_line : WS? 'VERS' WS? '=' WS? '1' '.0' WS? EOL ;
or this form:
version_line : WS? 'VERS' WS? '=' WS? DIGIT '.0' WS? EOL ;
DIGIT : '1' ;
Why is this different? I discovered this issue when trying to decompose the rule even more, hopefully ending up with something like this:
version_line : WS? 'VERS' WS? '=' WS? DIGIT '.' DIGIT WS? EOL ;
DIGIT : '0'..'9' ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为没有问题,所有四种语法都会产生预期的 AST:
1
2
3
4
使用输入:(
请注意,输入中的
#
是换行符!)使用 ANTLRWorks v1.3.1。
I see no problem, all four grammars produce the expected AST:
1
2
3
4
with input:
(Note that the
#
in the input is a new line char!)Tested with ANTLRWorks v1.3.1.