1.安装antlr
下载antlr
curl -O https://www.antlr.org/download/antlr-4.10.1-complete.jar
创建别名
在~/.bashrc文件中添加
alias antlr4='java -jar /usr/local/lib/antlr-4.10.1-complete.jar'
alias grun='java org.antlr.v4.runtime.misc.TestRig'
测试是否成功
antlr4
![使用Antlr生成语法分析器(python)](http://img.readke.com/220815/221325O17-0.jpg)
文章图片
grun
![使用Antlr生成语法分析器(python)](http://img.readke.com/220815/2213252L0-1.jpg)
文章图片
【使用Antlr生成语法分析器(python)】语法分析
(在~/antlrTest/osc2下)
1.通过g4文件生成python形式的词法分析和语法分析器
antlr4 -Dlanguage=Python3 -visitor Hello.g4
2.查看词法树
问题:
运行printTree.py出现问题,找不到antlr4模块
![使用Antlr生成语法分析器(python)](http://img.readke.com/220815/2213254333-2.jpg)
文章图片
解决:
安装antlr运行时
pip3 install antlr4-python3-runtime
sudo pip install antlr4-python3-runtime==4.5
问题:
![使用Antlr生成语法分析器(python)](http://img.readke.com/220815/2213254G3-3.jpg)
文章图片
解决:将运行时版本改为4.7
pip uninstall antlr4-python3-runtime==4.5
pip install antlr4-python3-runtime==4.7