ANTLR:解决根语法的静态初始值设定项中代码太大的问题

发布于 2024-12-13 13:40:08 字数 1176 浏览 0 评论 0原文

在寻找问题的解决方案时,我得到了这个问题< /a>,建议使用复合语法来消除代码太大。问题在那里,我已经在使用语法导入,但是当我进一步扩展导入的语法之一时,根解析器语法显示错误。显然,问题在于ANTLR在分析整个语法后生成的许多标记和DFA定义。有没有办法/建议的方法来摆脱这个问题?它是否可扩展,即它是否不依赖于解决方法所改变的部分足够小?

编辑:为了明确这一点(链接的问题没有明确说明):据我了解,代码太大错误是生成的解析器代码上的编译器错误通常是由于语法太大以致某些代码大于 java 规范的限制而引起的。就我而言,它是根解析器类的静态初始化程序,其中包含大量 DFA 前瞻变量,所有这些都会在初始化程序中生成代码。因此,理想情况下,ANTLR 应该能够在语法太大/用户告诉 ANTLR 这样做的情况下将其拆分。有这样的选择吗?

(我必须承认,链接问题的提问者有一个......有趣的规则,导致他的语法膨胀,这可能也是我的错误。但是这种可能性是不是语法作者的错误(在任何大型语法中)成立,所以我认为这是一个有效的、非语法特定的 ANTLR 问题)

编辑结束

我的语法解析“Magic the Gathering”规则文本并且可用<一href="http://code.google.com/p/laterna-magica/source/browse/?repo=oracle-parser#git%2Fsrc%2Fmain%2Fantlr3" rel="nofollow noreferrer">此处 (吉特)。当将 此文件。我使用 Maven 和 antlr3-maven-plugin 进行构建,所以理想情况下,解决方法是可以使用该插件的,但如果不是,那问题比我现在遇到的问题要小......

非常感谢,我希望我没有'没有监督任何对我有帮助的明显文件。

Searching for solutions for my problem, I got this question, suggesting composite grammars to get rid of code too large. Problem there, I'm already using grammar imports, but when I further extend one of the imported grammars, the root parser grammar shows the error. Apparently, the problem lies in the many tokens and DFA definitions that ANTLR generates after analyzing the whole grammar. Is there a way/what is the suggested way to get rid of this problem? Is it scalable, i.e. does it not depend on the parts changed by the workaround being small enough?

EDIT: To make this clear (the linked question didn't make it clear): The code too large error is a compiler error on the generated parser code, to my understanding usually caused by a grammar so large that some code is larger than the limit of the java specification. In my case, it's the static initializer of the root parser class, which contains tons of DFA lookahead variables, all resulting in code in the initializer. So, Ideally, ANTLR should be able to split that up in the case that the grammar is too big/the user tells ANTLR to do it. Is there such an option?

(I have to admit, the asker of the linked question had an... interesting rule that caused his grammar to bloat up, and it may be my error here, too. But the possibility of this being not the grammar's author's error (in any large grammar) stands, so I see this as a valid, non-grammar specific ANTLR question)

EDIT END

My grammar parses "Magic the Gathering" rules text and is available here (git). The problem specifically appears when exchanging line 33 for 34-36 in this file. I use Maven and antlr3-maven-plugin for building, so ideally, the workaround is doable using the plugin, but if it's not, that's a smaller problem than the one I have now...

Thanks a lot and I hope I haven't overseen any obvious documentation that would help me.

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

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

发布评论

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

评论(1

那伤。 2024-12-20 13:40:08

fragment 关键字只能在词法分析器规则之前使用,不能在解析器规则之前使用,就像我看到的那样。首先在所有语法中更改它(我只查看了ObjectExpressions.g)。不幸的是,当您尝试时 ANTLR 不会产生错误。但请相信我:这是错误的,并且可能会导致您的(部分)问题。

另外,第 34-36 行的规则:

qualities
  :  qualities0 
  |  qualities0 (COMMA qualities0)+ -> qualities0+ 
  |  qualities0 (Or qualities0)+    -> ^(Or qualities0+)
  ;

应重写为:

qualities
  :  qualities0 (COMMA qualities0)* -> qualities0+ 
  |  qualities0 (Or qualities0)+    -> ^(Or qualities0+)
  ;

编辑

所以,理想情况下,ANTLR 应该能够在语法太大/用户告诉 ANTLR 这样做的情况下将其拆分。有这样的选择吗?

不,不幸的是没有这样的选择。你必须将语法分成(甚至更多)更小的语法。

The fragment keyword can only be used before lexer rules, not before parser rules as I see you do. First change that in all your grammars (I only looked at ObjectExpressions.g). It's unfortunate that ANTLR does not produce an error when you try it. But believe me: it's wrong, and might be causing (a part of) your problem(s).

Also, your rule from line 34-36:

qualities
  :  qualities0 
  |  qualities0 (COMMA qualities0)+ -> qualities0+ 
  |  qualities0 (Or qualities0)+    -> ^(Or qualities0+)
  ;

should be rewritten as:

qualities
  :  qualities0 (COMMA qualities0)* -> qualities0+ 
  |  qualities0 (Or qualities0)+    -> ^(Or qualities0+)
  ;

EDIT

So, Ideally, ANTLR should be able to split that up in the case that the grammar is too big/the user tells ANTLR to do it. Is there such an option?

No, there is no such option unfortunately. You'll have to divide the grammar into (even more) smaller ones.

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