Reputation: 55
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?
Upvotes: 2
Views: 1602
Reputation: 6001
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.
Upvotes: 4