本文主要是介绍ANTLR4,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
配置开发环境
下载antlr4 jar包
https://github.com/antlr/antlr4/blob/master/doc/getting-started.md
#安装jdk11和antlr4.13
# wget https://github.com/antlr/antlr4/archive/refs/tags/4.13.1.tar.gz
# wget https://repo.huaweicloud.com/java/jdk/11.0.2+9/jdk-11.0.2_linux-x64_bin.tar.gz
#设置环境变量
export JAVA_HOME=/data0/huozai/software/installed/jdk-11.0.2/
export PATH=/opt/installed/jdk-11.0.2/bin:$PATH
cur=`pwd`
export CLASSPATH=$cur/antlr-4.13.1-complete.jar:.
#javac -version
#antlr4
#antlr4 Expr.g4
#antlr4-parse Expr.g4 prog -gui
#antlr4-parse Expr.g4 prog -tree
#antlr4-parse Expr.g4 prog -tokens -trace
#1+2*3
#antlr4 Expr.g4
#antlr4 -Dlanguage=Cpp Expr.g4#设置工具
alias antlr4='java -Xmx500M -cp "$cur/antlr-4.13.1-complete.jar:$CLASSPATH" org.antlr.v4.Tool'
alias grun='java -Xmx500M -cp "$cur/antlr-4.13.1-complete.jar:$CLASSPATH" org.antlr.v4.gui.TestRig'# 生成代码
# antlr4 Hello.g4
# antlr4 Hello.g4 -visitor # 编译运行代码
# javac Hello*.java
# grun Hello r -tree# 生成树结构
# yum install ghostscript
# grun Hello r -ps hello_tree.ps
# ps2pdf hello_tree.ps test.pdf 生成树
Hello
// 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
已经写好的grammar,可以直接拿来用
这篇关于ANTLR4的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!