ANTLR 语义谓词

发布于 2024-09-24 13:20:43 字数 307 浏览 1 评论 0原文

我正在尝试在 ANTLR 中使用语义谓词来实现以下语法规则

test[n]
       :({n==0}? => ~('a'))
       |({n==1}? => ~('b'))
       |({n==2}? => ~('c'))
       ;

但是,ANTLR 不允许我以这种方式定义语法,要求至少有一个替代方案是默认的。显示的确切误差是参数 n 的参考误差。

我的目标是我想要一种,并且只有一种选择在任何给定的时刻都可见。关于如何做到这一点有什么想法吗?

谢谢!

I am trying to use semantic predicates in ANTLR for the following grammar rule

test[n]
       :({n==0}? => ~('a'))
       |({n==1}? => ~('b'))
       |({n==2}? => ~('c'))
       ;

However, ANTLR does not let me define the grammar in such a way, requiring that at least one of the alternatives be default. The exact error displayed is a reference error, for the parameter n.

My aim is that I want one, and only one of the alternative to be visible at any given instant of time. Any ideas as to how to go about doing this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

北方的巷 2024-10-01 13:20:43

这是不对的:您忘记指定 n 是什么类型,因此编译生成的解析器将不起作用。它应该是:

test[int n]
  :  ({n==0}? => ~('a'))
  |  ({n==1}? => ~('b'))
  |  ({n==2}? => ~('c'))
  ;

当我使用上面的规则创建一个小语法时,我没有得到任何错误。

您可以发布完整的语法并测试产生此错误的输入字符串吗?您还可以复制并粘贴确切的错误消息吗?

That can't be right: you forgot to specify what type n is, so compiling a generated parser will not work. It should be:

test[int n]
  :  ({n==0}? => ~('a'))
  |  ({n==1}? => ~('b'))
  |  ({n==2}? => ~('c'))
  ;

When I create a small grammar with the rule above, I get no error however.

Can you post a complete grammar and test input string that produces this error? Can you also copy and paste the exact error message?

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