Skip to content

Yam1nX/compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C Compiler Design and Implementation

Language Status Target

A from-scratch C compiler implemented in C++, demonstrating the full compilation pipeline from source code to target assembly.
The project emphasizes modular design, educational clarity, and correctness, making it suitable for academic use and compiler fundamentals study.


Overview

This compiler processes C source code through all standard compilation phases:

  • Lexical Analysis – Token generation
  • Syntax Analysis – Parsing and AST construction
  • Semantic Analysis – Type checking and scope validation
  • Intermediate Code Generation – Three Address Code (TAC)
  • Target Code Generation – MIPS-like assembly output

The implementation mirrors the structure of industrial compilers while remaining readable and extensible.


Features

  • Complete multi-phase compiler pipeline
  • Recursive descent parser with operator precedence
  • Hierarchical symbol table with scope management
  • Semantic analysis with detailed error reporting
  • Three Address Code (TAC) intermediate representation
  • MIPS-style assembly code generation
  • Clear diagnostics with source line and column tracking
  • Modular and extensible architecture

Supported C Subset

Data Types

  • int
  • float
  • char
  • void

Language Constructs

  • Variable declarations and assignments
  • Arithmetic, relational, and logical expressions
  • Control flow:
    • if / else
    • while
    • for
    • do-while
  • Functions:
    • Definition
    • Parameters
    • Return values
    • Recursion
  • Basic I/O:
    • printf
    • scanf

Partial / Not Implemented

  • Arrays (basic support)
  • Pointers (limited)
  • Structs, unions, typedef
  • Preprocessor directives

Compiler Architecture


C Source Code
↓
Lexical Analyzer
↓
Syntax Analyzer (Parser)
↓
Semantic Analyzer
↓
Intermediate Code Generator (TAC)
↓
Target Code Generator
↓
MIPS-like Assembly Output

deepseek_mermaid_20260123_96ca32

Build

git clone https://github.com/Yam1nX/compiler.git
cd compiler
g++ compiler.cpp -o compiler

Usage

./compiler input.c

Output Includes

  • Token stream
  • Three Address Code (TAC)
  • Generated assembly code

Example

Input (input.c)

int main() {
    int a = 10;
    int b = 20;
    int sum = a + b;
    printf("Sum = %d\n", sum);
    return 0;
}

Output

Screenshot 2026-01-23 163034 Screenshot 2026-01-23 163144
  • Tokens generated
  • TAC instructions
  • MIPS-style assembly code

Testing

  • 30+ test programs covering:

    • Expressions
    • Control flow
    • Functions
    • Semantic errors
  • Lexical, syntax, and semantic error validation

  • Comparison against reference compiler outputs


Future Enhancements

  • Full support for arrays, pointers, structs, and unions

  • Advanced optimizations:

    • Constant folding
    • Dead code elimination
    • Better register allocation
  • Support for additional targets (x86-64, ARM, WebAssembly)

  • Basic linker and preprocessor support

  • Enhanced diagnostics and warnings


Author

Name: Ashabul Yamin Tuhin Course: Compiler Lab Department: Computer Science and Engineering


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors