Skip to content

Repository files navigation

🚀 MiniLang Compiler

Python Version PLY Build

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.

🌟 Key Features

  • 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 complex for loops.
  • 💡 Technical Highlight: Implemented one-pass code reordering for for loops (extracting and repositioning the update statement to the end of the loop body) without relying on a full AST structure.

🛠️ Architecture & Compilation Pipeline

The compiler processes source code through four distinct phases:

  1. Phase 1: Manual Lexer * A handcrafted token scanner utilizing finite automata (NFA/DFA) concepts.
  2. Phase 2: Automated Lexer (PLY)
    • A robust tokenizer supporting escape sequences (\n, \t) and comprehensive error handling.
  3. Phase 3: Parser & Semantic Analyzer
    • Uses PLY's yacc implementation to validate grammar rules while simultaneously performing semantic actions and type validations.
  4. Phase 4: Code Generator
    • Translates validated syntax directly into linear Three-Address Code (TAC), managing conditional jumps (ifFalse, goto) and memory operations.

📂 Repository Structure

📦 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

💻 Language Specification (MiniLang)

MiniLang is a procedural language designed for this compiler. It supports:

  • Data Types: int, bool, string
  • Operators: Arithmetic (+, -, *, /), Relational (<, >, <=, >=, ==, !=), Logical (&&, ||, !)
  • I/O: print(), input()

Example: Source Code to TAC Translation

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:

🚀 How to Run

This project uses uv, an extremely fast Python package installer and resolver, for dependency management.

  1. Clone the repository:

    git clone [https://github.com/your-username/minilang-compiler.git](https://github.com/your-username/minilang-compiler.git)
    cd minilang-compiler
  2. Install dependencies: Since the project includes a pyproject.toml, you can quickly sync the environment and install dependencies (like ply) using uv:

    uv sync
  3. Run the Interactive Test Suite: Use uv run to 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.

👨‍💻 Author

Hossein Rahmati Developer | Compiler Design Enthusiast LinkedIn | Email


This project was developed as a final academic project for the Compiler Design course.

About

A complete, multi-phase compiler for MiniLang (a custom, strongly-typed programming language), built from scratch in Python.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages