A complete, multi-phase compiler for MiniLang (a custom, strongly-typed programming language), built from scratch in Python. This project demonstrates a deep understanding of compiler design principles, including lexical analysis, LALR syntax parsing, strict semantic validation, and intermediate code generation.
- Dual Lexical Analyzers: Features both a manually implemented Regex/DFA-based scanner and an automated lexer using PLY.
- LALR Parsing: Robust syntax analysis handling operator precedence, associativity, and syntax error recovery.
- Strict Semantic Analysis:
- Real-time Symbol Table management.
- Strict type-checking (
int,bool,string) for assignments and arithmetic/logical expressions. - Prevention of undeclared variables and redeclarations.
- Three-Address Code (TAC) Generation: Translates high-level constructs into optimized intermediate machine code using temporary variables (
t_n) and control-flow labels (L_n). - Advanced Control Structures: Supports
if/else,while, and complexforloops. - 💡 Technical Highlight: Implemented one-pass code reordering for
forloops (extracting and repositioning the update statement to the end of the loop body) without relying on a full AST structure.
The compiler processes source code through four distinct phases:
- Phase 1: Manual Lexer * A handcrafted token scanner utilizing finite automata (NFA/DFA) concepts.
- Phase 2: Automated Lexer (PLY)
- A robust tokenizer supporting escape sequences (
\n,\t) and comprehensive error handling.
- A robust tokenizer supporting escape sequences (
- Phase 3: Parser & Semantic Analyzer
- Uses PLY's
yaccimplementation to validate grammar rules while simultaneously performing semantic actions and type validations.
- Uses PLY's
- Phase 4: Code Generator
- Translates validated syntax directly into linear Three-Address Code (TAC), managing conditional jumps (
ifFalse,goto) and memory operations.
- Translates validated syntax directly into linear Three-Address Code (TAC), managing conditional jumps (
📦 Minilang_compiler
┣ 📂 phase1_manual_lexer # Handcrafted DFA-based scanner
┣ 📂 phase2_auto_lexer # PLY-based automated tokenizer
┣ 📂 phase3_parser # Grammar rules, Symbol Table, and Semantic checks
┣ 📂 phase4_codegen # Three-Address Code (TAC) generator engine
┣ 📂 phase5_tests # Comprehensive test suites for all phases
┗ 📜 main.py # Interactive CLI menu for running tests
MiniLang is a procedural language designed for this compiler. It supports:
- Data Types:
int,bool,string - Operators: Arithmetic (
+,-,*,/), Relational (<,>,<=,>=,==,!=), Logical (&&,||,!) - I/O:
print(),input()
MiniLang Input:
int i;
for (i = 0; i <= 3; i = i + 1) {
print(i);
}Compiler Output (Three-Address Code):
i = 0
L1:
t1 = i <= 3
ifFalse t1 goto L2
print i
t2 = i + 1
i = t2
goto L1
L2:
This project uses uv, an extremely fast Python package installer and resolver, for dependency management.
-
Clone the repository:
git clone [https://github.com/your-username/minilang-compiler.git](https://github.com/your-username/minilang-compiler.git) cd minilang-compiler -
Install dependencies: Since the project includes a
pyproject.toml, you can quickly sync the environment and install dependencies (likeply) usinguv:uv sync
-
Run the Interactive Test Suite: Use
uv runto execute the main menu script within the isolated environment:uv run main.py
Select the desired phase from the menu to see token generation, semantic validations, or TAC outputs.
Hossein Rahmati Developer | Compiler Design Enthusiast LinkedIn | Email
This project was developed as a final academic project for the Compiler Design course.