需要搭建ANTLR v4的开发环境。
所以去参考:
去一步步操作。
1.去下载
下载到
ide:2013-01-16-antlrworks-2.0-b1.zip
2.把antlr-4.0-complete.jar的路径添加到classpath中去:
CLASSPATH=.;%JAVA_HOME%\lib\rt.jar;D:\DevTool\DD_Parser\Parser\ANTLR\v4\antlr-4.0-complete.jar; |
3.解压2013-01-16-antlrworks-2.0-b1.zip,找到可以运行的程序:
2013-01-16-antlrworks-2.0-b1\antlrworks2\bin\antlrworks264.exe
以待后用。
4.找个路径:
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\batchFile
去创建antlr4.bat和run.bat,内容分别为:
antlr4.bat:
java org.antlr.v4.Tool %*
run.bat:
java org.antlr.v4.runtime.misc.TestRig %*
5.再去把上述路径
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\batchFile
添加到对应的PATH环境变量中去。
6.再去windows的cmd中,验证一下是否正常找到antlr工具:
C:\Users\CLi>java org.antlr.v4.Tool ANTLR Parser Generator Version 4.0 -o ___ specify output directory where all output is generated -lib ___ specify location of grammars, tokens files -atn generate rule augmented transition network diagrams -encoding ___ specify grammar file encoding; e.g., euc-jp -message-format ___ specify output style for messages in antlr, gnu, vs2005 -long-messages show exception details when available for errors and warnings -listener generate parse tree listener (default) -no-listener don’t generate parse tree listener -visitor generate parse tree visitor -no-visitor don’t generate parse tree visitor (default) -package ___ specify a package/namespace for the generated code -depend generate file dependencies -D<option>=value set/override a grammar-level option -Werror treat warnings as errors -XdbgST launch StringTemplate visualizer on generated code -XdbgSTWait wait for STViz to close before continuing -Xforce-atn use the ATN simulator for all predictions -Xlog dump lots of logging info to antlr-timestamp.log |
7.找个文件夹:
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\demoHello |
在其下,创建对应的Hello.g4文件,内容为:
// Define a grammar called Hello grammar Hello; r : 'hello' ID ; // match keyword hello followed by an identifier ID : [a-z]+ ; // match lower-case identifiers WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
8.打开cmd,切换到上述目录,然后去运行:
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\demoHello>antlr4 Hello.g4 D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\demoHello>java org.antlr.v4.Tool Hello.g4
9.如果上述出错:
‘javac’ is not recognized as an internal or external command
那么可以参考:
【已解决】’javac’ is not recognized as an internal or external command,operable program or batch file.
10.然后运行:
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\demoHello>antlr4 Hello.g4 D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\demoHello>java org.antlr.v4.Tool Hello.g4
生成了很多java和tokens文件:
Hello.tokens HelloBaseListener.java HelloLexer.java HelloLexer.tokens HelloListener.java HelloParser.java |
11.接着再去编译:
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\demoHello>javac Hello*.java
会把上述的.java编译为.class文件。
12.接着运行grun结果出错:
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\demoHello>grun Hello r -tree ‘grun’ is not recognized as an internal or external command, operable program or batch file. |
13.后来经过一番折腾后,发现其实是教程中的笔误。
所以去把之前的:
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\batchFile\run.bat
改为:
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\batchFile\grun.bat
即可。
再去运行,就可以运行了。
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\demoHello>grun Hello r -tree D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\demoHello>java org.antlr.v4.runtime.misc.TestRig Hello r -tree |
但是,结果是无任何输出。
14.详细折腾过程,参见:
【未解决】antlr v4中运行grun Hello r -tree,结果无输出
15.结果是没有解决,找不到原因。
所以无奈之下,只能另外去使用antlrworks去看看同样的代码,运行的效果如何了:
【记录】尝试使用ANTLRWorks 2.0去进行ANTLR开发
【后记 2013-07-05】
1.后来折腾:
后。
然后顺带看到了:
中的:
hen run ANTLR the tool on it: $ cd /tmp $ antlr4 Hello.g4 $ javac Hello*.java Now test it: $ grun Hello r -tree hello parrt ^D (r hello parrt) (That ^D means EOF on unix; it's ^Z in Windows.) The -tree option prints the parse tree in LISP notation. BOOK SOURCE CODE http://pragprog.com/titles/tpantlr2/source_code |
才知道,需要额外输入内容测试的。
之前其实就知道要输入测试内容的,但是没有看到哪里有介绍,不知道如何输入而已。
2.现在就去测试一把,结果是:
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DemoHello>antlr4 Hello.g4 D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DemoHello>java org.antlr.v4.Tool Hello.g4 D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DemoHello>javac Hello*.java D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DemoHello>grun Hello r -tree D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DemoHello>java org.antlr.v4.runtime.misc.TestRig Hello r -tree hello crifan ^Z (r hello crifan) D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DemoHello>
其中:
1.在:
java org.antlr.v4.runtime.misc.TestRig Hello r -tree |
之后,其命令行此时就在等待你的输入
2.然后输入:
hello crifan |
3.之后再回车一次。
4.然后才是按照其提示,去在Windows(win7的cmd)中按:
Ctrl+Z
对应的显示出的就是那个:
^Z
5.然后程序输出的结果就是那个:
(r hello crifan) |
即,我们所要的抽象语法树AST的表达形式。
【总结】
总体上说,antlr,本身功能算不错的。
但是就是稳定方面,还是不够好,不够全面,解释的很不够。
转载请注明:在路上 » 【记录】搭建ANTLR v4开发环境