如何在ANTLR 4中使用自定义标记和上下文 [英] How do I use custom tokens and contexts in ANTLR 4

查看:76
本文介绍了如何在ANTLR 4中使用自定义标记和上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用ANTLR3一段时间了.我只是切换到ANTLR4.通常,对于我的编译器课程的学生来说,它更容易理解.但是,从我所找到的书和其他文档中,如何使标记和上下文构成解析树自定义类的节点尚不清楚.对于ANTLR 3,我只是使用了选项来使生成的代码在生成的代码中重命名它们.在ANTLR 4中该怎么办?我应该找到一些文档吗?

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?

推荐答案

实现 TokenFactory< CustomTokenType> ,其中 CustomTokenType 扩展 CommonToken .在调用解析器之前,在词法分析器(和解析器)上设置TokenFactory.

Implement TokenFactory<CustomTokenType> where CustomTokenType extends CommonToken. Set the TokenFactory on the lexer (and parser as needed) before invoking the parser.

在源代码的"extras"目录中查找"权威的ANTLR 4参考书"的一些简单示例.这些在本书中进行了讨论.

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.

请参见 GenPackage/GenPackageModel ,了解具体的示例,特别是

Look at GenPackage/GenPackageModel for a worked example, specifically the parse method in Converter.java.

Github上还有其他示例-使用'language:antlr'作为搜索词.其他人则根据其实现语言进行分类,因此很难找到它们-'language:java antlr'将找到很多.

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.

这篇关于如何在ANTLR 4中使用自定义标记和上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆