Altering ANTLR’s Code Generation
ANTLR uses two things to generate code: a StringTemplate[54] group file (containing templates) and a Target subclass called LanguageTarget where Language is the grammar language option. The StringTemplate group file is org/antlr/v4/tool/templates/codegen/Language.stg. If you would like to tweak the code generation templates for, say, Java, all you have to do is copy and modify org/antlr/v4/tool/templates/codegen/Java.stg. Then, put it in the CLASSPATH before ANTLR’s jar. ANTLR uses a resource loader to get those templates so it’ll see your modified version first.
The templates just generate code specific to a grammar. Most of the common functionality has been factored out into the runtime library. So, Lexer ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access