简体   繁体   English

ANTLR:解决根语法静态初始化程序中太大的代码

[英]ANTLR: Resolving code too large in a root grammar's static initializer

Searching for solutions for my problem, I got this question , suggesting composite grammars to get rid of code too large . 在为我的问题寻找解决方案时,我遇到了这个问题 ,建议使用复合语法来消除code too largecode 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. 显然,问题在于ANTLR在分析整个语法后生成了许多标记和DFA定义。 Is there a way/what is the suggested way to get rid of this problem? 有没有一种方法/建议的方法可以解决此问题? Is it scalable, ie 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. 编辑:为了使这一点清楚(链接的问题没有使之清楚): code too large错误是生成的解析器代码上的编译器错误,据我所知,通常是由于语法过大而导致某些代码大于Java规范的限制。 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. 在我的情况下,它是根解析器类的静态初始化程序,其中包含大量的DFA超前变量,所有这些变量都会在初始化程序中产生代码。 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. 因此,理想情况下,在语法太大/用户告诉ANTLR的情况下,ANTLR应该能够将其分解。 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) (我必须承认,链接问题的提问者有一个...有趣的规则,导致他的语法膨胀,这也可能是我的错误。但是,这可能不是语法作者的错误(在任何大的语法),所以我认为这是一个有效的,非语法特定的ANTLR问题)

EDIT END 编辑结束

My grammar parses "Magic the Gathering" rules text and is available here (git). 我的语法解析“魔术聚会”规则文本,并且在此处 (git)。 The problem specifically appears when exchanging line 33 for 34-36 in this file . 当在该文件中将第33行交换为34-36时,特别会出现此问题。 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... 我使用Maven和antlr3-maven-plugin进行构建,因此理想情况下,可以使用该插件解决该问题,但是,如果不是这样,那这比我现在遇到的问题要小...

Thanks a lot and I hope I haven't overseen any obvious documentation that would help me. 非常感谢,我希望我没有监督任何对我有帮助的明显文档。

The fragment keyword can only be used before lexer rules, not before parser rules as I see you do. fragment关键字只能在词法分析器规则之前使用, 而不能在解析器规则之前使用,就像我看到的那样。 First change that in all your grammars (I only looked at ObjectExpressions.g ). 首先在所有语法中进行更改(我只查看了ObjectExpressions.g )。 It's unfortunate that ANTLR does not produce an error when you try it. 不幸的是,尝试使用ANTLR不会产生错误。 But believe me: it's wrong, and might be causing (a part of) your problem(s). 但请相信我:这是错误的,并且可能会导致您的问题(部分)。

Also, your rule from line 34-36: 另外,您从第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. 因此,理想情况下,在语法太大/用户告诉ANTLR的情况下,ANTLR应该能够将其分解。 Is there such an option? 有这样的选择吗?

No, there is no such option unfortunately. 不,不幸的是没有这样的选择。 You'll have to divide the grammar into (even more) smaller ones. 您必须将语法划分为(甚至更多)较小的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM