A teaching compiler written in Python that parses a small C‑like language (minimal++), generates equivalent C code, and then produces x86 assembly via GCC. The project implements lexical analysis, parsing, semantic checks, and code generation to demonstrate end‑to‑end compiler construction (github.com).
- Overview
- Features
- Test Programs
- Technologies
- Installation
- Usage
- Architecture
- Report & Grammar
- Contributing
- License
- Contact
This project is an educational compiler implemented in Python 3 that takes as input programs written in minimal++ (a small C‑like grammar), translates them to C code, and then invokes GCC to produce x86 assembly and executable output. It covers all major compiler phases—lexing, parsing, semantic analysis, intermediate code generation, and final code emission. The goal is to understand how high‑level language constructs map to low‑level code and how an OS executes compiled programs.
- Lexical Analysis: Tokenizes minimal++ source using regex‑based scanner.
- Parser: Recursive‑descent parser for minimal++ grammar (operators, control flow, functions).
- Semantic Checks: Type checking, symbol table management, scope rules enforcement.
- Code Generation: Emits equivalent C code and then x86 assembly via
gcc -S
. - Test Suite: Includes sample
.min
programs with expected C, assembly, and execution output. - Extensible: Grammar and code‑gen modules separated for easy extension.
Sample minimal++ programs are provided under /
:
File | Description |
---|---|
test_1.min |
Arithmetic expressions and variable usage |
test_2.min |
Control flow: if , while loops |
test_3.min |
Functions and recursion |
test_4.min |
Array and pointer operations |
Component | Arithmetic expressions and variable usage |
Language | Control flow: if , while loops |
Compiler frontend | Functions and recursion |
Code generation | Array and pointer operations |
- Clone the repo
git clone https://github.com/johnprif/Compiler.git
cd Compiler
- Install dependencies (PLY)
pip install ply
- Ensure GCC is installed and on your PATH for C and assembly generation
Compile a .min
file end‑to‑end:
python3 minimal++.py tests/test_1.min
# Generates test_1_quads.c, test_1_final.asm, and executable via gcc
./a.out
Control flags are available in the script for debug tracing of lexer/parser.
┌─────────────┐ ┌──────────┐ ┌──────────────┐
│ Source (.min)│ --> │ Lexer │ --> │ Parser │
└─────────────┘ └──────────┘ └──────────────┘
│
v
┌─────────────┐
│ Semantic │
│ Analyzer │
└─────────────┘
│
v
┌─────────────┐
│ Code Gen │
└─────────────┘
│
v
┌───────────────────────────┐
│ Generated C & Assembly │
└───────────────────────────┘
Follows classical compiler phases: scanning → parsing → semantic analysis → code generation.
Detailed design, algorithmic choices, and test results are documented in report.pdf
.
The minimal++ grammar is defined in Η γραμματική της minimal++.v1.2.pdf
under the repo root.
Contributions are welcome! Please fork, create a feature branch, and submit a PR. For major changes, open an issue first to discuss.
This project is licensed under the MIT License. See LICENSE for full text.
- GitHub: joanisprifti
- Email: [email protected]
- Phone: +306940020178
Implemented as part of an academic workshop on compiler construction.