A complete, end-to-end compiler for a statically-typed, C-like language built with Flex, Bison, and C.
MiniLang is an educational programming language compiler that transforms high-level code through a rigorous pipeline: Lexical Analysis -> Syntactic Parsing -> Semantic Validation -> Intermediate Code Gen -> Optimization -> Assembly.
It handles procedural constructs such as strictly typed variable declarations, block-scoping, arithmetic/relational expressions, and control flow loops without relying on standard library overhead.
- Statically Typed: Strict enforcement of
intandbooltypes, preventing logic errors liketrue < falseor adding ints to booleans. - Block Scoping: Implements static scoping via a dynamic symbol table stack, allowing true local variable encapsulation within
{ }blocks. - Optimizing Middle-End: Generates Three-Address Code (TAC) and applies Constant Folding, Algebraic Simplification, and Dead Code Elimination.
- Pseudo-Assembly Target: Maps optimized IR to a custom virtual register machine architecture (
output.s).
You need a Unix-like environment with standard GNU build tools installed:
- GCC (C Compiler)
- Flex (Lexical Scanner Generator)
- Bison (Parser Generator)
- Make
Simply clone the repository and use make:
git clone https://github.com/Nazmul42726/MiniCompiler.git
cd MiniCompiler
makeRun the generated minicompiler binary against any .ml source file:
./minicompiler testcases/test_valid.mlArtifacts Generated:
output.tac: The optimized Three-Address Code intermediate representation.output.s: The final pseudo-assembly machine code.
Input (testcases/test_valid.ml):
int x;
x = 5;
if (x > 3) {
int y;
y = x * 2;
print(y);
}Optimized TAC (output.tac):
x = 5
t0 = x > 3
ifFalse t0 goto L0
t1 = x * 2
y = t1
print y
goto L1
L0:
L1:
Want to understand how this compiler works under the hood? I've written "spoonfed" explanations for every single phase of the compiler.
- 🗺️ Compiler Workflow: A high-level visual and written explanation of the entire pipeline from Source Code to Assembly.
- 📖 Lexer: How Flex translates text to tokens.
- 🌳 Parser: How Bison builds the Abstract Syntax Tree (AST).
- 🏗️ AST Structures: How the tree is stored in memory.
- 🧠 Symbol Table: How block scoping and memory are managed.
- ⚖️ Semantic Analysis: How type checking and logic validation work.
- ⚡ Code Gen & TAC: How the AST is flattened and optimized.
- 🤖 Target Assembly: How TAC is mapped to CPU registers.
- 🚦 Main Orchestrator: The driver that ties it all together.
This project is distributed under the MIT License. See the LICENSE file for details.