《ANTLR 4权威指南》笔记1 - 初识ANTLR
ANTLR的作用⌗
自动生成词法和语法分析器
下载安装Java和ANTLR⌗
由于ANTLR本身是Java开发的,所以我们需要先安装Java运行时环境才可以运行ANTLR。
Linux系统⌗
首先安装Java 8运行时环境,如果是Ubuntu系统,输入以下命令安装JRE:
sudo apt-get install openjdk-8-jre
如果是CentOS系统,输入以下命令安装JRE:
su -c "yum install java-1.8.0-openjdk"
配置环境变量CLASSPATH
,编辑~/.bashrc
或者~/.zshrc
,添加一行:
export CLASSPATH=".:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib"
**注意:**上面的/usr/lib/jvm/java-8-openjdk-amd64/jre
只是例子,你需要改成自己的JRE安装目录。
接下来安装ANTLR,在ANTLR官网的下载页面下载ANTLR二进制jar文件,目前最新版是ANTLR 4.9,直接下载链接在这里。
配置环境变量,在~/.bashrc
或~/.zshrc
添加以下几行:
export CLASSPATH="$CLASSPATH:/usr/local/lib/antlr-4.9-complete.jar"
alias antlr='java org.antlr.v4.Tool'
alias grun='java org.antlr.v4.gui.TestRig'
**注意:**上面的/usr/local/lib/antlr-4.9-complete.jar
只是例子,你需要改成自己的jar文件路径。
保存后执行source ~/.bashrc
或source ~/.zshrc
。
Windows系统⌗
在Oracle官网下载Java SE 8,并且配置环境变量CLASSPATH
。
接下来安装ANTLR,在ANTLR官网的下载页面下载ANTLR二进制jar文件,目前最新版是ANTLR 4.9,直接下载链接在这里。下载完成后将jar文件的路径添加到CLASSPATH
环境变量中。
创建批处理文件antlr.bat
:
java org.antlr.v4.Tool %*
grun.bat
:
java org.antlr.v4.gui.TestRig %*
其他比较麻烦的办法⌗
Linux下可以新建一个antlr.sh
:
java -cp "/usr/local/lib/antlr4.9-complete.jar:$CLASSPATH" org.antlr.v4.Tool $*
或者用以下alias
:
alias antlr='java -jar /usr/local/lib/antlr-4.9-complete.jar'
Windows下可以创建批处理文件antlr.bat
:
java -cp %CLASSPATH%;C:\libraries\antlr-4.9-complete.jar org.antlr.v4.Tool %*
以及grun.bat
:
java -cp %CLASSPATH%;C:\libraries\antlr-4.9-complete.jar org.antlr.v4.gui.TestRig %*
运行ANTLR⌗
尝试运行ANTLR,输入命令antlr
和grun
,如果输出以下文字,说明安装成功:
$ antlr
ANTLR Parser Generator Version 4.9
-o ___ specify output directory where all output is generated
-lib ___ specify location of grammars, tokens files
-atn generate rule augmented transition network diagrams
...
$ grun
java org.antlr.v4.gui.TestRig GrammarName startRuleName
[-tokens] [-tree] [-gui] [-ps file.ps] [-encoding encodingname]
...
例子⌗
Hello.g4
grammar Hello; // Define a grammar called 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, \r (Windows)
antlr Hello.g4
javac *.java
grun Hello r -tokens
grun Hello r -tree
grun Hello r -gui
ANTLR可以生成多种语言的parser,需要相应的运行时环境库,目前支持以下几种语言:
- Java
- C# (and an alternate C# target)
- Python (2 and 3)
- JavaScript
- Go
- C++
- Swift
- PHP
- DART
antlr
命令行参数:
ANTLR Parser Generator Version 4.8
-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
-Xexact-output-dir all output goes into -o dir regardless of paths/package
grun
(grammar run)工具参数:
java org.antlr.v4.gui.TestRig GrammarName startRuleName
[-tokens] [-tree] [-gui] [-ps file.ps] [-encoding encodingname]
[-trace] [-diagnostics] [-SLL]
[input-filename(s)]
Use startRuleName='tokens' if GrammarName is a lexer grammar.
Omitting input-filename makes rig read from stdin.
grun
参数含义:
-tokens
:打印token stream词法单元流-tree
:打印parse tree解析树,LISP格式-gui
:可视界面-ps
:生成PostScript文件-encoding
:编码-trace
:打印rule name和进入退出rule时的当前token-diagnostics
:输出诊断信息-SLL
:使用更快但是稍弱一些的解析策略