This mini interpreter is capable of intaking Python syntax and producing a relatively similar response to Python. Utilizing "flex & bison"
authored by John R. Levine, I converted its original implementation to match more of Python syntax rather then a calculator. The major challenge of creating this interpreter is using Flex and Bison in parallel with the abstract syntax tree. No references exist in the implementation without AST.
Installing and configuring Flex and Bison using Docker.
Have Docker pre-installed into your operating system.
- Using CLI Command for Ubuntu
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Below is an example of how you download this project and start using Docker.
- Clone the repo
git clone <repo_url>
- Go into the Project Repository
cd /path/to/repository
- Build Image
sudo docker build -t compiler-image .
- Run Container
sudo docker run -it compiler-image
- Run Test inside Container
$ make $ ./interpreter <source_file>
Test arithmetic, flow control, or trig operations by inputting Python syntax.
$ make
$ ./compiler
$ >>> <insert valid Python syntax>
Remove all generated files.
make clean
Example of arithmetic operations.
$ >>> print(4 + 7)
$ = 11
$ >>> print(20 - 7)
$ = 13
$ >>> print(4 + 1 - 6 * 100 / 70)
$ = -3.571
Example of flow control operations.
$ >>> def addition(a, b): print(a + b)
$ Defined addition
$ >>> addition(1, 2)
$ = 3
$ >>> i = 5
$ >>> b = 6
$ if (i == b): print(i) else: print(b)
$ = 6
$ >>> i = 5
$ while (i > 0): print(i) i = i - 1
$ = 5
$ = 4
$ = 3
$ = 2
$ = 1
Example of trig operations.
$ >>> print(math.sqrt(16))
$ = 4
$ >>> print(math.exp(8))
$ = 2981
$ >>> print(math.log(20))
$ = 2.996
$ >>> print(math.sin(100))
$ = -0.5064
$ >>> print(math.cos(100))
$ = 0.8623
$ >>> print(math.tan(100))
$ = -0.5872