This repository contains the implementation of a simple compiler created as part of the COMPILER CONSTRUCTION LAB - 2024, instructed by Sir Laeeq Khan Niazi.
This compiler takes a source code file (e.g., code.txt
) as input and performs the following steps:
- Tokenization: Breaks the code into meaningful units called tokens.
- Parsing: Analyzes the tokens to check the syntax and ensures the code follows the grammar rules.
- Symbol Table Management: Keeps track of all variables, constants, and their attributes (e.g., type, value, scope).
- Three-Address Code (TAC) Generation: Converts the code into an intermediate representation using three-address code.
- Intel x86 Assembly Code Generation: Generates assembly code (e.g.,
output.asm
) compatible with the Intel x86 architecture.
This tool helps understand the process behind compiling a program, from high-level code to low-level machine instructions.
Run the following command to compile the source code of the compiler:
g++ compiler.cpp -o compiler.exe
Once compiled, you can use the compiler with the following command:
./compiler.exe code.txt output.asm
In case the above command does not work, run this:
compiler.exe code.txt output.asm
Here:
code.txt
is the input file containing the source code to be compiled.output.asm
is the generated Intel x86 assembly file.
- Educational Purpose: Simplified design to help students learn about compiler construction.
- Full Pipeline: Covers key phases of compilation: Lexical Analysis, Syntax Analysis, Semantic Analysis, Intermediate Code Generation, and Assembly Code Generation.
- Output Assembly: Generates Intel x86 assembly code as output, making it easier to see how high-level code maps to low-level instructions.
Feel free to explore the code, test it with your own examples, and learn how a compiler works step by step!