为什么这个语法是错误208?
我不明白为什么以下语法会导致错误 208 抱怨 IF 将永远不会匹配:
error(208): test.g:11:1: The following token definitions can never be matched because prior tokens match the same input: IF
ANTLRWorks 1.4.3
ANTLT 3.4
grammar test;
@lexer::members {
private boolean rawAhead() {
}
}
parse : IF*;
RAW : ({rawAhead()}?=> . )+;
IF : 'if';
ID : ('A'..'Z'|'a'..'z')+;
删除 RAW 规则或 ID 规则可以解决该错误... 从我的角度来看,当 rawAhead() 返回 false 时,IF 确实有可能被匹配。
I don't understand why the following grammar leads to error 208 complaining IF will be never matched:
error(208): test.g:11:1: The following token definitions can never be matched because prior tokens match the same input: IF
ANTLRWorks 1.4.3
ANTLT 3.4
grammar test;
@lexer::members {
private boolean rawAhead() {
}
}
parse : IF*;
RAW : ({rawAhead()}?=> . )+;
IF : 'if';
ID : ('A'..'Z'|'a'..'z')+;
Either remove RAW rule or ID rule solves the error...
From my point of view, IF does have the possibility to be matched when rawAhead() returns false.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,你说得对,好点。再考虑一下,这是预期的行为 AFAIK。但是,事情的工作方式似乎有点不同:
RAW
规则优先于ID
和IF
规则,即使放在末尾时也是如此。如您所见,词法分析器语法:freemarker_simple.g
Main.java
会将以下内容打印到控制台:
如您所见,
'if'
和'foo'
< em> 被标记为输入中的RAW
:Yeah, you're right, good point. Giving it some more thought that is the expected behavior AFAIK. But, it seems things work a bit differently: the
RAW
rule gets precedence over theID
andIF
rules, even when placed at the end of the lexer grammar as you can see:freemarker_simple.g
Main.java
will print the following to the console:
As you can see, the
'if'
and'foo'
are tokenized asRAW
in the input: