Table of Contents
This mini-compiler project is an educational endeavor to build a functional compiler for a simplified programming language. It meticulously implements the traditional stages of compilation, demonstrating the process of transforming source code through an Intermediate Language (ILOC) into executable x86 assembly code. The project primarily focuses on understanding and implementing the core principles of lexical analysis, parsing, semantic analysis, intermediate code generation, and target code generation.
Key Features:
- Program Structure: Supports functions, global variables, and sequential execution.
- Data Types:
intandfloatfor variables and literals. - Control Flow:
if-elseandwhilestatements. - Variables: Declaration, initialization, and assignment.
- Function Calls: User-defined functions with arguments.
- Expressions: Arithmetic, comparison, bitwise, and unary operations.
- Return Statements: Type-checked return values.
- Scoping: Block and function-level scope management.
- Frontend Integration: Lexical, syntactic, and semantic analysis.
- ILOC Generation: Outputs machine-independent intermediate code.
- x86 Assembly: Translates ILOC to x86 assembly.
Follow the steps below to set up and build the project locally.
Make sure the following tools are installed on your system:
- Flex
sudo apt install flex
- Bison
sudo apt install bison
- GCC
sudo apt install build-essential
- Make
sudo apt install make
- Clone the repository:
git clone https://github.com/diegohommer/mini-compiler.git cd mini-compiler - Build the project using make:
make
- Change git remote url to avoid accidental pushes to base project
git remote set-url origin YOUR_USERNAME/mini-compiler git remote -v # Confirm the changes
Use the generated executable ./bin/mini_compiler to compile source files written in the defined language.
The program supports two output modes via command-line flags:
--iloc— outputs the Intermediate Language (ILOC) code.--asm(default) — outputs the generated x86 assembly code.
There is a Python script ilocsim.py in the src/codegen subdirectory to simulate ILOC execution.
The generated assembly can be compiled and run with GCC.
Run semantic tests with the provided run_tests.sh script, which checks compiler exit codes against expected results.
# Generate x86 assembly (default)
./bin/mini_compiler < source_code.txt
# Generate ILOC code
./bin/mini_compiler --iloc < source_code.txt- Support for function calls in code generation
- Support for float operations
- Support for calling other functions at runtime
- ILOC optimizations
- Assembly optimizations
- Register allocation improvements
See the open issues for a full list of proposed features and known issues.
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Distributed under the MIT license. See LICENSE.txt for more information.
Diego Hommerding Amorim - LinkedIn - diegohommeramorim@gmail.com
Project Link: https://github.com/diegohommer/mini-compiler
- Prof. Lucas Schnorr – for guidance and support during the course.