ANTLR:解决根语法的静态初始值设定项中代码太大的问题
编辑:为了明确这一点(链接的问题没有明确说明):据我了解,代码太大
错误是生成的解析器代码上的编译器错误通常是由于语法太大以致某些代码大于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
fragment
关键字只能在词法分析器规则之前使用,不能在解析器规则之前使用,就像我看到的那样。首先在所有语法中更改它(我只查看了ObjectExpressions.g
)。不幸的是,当您尝试时 ANTLR 不会产生错误。但请相信我:这是错误的,并且可能会导致您的(部分)问题。另外,第 34-36 行的规则:
应重写为:
编辑
不,不幸的是没有这样的选择。你必须将语法分成(甚至更多)更小的语法。
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 atObjectExpressions.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:
should be rewritten as:
EDIT
No, there is no such option unfortunately. You'll have to divide the grammar into (even more) smaller ones.