Skip to content

Nazmul42726/MiniCompiler

Repository files navigation

MiniLang Compiler

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.

At a Glance

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:

Project Architecture

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

Compilation Phases

  • 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.

Getting Started

Prerequisites

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.

Building & Running

  1. Clone the repository:

    git clone https://github.com/Nazmul42726/MiniCompiler.git
    cd MiniCompiler
  2. Compile the project:

    make
  3. Run a test case:

    ./minicompiler testcases/complex.ml

The generated code will be saved in output.tac and output.s.

Deep Dive into Compiler Theory

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.

License

Distributed under the MIT License. See LICENSE for more information.

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

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors