暂且简单的记录一下:
ANTLR v3版,包括一个jar文件(包含runtiime和sdk和gunit,而在2.1。7版时,还是分开的4个jar文件)、一个antlr3.jar的ANT任务库文件(也是来自antlr的主页)
eclipse ,分别在ganymede和galileo两个版本的modeling_incubation和SDK上验证通过
Windows 7 / Windows XP+java6u16和Ubuntu 9.10+sun-java6 上分别验证通过
环境:
antlr-for-eclipse,即antlride-2.0-rc4版,在eclipse-modeling软件包中使用还需要dltk包支持ANTLR v3版,包括一个jar文件(包含runtiime和sdk和gunit,而在2.1。7版时,还是分开的4个jar文件)、一个antlr3.jar的ANT任务库文件(也是来自antlr的主页)
eclipse ,分别在ganymede和galileo两个版本的modeling_incubation和SDK上验证通过
Windows 7 / Windows XP+java6u16和Ubuntu 9.10+sun-java6 上分别验证通过
目标1:在elicpse平台上使用ant构建ANTLR
- 以Windows 版为例:(Ubuntu版在设置ANTLR_HOME时,可以通过输入ANTLR_HOME=xxx ./eclipse方式启动,不需要修改系统环境变量)
- 在系统属性中添加ANTLR_HOME环境变量,如antlr-3.2.jar位于F:workspaceESAlib,则设为:F:workspaceESA
- 将antlr3.jar这个ant任务文件复制到eclipse的ant库下,如:F:modelingpluginsorg.apache.ant_1.7.1.v20090120-1145lib
- 在eclipse的ANTruntime配置中,单击ANT_HOME,重新定位之,使ANT重新扫描,发现新加入的任务antlr3并添加之。这时候会提示错误,再加入JDK6的tools.jar.
- 编写build.xml,形如:
xml version="1.0" ?>
- < project name =" c " default =" generate " basedir =" . " >
< property name =" version " value =" 1.00 " />
< property name =" src " location =" src " />
< property name =" bin " location =" bin " />
< property name =" lib " location =" lib " />
< property name =" build " location =" build " />
< property name =" dist " location =" dist " />
< property name =" doc " location =" doc " />
< property name =" grammar " location =" grammar " />
- < target name =" init " >
-
< tstamp />
< mkdir dir =" ${bin}/META-INF " />
-
- < fail message =" Environment variable ANTLR_HOME is not set! " >
- < condition >
- < not >
< isset property =" envList.ANTLR_HOME " />
not >
condition >
fail >
target >
-
-
-
< property environment =" envList " />
-
< property name =" antlrHome " value =" ${envList.ANTLR_HOME} " />
- < patternset id =" antlr.libs " >
< include name =" antlr-*.jar " />
< include name =" stringtemplate-*.jar " />
< include name =" runtime-*.jar " />
patternset >
- < path id =" antlr.path " >
- < fileset dir =" ${antlrHome}/lib " casesensitive =" yes " >
< patternset refid =" antlr.libs " />
fileset >
path >
-
< property name =" report " value =" true " />
< property name =" multithreaded " value =" true " />
< property name =" debug " value =" false " />
-
- < macrodef name =" antlr3 " >
< attribute name =" grammar.name " />
< attribute name =" package.dir " />
- < sequential >
< echo message =" antlr ${grammar}/@{grammar.name} " />
- < antlr:antlr3 xmlns:antlr =" antlib:org/apache/tools/ant/antlr " target =" ${grammar}/@{grammar.name} " outputdirectory =" ${src}/@{package.dir} " libdirectory =" ${src}/@{package.dir} " multithreaded =" ${multithreaded} " report =" ${report} " debug =" ${debug} " >
- < classpath >
< path refid =" antlr.path " />
classpath >
< jvmarg value =" -Xmx512M " />
antlr:antlr3 >
sequential >
macrodef >
-
-
- < target name =" generate " depends =" init " description =" using ANTLR to generated *.java " >
< antlr3 package.dir =" per/chenjw/esa/front/c " grammar.name =" C.g " />
target >
- < target name =" compile " depends =" init, generate " description =" compile *.java generated by ANTLR. " >
- < javac debug =" true " srcdir =" ${src} " destdir =" ${bin} " listfiles =" Yes " deprecation =" Yes " >
< compilerarg value =" -Xlint:unchecked -nwarn " />
- < classpath >
< path refid =" antlr.path " />
classpath >
javac >
target >
-
- < target name =" clean " description =" Removes all generated and disttribution files. " >
< delete dir =" ${bin} " />
- < delete >
- < fileset dir =" ${dist} " >
< include name =" *.jar " />
< exclude name =" *.txt " />
fileset >
delete >
target >
project >
- Alt+X+Q运行之。
FAQ - 常见错误如报错 ant任务不识别,这主要是没有重新扫描ant的lib目录造成的。
PS - TODO - antlr3通过gunit支持更加便捷的测试途径,更详细的ant脚本在这@gmail.com。
PS - TODO - antlr3通过gunit支持更加便捷的测试途径,更详细的ant脚本在这@gmail.com。
目标2:在elicpse平台上使用antlride编写ANTLR语法
- 将antlride和ltdk两个的插件包分别放置到eclipse-modeling平台的dropins里面
- 启动eclipse后修改ANTLR的配置,指定ANTLR runtime环境,如F:workspaceESA,并制定antlr的输出文件路径。需要注意的是,这里的路径要跟.g文件中header段指定的包名一致。
- 在项目如ESA右键中添加ANTLR IDE support,这时候在buiild project操作时,就会包含.g文件的编译。
Suggestion - 为了使antlride和ant方式的输出保持一致,不得不将所有的语法文件生成的包放置到同一个路径下,因此,在.g语法文件中声明的package也必须统一。至于tokens,则不是必须。
进阶:利用ANTLR编写完整的ANSI C解析器
调查
网上有人提供的ANTLR自学经验、资料 ,包含一个tiny-C的语法文件。不过,里面的内容确实很老了,但是一些“老人谈”还是成立的:利用ANTLR编写一个实用的解析器并不比利用Yacc/Lex编写容易太多,作者本人就因为调试语法不成放弃了 。正如下图所示,编译器技术本来就是一件“屠龙技”。
当然,辅助编写语法的东西还是有的,比如上面网址提供了一个 AST树美化输出程序()
eclipse 上的 JDT中关于Java 解析的原理、实现
Flick: The Flexible IDL Compiler Kit
utah大学提供的IDL 编译器助手库:
Thoughts on the Visual C++ Abstract Syntax Tree (AST)
Parsing C++
最后,如果看完上面的内容都没有什么收获的话,那么最值得一看就是这篇,作者详细说明了他试图开发一个refactoring C++工具的前因后果,很有深度和概括性。
文中介绍了很多启发性的东西,比如元解析----不是直接打造一个完整的解析器,而是迭代迭构造,思路来自于一篇博士论文。而且这样可以构造出一个比原始C++更大的集合,这可以用来是实现 如嵌入式领域C++语言的扩展。文章提到一个 Edison Design Group公司 ,主页上说的话非常贴近心声,强烈推荐。