A fully functional educational compiler demonstrating all five phases of compilation with a beautiful, interactive web interface! π
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! π―
| 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 |
- π 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
- Backend: π Python 3.7+
- Web Framework: πΆοΈ Flask
- Frontend: π¨ HTML5, CSS3, JavaScript
- Architecture: ποΈ Visitor Pattern, Recursive Descent Parser
- Design Patterns: π Object-Oriented Programming
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! π
Make sure you have Python 3.7+ installed on your system.
1οΈβ£ Clone the repository
git clone https://github.com/NoumanAfzal54/compiler-in-python.git
cd compiler-in-python2οΈβ£ Install dependencies
pip install flask3οΈβ£ Run the web interface
python compiler_gui.py4οΈβ£ Open your browser
π Navigate to: http://localhost:5000
Run the compiler in command-line mode:
python main.pyOr run the test suite:
python test.pyx = 5;
y = x + 10;
z = y * 2;
print(z);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
- β
Integer arithmetic (
+,-,*,/) - β Variable assignment
- β Print statements
- β Parenthesized expressions
- β Operator precedence
- β Semantic error detection
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
- π Computer Science students learning compiler design
- π¨βπ« Educators teaching compiler construction
- π» Developers curious about how compilers work
- π Anyone building their own programming language
-
Visitor Pattern π
- Used in semantic analysis and code generation
- Clean separation of concerns
- Easy to extend with new node types
-
Recursive Descent Parsing π
- Simple and intuitive
- One method per grammar rule
- Easy to understand and debug
-
Symbol Table π
- Tracks variable declarations
- Enables semantic checking
- Foundation for type systems
Source Code
β
π Lexer β Tokens
β
π³ Parser β Abstract Syntax Tree (AST)
β
β
Semantic Analyzer β Validated AST + Symbol Table
β
βοΈ Code Generator β Three Address Code (TAC)
β
βΆοΈ Interpreter β Execution & Output
The web GUI features:
- π Beautiful gradient backgrounds
- β¨ Smooth animations and transitions
- π± Responsive design
- π― Interactive phase navigation
- π‘ Real-time compilation feedback
- π Modern typography (Inter, Helvetica)
Contributions are welcome! Feel free to:
- π Report bugs
- π‘ Suggest new features
- π§ Submit pull requests
- π Improve documentation
This project is licensed under the MIT License - see the LICENSE file for details.
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
Nouman Afzal
- π GitHub: @NoumanAfzal54
- π§ Email: noumanafzal05@gmail.com
- πΌ LinkedIn: Nouman Afzal