Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚙️ MiniLang Compiler

A complete, end-to-end compiler for a statically-typed, C-like language built with Flex, Bison, and C.


🌟 Overview

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.

✨ Features

  • Statically Typed: Strict enforcement of int and bool types, preventing logic errors like true < false or 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).

🚀 Getting Started

Prerequisites

You need a Unix-like environment with standard GNU build tools installed:

  • GCC (C Compiler)
  • Flex (Lexical Scanner Generator)
  • Bison (Parser Generator)
  • Make

Build Instructions

Simply clone the repository and use make:

git clone https://github.com/Nazmul42726/MiniCompiler.git
cd MiniCompiler
make

Usage

Run the generated minicompiler binary against any .ml source file:

./minicompiler testcases/test_valid.ml

Artifacts Generated:

  • output.tac: The optimized Three-Address Code intermediate representation.
  • output.s: The final pseudo-assembly machine code.

💻 Code Example

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:

📚 Documentation & Architecture

Want to understand how this compiler works under the hood? I've written "spoonfed" explanations for every single phase of the compiler.

  1. 🗺️ Compiler Workflow: A high-level visual and written explanation of the entire pipeline from Source Code to Assembly.
  2. 📖 Lexer: How Flex translates text to tokens.
  3. 🌳 Parser: How Bison builds the Abstract Syntax Tree (AST).
  4. 🏗️ AST Structures: How the tree is stored in memory.
  5. 🧠 Symbol Table: How block scoping and memory are managed.
  6. ⚖️ Semantic Analysis: How type checking and logic validation work.
  7. Code Gen & TAC: How the AST is flattened and optimized.
  8. 🤖 Target Assembly: How TAC is mapped to CPU registers.
  9. 🚦 Main Orchestrator: The driver that ties it all together.

📄 License

This project is distributed under the MIT License. See the LICENSE file for details.

About

A C-based compiler for MiniLang, a statically-typed C-like language, translating source code to Three-Address Code (TAC) using Flex and Bison.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages