/calculANTLR-python3

Example calculator implemented with ANTLR4 and python

Primary LanguagePythonMIT LicenseMIT

calculANTLR-python3

This repo is a simple calculator implemented with ANTLR4 and Python3.

This project is inspired by empirical-soft/calculANTLR, which is a simple calculator using ANTLR4 and C++.

demo

🚚 step1 install

install antlr4

Follow the instruction on Antlr4.

cd /usr/local/lib
sudo curl -O https://www.antlr.org/download/antlr-4.8-complete.jar

export CLASSPATH=".:/usr/local/lib/antlr-4.8-complete.jar:$CLASSPATH"
alias antlr4='java -jar /usr/local/lib/antlr-4.8-complete.jar'
alias grun='java org.antlr.v4.gui.TestRig'

install python runtime

Details can be found in python-target.

pip3 install antlr4-python3-runtime

🧱 step2 use .g4 to generate parser and lexer

antlr4 -Dlanguage=Python3 Calculantlr.g4 -visitor -o dist 

Use -visitor to generate Visitor Class Use -o to specify output path.

🎉 step3 done

python3 main.py