EyasWannous / python-compiler-GNU-flex-bison

python compiler written in C, cpp ( c++ ), lex and yacc with the assistance of GNU flex & bison

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Python Compiler

A Python compiler written in C and C++ with the assistance of Lex and Yacc, utilizing Flex, Bison, Win_Flex, and Win_Bison. The project aims to replicate Python's syntax, providing error checking and generating an Abstract Syntax Tree (AST) visualization. GNU's flex bison.

Overview

The Python compiler supports a wide range of Python syntax, including defining functions, classes, expressions, assignment statements, if conditions, loops (for and while), and various other language structures. It handles features like inheritance, data types, arrays (lists), decorations, and more. The compiler goes through three phases: scanning the code, parsing for validation, and generating an AST for code visualization.

Target Audience

This project is open to any developer interested in exploring or contributing to a Python compiler implementation.

Technologies and Dependencies

  • C and C++ programming languages
  • Lex and Yacc (Flex, Bison, Win_Flex, Win_Bison)
  • Graphviz for AST visualization

Prerequisites

Before running the project, ensure you have the following installed:

Installation

  1. Clone the repository:

    git clone https://github.com/EyasWannous/python-compiler-GNU-flex-bison.git
  2. Navigate to the project directory:

    cd python-compiler-GNU-flex-bison
  3. Run the following commands:

    Flex scanner.l
    bison -d parser.y
    bison -v parser.y
    gcc parser.tab.c lex.yy.c
    win_flex scanner.l -o scanner.cpp
    win_bison -d parser.y -o parser.cpp
    g++ ast.cpp scanner.cpp parser.cpp
  4. Execute the compiler with a test file:

    a.exe < Tests/(testFile.py) > p1.gv

    Replace "(testFile.py)" with the desired test file from the "Tests" directory.

  5. Generate AST visualization:

    dot -Tpng -op1.png p1.gv

Example

To run the compiler on a sample Python file and visualize the AST:

a.exe < Tests/p1.py > p1.gv
dot -Tpng -op1.png p1.gv

Contribution

Feel free to clone the repository, experiment with the code, and contribute by sending pull requests. Your contributions are welcome!


Let me know if you have any additional information or changes you'd like to make!

Authors

About

python compiler written in C, cpp ( c++ ), lex and yacc with the assistance of GNU flex & bison

License:MIT License


Languages

Language:C++ 48.4%Language:C 40.6%Language:Yacc 7.3%Language:Lex 3.2%Language:Python 0.5%