Skip to content

commoner02/Complier-Design-MiniLang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MiniLang Compiler

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)

Features

  • Three data typesnum (double), str (char[1000]), bool (int)
  • Arithmetic, comparison & logical operators — including %, ++x, --x
  • Conditionalswhen / otherwise when / otherwise / done
  • Loops — fixed-count repeat n times do and repeat while
  • User-defined functions with typed parameters and return values
  • I/Oprint and input as <type>
  • 11 built-in math functionssqrt, 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

Prerequisites

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 bison

Building & Usage

git clone https://github.com/commoner02/Complier-Design-MiniLang.git
cd Complier-Design-MiniLang
make clean && make

Compile a .min source file:

./minilang tests/test_basic.min

The compiler will:

  1. Print the symbol table
  2. Print the generated C code
  3. Save test_basic.c and run it, saving output to test_basic_output.txt

Language Reference

Data Types

Type C Equivalent Example
num double num x = 3.14
str char[1000] str name = "MiniLang"
bool int (0 or 1) bool flag = true

Variables

num x = 10
num y
y = 20
str greeting = "Hello"
bool active = true

Operators

Category Operators
Arithmetic + - * / %
Comparison == != < > <= >=
Logical and or not
Inc / Dec ++x --x

Control Flow

when score >= 90 do
    print "A"
otherwise when score >= 80 do
    print "B"
otherwise do
    print "F"
done

Loops

# 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

Functions

func add(num a, num b) -> num do
    return a + b
done

num result = add(5, 3)
print result

Input / Output

print "Hello, World!"
print x + y

num age = input as num
str name = input as str

Built-in Math Functions

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) 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

Example Programs

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

Running Tests

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 Handling

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.


How It Works

  1. Lexer (minilang.llex.yy.c) — Flex tokenizes source into terminals like NUM_TYPE, IDENTIFIER, WHEN, PLUS, etc.
  2. Parser (minilang.yminilang.tab.c) — Bison validates syntax via an LALR(1) grammar and builds an Abstract Syntax Tree (AST).
  3. Symbol Table (symbol_table.c) — Tracks declared variables and functions; enforces scope rules during parsing.
  4. Code Generator (code_gen.c) — Walks the AST and emits C into two buffers: one for user-defined functions (before main) and one for the main body.
  5. Output — Both buffers are printed to stdout as a valid, self-contained C file.

Project Structure

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

License

Developed by Shuvo as a Compiler Design Laboratory assignment at Khulna University of Engineering and Technology (KUET), Department of Computer Science and Engineering.

About

MiniLang Compiler - A simple educational compiler that translates MiniLang code to C

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors