antlr 3 歧义

发布于 2024-09-02 01:26:43 字数 503 浏览 4 评论 0原文

我尝试编写一些简单的规则,但出现了这种歧义

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
  1. 我理解这种歧义,但我不明白为什么前瞻不能解决这个问题。

  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
  1. I understand the ambiguity, but I don't understand why lookahead doesn't solve this.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦巷 2024-09-09 01:26:43

我无法重现您的问题,但我所能做的就是猜测“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文