I've used ANTLR3 for quite a while. I am just switching to ANTLR 4. It is, in general, much more understandable for my students in my compiler class. However, it's not clear from the book and other documentation that I've located, how to make the tokens and contexts that form the nodes of the parse tree customized classes. With ANTLR 3 I just used the options to have the generated code rename them in the generated code. What about in ANTLR 4?Is there documentation that I shoudl have been able to find?
Asked
Active
Viewed 1,332 times
1 Answers
4
Implement TokenFactory<CustomTokenType>
where CustomTokenType
extends CommonToken
. Set the TokenFactory on the lexer (and parser as needed) before invoking the parser.
Look in the 'extras' directory of the source code to the book "The Definitive ANTLR 4 Reference" for some simple examples. These are discussed in the book.
Look at GenPackage/GenPackageModel for a worked example, specifically the parse method in Converter.java.
There are other examples on Github -- use 'language:antlr' as the search term. Others are classed under their implementing language so are a bit harder to find -- 'language:java antlr' will find many.

GRosenberg
- 5,843
- 2
- 19
- 23
-
Thanks. Much appreciated. I did find the token answer in the book, but not anything about the parser. This is a big help. – Gary Pollice Feb 01 '15 at 17:49