Skip to content

yycorcino/flex-bison-python-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Python

Mini Python Interpreter

A intricate Python interpreter using CLI to process Python syntax.

About The Project

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.

Getting Started

Installing and configuring Flex and Bison using Docker.

Prerequisites

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
    

Installation

Below is an example of how you download this project and start using Docker.

  1. Clone the repo
    git clone <repo_url>
    
  2. Go into the Project Repository
    cd /path/to/repository
    
  3. Build Image
    sudo docker build -t compiler-image .
    
  4. Run Container
    sudo docker run -it compiler-image
    
  5. Run Test inside Container
    $ make
    $ ./interpreter <source_file>
    

Usage

Test arithmetic, flow control, or trig operations by inputting Python syntax.

$ make
$ ./compiler
$ >>> <insert valid Python syntax>

Remove all generated files.

make clean

Usage Example

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

About

Intricate Python Interpreter: Arithmetic, Flow-Control, Trigonometric

Topics

Resources

Stars

Watchers

Forks