Building a Translator with a Listener

Imagine your boss assigns you to build a tool that generates a Java interface file from the methods in a Java class definition. Panic ensues if you’re a junior programmer. As an experienced Java developer, you might suggest using the Java reflection API or the javap tool to extract method signatures. If your Java tool building kung fu is very strong, you might even try using a bytecode library such as ASM.[19] Then your boss says, “Oh, yeah. Preserve whitespace and comments within the bounds of the method signature.” There’s no way around it now. We have to parse Java source code. For example, we’d like to read in Java code like this:

​ ​import​ java.util.List;
​ ​import ...

Get The Definitive ANTLR 4 Reference, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.