antlr 3 歧义
我尝试编写一些简单的规则,但出现了这种歧义
rule: field1 field2; //ambiguity between nsf1 and nsf2 even if I use lookahead k=4
field1: nsf1 | whatever1...;
field2: nsf2 | whatever2...;
nsf1: 'N' 'S' 'F' '1'; //meaning: no such field 1
nsf2: 'N' 'S' 'F' '2'; //meaning: no such field 2
我理解这种歧义,但我不明白为什么前瞻不能解决这个问题。
我有一个简单的解决方案,但我不喜欢它:
规则:(nsf1 (nsf2 |whatever2)) | (whatever1 (nsf2 |whatever2));
有人有更优雅的解决方案吗?
多谢, 克里斯
I try to write some simple rules and I get this ambiguity
rule: field1 field2; //ambiguity between nsf1 and nsf2 even if I use lookahead k=4
field1: nsf1 | whatever1...;
field2: nsf2 | whatever2...;
nsf1: 'N' 'S' 'F' '1'; //meaning: no such field 1
nsf2: 'N' 'S' 'F' '2'; //meaning: no such field 2
I understand the ambiguity, but I don't understand why lookahead doesn't solve this.
I have a simple solution but I don't like it:
rule: (nsf1 (nsf2 | whatever2))
| (whatever1 (nsf2 | whatever2));
Does anybody have a more elegant solution?
Thanks a lot,
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法重现您的问题,但我所能做的就是猜测“whatever1”和“whatever2”的规则是什么。你能发布一个更完整的语法吗?
然而,语法中没有什么是不能完全使用词法分析器标记而不是解析器规则来完成的。尝试将所有规则名称大写,将它们转换为词法分析器标记,看看是否有帮助。
I couldn't reproduce your problem, but all I could do was guess what the rules for 'whatever1' and 'whatever2' were. Can you post a more complete grammar?
However, there's nothing in the grammar that couldn't be done entirely with lexer token rather than parser rules. Try capitalizing all of the rule names to turn them into lexer token and see if that helps.