A compiler for a custom programming language — built with Flex, Bison, and C. MiniLang (
.min) source files compile down to C, then to native executables.
source.min ──► Lexer ──► Parser ──► AST ──► Code Gen ──► C Code ──► Executable
(Flex) (Bison) (code_gen.c)
- Three data types —
num(double),str(char[1000]),bool(int) - Arithmetic, comparison & logical operators — including
%,++x,--x - Conditionals —
when / otherwise when / otherwise / done - Loops — fixed-count
repeat n times doandrepeat while - User-defined functions with typed parameters and return values
- I/O —
printandinput as <type> - 11 built-in math functions —
sqrt,abs,pow,sin,cos,tan,exp,log,ceil,floor,rand - Symbol table — duplicate-declaration and undeclared-variable detection with line numbers
- Clean C output — emitted code is human-readable and GCC-compilable
| Tool | Purpose |
|---|---|
| GCC | C compiler |
| Flex | Lexer generator |
| Bison | Parser generator |
| Make | Build automation |
# Ubuntu/Debian
sudo apt install gcc flex bison make
# macOS (Homebrew)
brew install flex bisongit clone https://github.com/commoner02/Complier-Design-MiniLang.git
cd Complier-Design-MiniLang
make clean && makeCompile a .min source file:
./minilang tests/test_basic.minThe compiler will:
- Print the symbol table
- Print the generated C code
- Save
test_basic.cand run it, saving output totest_basic_output.txt
| Type | C Equivalent | Example |
|---|---|---|
num |
double |
num x = 3.14 |
str |
char[1000] |
str name = "MiniLang" |
bool |
int (0 or 1) |
bool flag = true |
num x = 10
num y
y = 20
str greeting = "Hello"
bool active = true
| Category | Operators |
|---|---|
| Arithmetic | + - * / % |
| Comparison | == != < > <= >= |
| Logical | and or not |
| Inc / Dec | ++x --x |
when score >= 90 do
print "A"
otherwise when score >= 80 do
print "B"
otherwise do
print "F"
done
# Fixed iterations
repeat 5 times do
print "Hello"
done
# While loop
num i = 0
repeat while i < 5 do
print i
i = i + 1
done
func add(num a, num b) -> num do
return a + b
done
num result = add(5, 3)
print result
print "Hello, World!"
print x + y
num age = input as num
str name = input as str
| Function | Description | Example |
|---|---|---|
sqrt(x) |
Square root | sqrt(16) → 4 |
abs(x) |
Absolute value | abs(-10) → 10 |
pow(x, y) |
x raised to power y | pow(2, 3) → 8 |
sin(x) |
Sine (radians) | sin(0) → 0 |
cos(x) |
Cosine (radians) | cos(0) → 1 |
tan(x) |
Tangent (radians) | tan(0) → 0 |
exp(x) |
eˣ | exp(1) → 2.718 |
log(x) |
Natural logarithm | log(2.718) → 1 |
ceil(x) |
Round up | ceil(3.2) → 4 |
floor(x) |
Round down | floor(3.8) → 3 |
rand() |
Random number in [0, 1) | rand() → 0.456 |
Fibonacci (first 8 terms)
num a = 0
num b = 1
num temp
repeat 8 times do
print a
temp = a + b
a = b
b = temp
done
Grade calculator
num score = 85
when score >= 90 do
print "A"
otherwise when score >= 80 do
print "B"
otherwise when score >= 70 do
print "C"
otherwise do
print "F"
done
cd tests
./run_tests.sh=========================================
MiniLang Test Suite
=========================================
Testing: test_arithmetic ... ✓ PASSED
Testing: test_basic ... ✓ PASSED
Testing: test_conditional ... ✓ PASSED
Testing: test_functions ... ✓ PASSED
Testing: test_loops ... ✓ PASSED
Testing: test_math_functions ... ✓ PASSED
Testing: test_strings ... ✓ PASSED
=========================================
Results: 7 passed, 0 failed
All tests passed!
=========================================
| Error Type | Trigger | Example Message |
|---|---|---|
| Lexical | Unknown character in source | Error: unknown character '@' at line 3 |
| Syntax | Grammar rule violation | Error: syntax error at line 7 |
| Semantic | Undeclared variable or duplicate declaration | Error: Variable 'x' not declared at line 5 |
Compilation halts on any error and no C code is emitted.
- Lexer (
minilang.l→lex.yy.c) — Flex tokenizes source into terminals likeNUM_TYPE,IDENTIFIER,WHEN,PLUS, etc. - Parser (
minilang.y→minilang.tab.c) — Bison validates syntax via an LALR(1) grammar and builds an Abstract Syntax Tree (AST). - Symbol Table (
symbol_table.c) — Tracks declared variables and functions; enforces scope rules during parsing. - Code Generator (
code_gen.c) — Walks the AST and emits C into two buffers: one for user-defined functions (beforemain) and one for themainbody. - Output — Both buffers are printed to stdout as a valid, self-contained C file.
minilang/
├── src/
│ ├── minilang.l # Flex lexer rules
│ ├── minilang.y # Bison grammar + semantic actions
│ ├── ast.c / ast.h # AST node definitions and constructors
│ ├── symbol_table.c/.h # Variable and function symbol table
│ ├── code_gen.c/.h # C code generation from AST
│ └── main.c # Entry point, drives compilation
├── tests/
│ ├── run_tests.sh
│ └── test_*.min
├── Makefile
└── README.md
Developed by Shuvo as a Compiler Design Laboratory assignment at Khulna University of Engineering and Technology (KUET), Department of Computer Science and Engineering.