A lightweight educational compiler written in Python. It implements the core phases of a simple compiler pipeline: lexical analysis, parsing, semantic analysis, intermediate code generation, and target code generation.
MiniLang Compiler takes a small Pascal-like source program and produces compiler reports for each stage of compilation. It is designed for learning compiler construction concepts and demonstrating how source code moves through tokens, syntax trees, semantic checks, intermediate representation, and target-like instructions.
- Lexical analysis for reserved words, identifiers, numbers, strings, symbols, and compound operators
- Parser for a simple program structure and basic statements
- Semantic analysis for symbol tracking and use-before-assignment checks
- Intermediate code generation
- Simple target code generation
- Compilation reports written to the
reports/directory
The current compiler supports a small subset of a Pascal-like language:
- Program declaration using
program <name>; - Input statement:
read(variable) - Output statement:
write(variable) - Assignment statement:
variable := value - Comments beginning with
!
Example input:
program demo;
read(x)
y := 10
write(x)
write(y).
├── compiler.py
├── README.md
└── blahblahblah/
├── lexer.py
├── parser.py
├── semantic.py
├── intermediate.py
└── codegen.py
- Python 3.8 or newer
This project uses only the Python standard library.
Create a source file, for example sample.bbb:
program demo;
read(x)
y := 10
write(x)
write(y)Run the compiler:
python compiler.py sample.bbbAfter compilation, generated reports are written to the reports/ directory.
The compiler can generate the following files:
| Report | Description |
|---|---|
reports/lexer_report.txt |
Tokens, identifiers, and lexical errors |
reports/parser_report.txt |
Parsed syntax tree and parser errors |
reports/semantic_report.txt |
Symbol table and semantic errors |
reports/listing.txt |
Source listing with errors attached to line numbers |
reports/intermediate_report.txt |
Generated intermediate representation |
reports/target_report.txt |
Generated target-like instructions |
If lexical, parser, or semantic errors are found, compilation stops before intermediate and target code generation.
Source Code
↓
Lexer
↓
Parser
↓
Semantic Analyzer
↓
Intermediate Code Generator
↓
Target Code Generator
For this source:
program demo;
read(x)
y := 10
write(y)The intermediate report may contain:
READ x
y = 10
WRITE y
The target report may contain:
IN x
LOAD 10
STORE y
OUT y
- Identifiers are normalized to lowercase by the lexer.
- Identifier tokens are limited to the first 32 characters.
- Text after
!on a line is treated as a comment. - The parser currently supports a compact statement format and does not require semicolons after every statement.
- Add support for expressions such as
x + yora * b - Add block syntax with
beginandend - Add variable declarations and type checking
- Improve parser error recovery
- Add automated tests
- Package the compiler as a command-line tool
No license has been specified yet. Add a license before distributing or publishing this project.