Skip to content

Educational compiler implementation in Python featuring lexer, parser, semantic analyzer, intermediate code generator, and interpreter with a modern web GUI

License

Notifications You must be signed in to change notification settings

NoumanAfzal54/Compiler-in-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸŽ“ Educational Compiler in Python

A fully functional educational compiler demonstrating all five phases of compilation with a beautiful, interactive web interface! πŸš€

Python Flask License


πŸ“– Overview

This project is a complete compiler implementation built from scratch in Python, designed for educational purposes. It takes you through the entire journey of how compilers workβ€”from raw source code to executable outputβ€”with clean, well-documented code and an interactive web GUI.

Perfect for students, educators, and anyone curious about compiler design! 🎯


✨ Features

πŸ”§ 5-Phase Compiler Pipeline

Phase Component Description
1️⃣ Lexical Analysis πŸ”€ Tokenizes source code into meaningful units (keywords, operators, identifiers)
2️⃣ Syntax Analysis 🌳 Builds Abstract Syntax Tree (AST) using recursive descent parsing
3️⃣ Semantic Analysis βœ… Validates variable declarations and usage with symbol table
4️⃣ Code Generation βš™οΈ Generates Three Address Code (TAC) intermediate representation
5️⃣ Execution ▢️ Interprets and executes the TAC instructions

🎨 Interactive Web GUI

  • πŸ’… Modern, gradient-based UI with smooth animations
  • πŸ“Š Real-time visualization of all compilation phases
  • πŸ’» Live code editor with instant feedback
  • πŸ” Step-by-step execution tracking
  • πŸ“ˆ Memory state visualization
  • 🎯 Token breakdown display
  • 🌲 AST tree visualization
  • πŸ“ TAC instruction listing

πŸ› οΈ Tech Stack

  • Backend: 🐍 Python 3.7+
  • Web Framework: 🌢️ Flask
  • Frontend: 🎨 HTML5, CSS3, JavaScript
  • Architecture: πŸ—οΈ Visitor Pattern, Recursive Descent Parser
  • Design Patterns: 🎭 Object-Oriented Programming

πŸ“¦ Project Structure

compiler-in-python/
β”‚
β”œβ”€β”€ πŸ“„ lexer.py              # Lexical analyzer (tokenizer)
β”œβ”€β”€ πŸ“„ ast_parser.py         # Syntax analyzer & AST builder
β”œβ”€β”€ πŸ“„ semantic.py           # Semantic analyzer with symbol table
β”œβ”€β”€ πŸ“„ intermediate.py       # TAC code generator
β”œβ”€β”€ πŸ“„ interpreter.py        # TAC interpreter/executor
β”œβ”€β”€ πŸ“„ compiler_gui.py       # Flask web server
β”œβ”€β”€ πŸ“„ main.py               # CLI interface
β”œβ”€β”€ πŸ“„ test.py               # Test suite
β”‚
β”œβ”€β”€ πŸ“ templates/
β”‚   └── πŸ“„ index.html        # Modern web interface
β”‚
└── πŸ“„ README.md             # You are here! πŸ‘‹

πŸš€ Quick Start

Prerequisites

Make sure you have Python 3.7+ installed on your system.

Installation

1️⃣ Clone the repository

git clone https://github.com/NoumanAfzal54/compiler-in-python.git
cd compiler-in-python

2️⃣ Install dependencies

pip install flask

3️⃣ Run the web interface

python compiler_gui.py

4️⃣ Open your browser

🌐 Navigate to: http://localhost:5000

Alternative: CLI Mode

Run the compiler in command-line mode:

python main.py

Or run the test suite:

python test.py

πŸ’‘ Usage Example

Sample Program

x = 5;
y = x + 10;
z = y * 2;
print(z);

Compilation Output

Phase 1: Tokens πŸ”€

Token(ID, 'x', 0)
Token(ASSIGN, '=', 2)
Token(NUMBER, '5', 4)
Token(SEMICOLON, ';', 5)
...

Phase 2: AST 🌳

Program
β”œβ”€β”€ Assignment(x)
β”‚   └── Number(5)
β”œβ”€β”€ Assignment(y)
β”‚   └── BinaryOp(+)
β”‚       β”œβ”€β”€ Variable(x)
β”‚       └── Number(10)
...

Phase 3: Symbol Table βœ…

Symbol Table:
  x: declared
  y: declared
  z: declared

Phase 4: TAC βš™οΈ

t0 = x + 10
y = t0
t1 = y * 2
z = t1
print z

Phase 5: Execution ▢️

OUTPUT: 30

🎯 Supported Language Features

  • βœ… Integer arithmetic (+, -, *, /)
  • βœ… Variable assignment
  • βœ… Print statements
  • βœ… Parenthesized expressions
  • βœ… Operator precedence
  • βœ… Semantic error detection

πŸ“š Educational Value

This compiler is designed with learning in mind:

  • πŸ“– Comprehensive Comments: Every function and class is thoroughly documented
  • πŸŽ“ Clear Structure: Each phase is in a separate, well-organized file
  • πŸ’­ Conceptual Explanations: Comments explain WHY, not just WHAT
  • πŸ” Real-World Comparisons: References to GCC, LLVM, Java, Python compilers
  • πŸ§ͺ Test Cases: Includes working examples and test programs
  • 🎨 Visual Learning: Web GUI shows each phase in action

Perfect For:

  • πŸŽ“ Computer Science students learning compiler design
  • πŸ‘¨β€πŸ« Educators teaching compiler construction
  • πŸ’» Developers curious about how compilers work
  • πŸš€ Anyone building their own programming language

πŸ—οΈ Architecture

Design Patterns Used

  1. Visitor Pattern 🎭

    • Used in semantic analysis and code generation
    • Clean separation of concerns
    • Easy to extend with new node types
  2. Recursive Descent Parsing πŸ“

    • Simple and intuitive
    • One method per grammar rule
    • Easy to understand and debug
  3. Symbol Table πŸ“‹

    • Tracks variable declarations
    • Enables semantic checking
    • Foundation for type systems

πŸ”¬ How It Works

The Compilation Pipeline

Source Code
    ↓
πŸ“ Lexer β†’ Tokens
    ↓
🌳 Parser β†’ Abstract Syntax Tree (AST)
    ↓
βœ… Semantic Analyzer β†’ Validated AST + Symbol Table
    ↓
βš™οΈ Code Generator β†’ Three Address Code (TAC)
    ↓
▢️ Interpreter β†’ Execution & Output

🎨 Web Interface Preview

The web GUI features:

  • 🌈 Beautiful gradient backgrounds
  • ✨ Smooth animations and transitions
  • πŸ“± Responsive design
  • 🎯 Interactive phase navigation
  • πŸ’‘ Real-time compilation feedback
  • 🎭 Modern typography (Inter, Helvetica)

🀝 Contributing

Contributions are welcome! Feel free to:

  • πŸ› Report bugs
  • πŸ’‘ Suggest new features
  • πŸ”§ Submit pull requests
  • πŸ“– Improve documentation

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ™ Acknowledgments

Inspired by classic compiler design principles from:

  • πŸ“š "Compilers: Principles, Techniques, and Tools" (Dragon Book)
  • πŸ”§ Real-world compilers: GCC, LLVM, Python, Java
  • πŸŽ“ Computer Science education community

πŸ“§ Contact

Nouman Afzal


⭐ Star this repo if you found it helpful!

Made with ❀️ and Python

πŸŽ‰ Happy Compiling! πŸŽ‰

About

Educational compiler implementation in Python featuring lexer, parser, semantic analyzer, intermediate code generator, and interpreter with a modern web GUI

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published