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