简体   繁体   中英

ANTLR4: How to control class hierarchy?

How to configure ANTLR4 to not generate a parent class?

Given grammar MyGrammar

statement: 'do' | 'check';

ANTLR4 generates

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. 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

statement: 'do' | 'check';

have ANTLR4 generate

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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