Skip to content

tausif112/mini_compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🔧 Mini Compiler using Flex & Bison

Language Flex Bison Platform License

A lightweight **Mini Compiler** built using **Flex (Lex)**, **Bison (YACC)**, and **C**. The project demonstrates the fundamental phases of compiler construction, including lexical analysis, syntax analysis, parsing, symbol table generation, and intermediate instruction generation.

🚀 Features

  • ✅ Lexical Analysis using Flex
  • ✅ Syntax Analysis using Bison
  • ✅ Token Recognition
  • ✅ Parsing and Grammar Validation
  • ✅ Symbol Table Generation
  • ✅ Intermediate Instruction Generation
  • ✅ Error Detection and Reporting
  • ✅ Command-Line Based Compiler Workflow

🏗 Compiler Architecture

Source Code
     │
     ▼
Lexical Analyzer (Flex)
     │
     ▼
Token Stream
     │
     ▼
Parser (Bison)
     │
     ▼
Syntax Tree / Validation
     │
     ▼
Symbol Table Generation
     │
     ▼
Intermediate Instructions

🛠 Technologies Used

Technology Purpose
C Core Implementation
Flex (Lex) Lexical Analysis
Bison (YACC) Syntax Analysis & Parsing
GCC Compilation
Ubuntu 24.04 Development Environment
Oracle VirtualBox Virtualized Environment
Git & GitHub Version Control

📂 Project Structure

mini_compiler/
│
├── lexer.l
├── parser.y
├── input.txt
├── input2.txt
├── compiler
├── README.md
├── LICENSE
└── .gitignore

⚙️ Installation

Install Required Packages

sudo apt update
sudo apt install flex bison gcc make git -y

🔨 Build Instructions

Generate lexer and parser files:

flex lexer.l
bison -d parser.y

Compile the compiler:

gcc lex.yy.c parser.tab.c -o compiler -lfl

▶️ Running the Compiler

Test Input 1

./compiler < input.txt

Test Input 2

./compiler < input2.txt

🧪 Sample Source Code

int a;

a = 10;

if (a > 5) {

    int b;
    b = a + 2;

    while (b > 0) {

        int c;
        c = b - 1;

        b = b - 1;
    }
}

📚 Supported Language Elements

Category Supported Elements
Data Types int, float
Conditional Statements if, else
Loops while
Arithmetic Operators +, -, *, /
Relational Operators ==, !=, <, >, <=, >=
Delimiters ;
Brackets (), {}
Variable Declarations Supported
Assignments Supported

📈 Learning Outcomes

Through this project, the following compiler design concepts were explored:

  • Compiler Construction Fundamentals
  • Lexical Analysis
  • Syntax Analysis
  • Parsing Techniques
  • Context-Free Grammars
  • Symbol Table Management
  • Intermediate Code Representation
  • Flex & Bison Integration
  • Linux-Based Development Workflow

🎓 Academic Relevance

This project was developed as part of Compiler Design coursework and serves as a practical demonstration of how programming language source code is analyzed and processed during compilation.


🔮 Future Improvements

  • Semantic Analysis
  • Abstract Syntax Tree (AST) Generation
  • Type Checking
  • Optimization Passes
  • Three-Address Code Generation
  • Assembly Code Generation
  • Enhanced Error Recovery

👨‍💻 Author

Md Tausif Uddin

Department of Computer Science & Engineering (CSE)
University of Asia Pacific (UAP)

GitHub: https://github.com/tausif112


📜 License

This project is licensed under the MIT License.

Releases

No releases published

Packages

 
 
 

Contributors