This repository contains the implementation of a complete compiler for MiniLang, a statically-typed, C-like educational programming language. The compiler transforms high-level MiniLang source code through a full pipeline: lexical, syntactic, and semantic analysis, followed by intermediate code generation (TAC) with optimization, and final target pseudo-assembly emission.
MiniLang allows for standard procedural constructs like variable declarations, arithmetic expressions, and control flow.
Source (testcases/complex.ml):
int x;
x = 5 + 3 * 2;
if (x > 10) {
print(x);
}Optimized Intermediate Code (output.tac):
t1 = 11
x = 11
t2 = x > 10
ifFalse t2 goto L0
print x
L0:
The codebase is structured to mirror the standard phases of a compiler:
.
├── lexer.l # Lexical Analyzer (Flex)
├── parser.y # Syntax Analyzer (Bison)
├── ast.c/h # Abstract Syntax Tree representation
├── symbol_table.c/h # Scoping and Symbol Management
├── semantic.c/h # Semantic Validation & Type Checking
├── codegen.c/h # TAC Generation & Intermediate Optimization
├── target_gen.c/h # Target Pseudo-Assembly Generation
├── main.c # Compiler Driver
└── testcases/ # Feature-specific test suite
- Lexical Analysis: Tokenization using Finite Automata.
- Syntax Analysis: LALR(1) Parsing and AST Construction.
- Semantic Analysis: Type checking and static scoping.
- ICG: Generation of Three-Address Code (TAC).
- Optimization: Constant folding and dead code elimination.
- Target Generation: Mapping IR to pseudo-assembly.
To build and run the compiler, you will need:
- GCC: C compiler.
- Flex: Fast lexical analyzer generator.
- Bison: GNU parser generator.
- Make: Build automation tool.
-
Clone the repository:
git clone https://github.com/Nazmul42726/MiniCompiler.git cd MiniCompiler -
Compile the project:
make
-
Run a test case:
./minicompiler testcases/complex.ml
The generated code will be saved in output.tac and output.s.
For a comprehensive walkthrough of the architectural decisions, theoretical foundations (Chomsky Hierarchy, DFA, LALR parsing), and implementation details, refer to:
- EXPLANATION.md: A detailed chapter-by-chapter guide to the compiler pipeline.
- docs/report.pdf: Formal technical report and complexity analysis.
Distributed under the MIT License. See LICENSE for more information.