How do I use custom tokens and contexts in ANTLR 4

末鹿安然 提交于 2019-12-11 13:15:27

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/28256322/how-do-i-use-custom-tokens-and-contexts-in-antlr-4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!