Antlr 非 LL(*) 决策

发布于 2024-12-13 22:14:42 字数 540 浏览 5 评论 0原文

我在为我的编程语言创建部分 ANTLR 语法时遇到一些问题。

type 声明的第二部分发生时,我收到错误:

public type
    :   ID ('.' ID)* ('?')? -> ^(R__Type ID ID* ('?')?)
    |   '(' type (',' type)*  ')' ('?')? -> ^(R__Type type* ('?')?)
    ;

我尝试匹配:

  • System.String 这样的行(工作正常)
  • 元组例如 (System.String, System.Int32)

错误发生在树的稍上方,并指出:

[fatal] 规则语句由于递归规则而具有非 LL(*) 决策可从 alts 进行调用1,2。通过左因子分解或使用语法谓词或使用 backtrack=true 选项来解决。

我做错了什么?

I'm having some trouble creating part of my ANTLR grammar for my programming language.

I'm getting the error when the second part of the type declaration occurs:

public type
    :   ID ('.' ID)* ('?')? -> ^(R__Type ID ID* ('?')?)
    |   '(' type (',' type)*  ')' ('?')? -> ^(R__Type type* ('?')?)
    ;

I'm trying to either match:

  • A line like System.String (works fine)
  • A tuple such as (System.String, System.Int32)

The error occurs slightly higher up the tree, and states:

[fatal] rule statement has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2. Resolve by left-factoring or using syntactic predicates or using backtrack=true option.

What am I doing wrong?

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

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

发布评论

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

评论(1

风吹过旳痕迹 2024-12-20 22:14:42

是的,我通过编辑处理变量声明的规则,设法在树的早期修复了这个问题:

'my' ID (':' type '=' constant | ':' type | '=' constant) -> ^(R__VarDecl ID type? constant?)

这样它的工作方式如下:

'my' ID
(
   ':' type ('=' constant)?
|   '=' constant
) -> ^(R__VarDecl ID type? constant?)

我从这里的语法谓词示例中得到了这个想法:

https://wincent.com/wiki/ANTLR_predicates

幸运的是我最终不需要谓词!

Right, I managed to fix this a little earlier up the tree, by editing the rule that deals with variable declarations:

'my' ID (':' type '=' constant | ':' type | '=' constant) -> ^(R__VarDecl ID type? constant?)

So that it works like:

'my' ID
(
   ':' type ('=' constant)?
|   '=' constant
) -> ^(R__VarDecl ID type? constant?)

I got the idea from the example of syntactic predicates here:

https://wincent.com/wiki/ANTLR_predicates

Luckily I didn't need a predicate in the end!

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