简体   繁体   English

ANTLR4:如何控制类层次结构?

[英]ANTLR4: How to control class hierarchy?

How to configure ANTLR4 to not generate a parent class? 如何配置ANTLR4 生成父类?

Given grammar MyGrammar 给定语法MyGrammar

statement: 'do' | 'check';

ANTLR4 generates ANTLR4生成

class MyGrammar {
    class StatementContext {}
}

Since ANTLR4 generates a parent class for all XXXContext classes, this class name must be used in any usages as well. 由于ANTLR4为所有XXXContext类生成一个父类,因此该类名称也必须在任何用法中使用。 So I must write 所以我必须写

MyGrammar.StatementContext node = ...;

and cannot simply write 不能简单地写

NodeContext node = ...;

This is big enough of a deal, as it will pollute my code with extra characters that don't add any readability. 这足够大,因为它将用多余的字符污染我的代码,这些字符不会增加任何可读性。 As such, it downgrades readability, thus comprehensibility and debuggability, et cetera. 因此,它降低了可读性,从而降低了可理解性和可调试性,等等。

So, how can I make the grammar MyGrammar 所以,我该如何制作语法MyGrammar

statement: 'do' | 'check';

have ANTLR4 generate 让ANTLR4生成

class StatementContext {} // look ma: no parent MyGrammar class!

As simple as that: you cannot change the way classes are generated. 如此简单:您无法更改生成类的方式。 It's a carefully arranged ensemble and changing that would break many things. 这是一个精心安排的合奏,它会改变很多东西。

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

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