Skip to content

Latest commit

 

History

164 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Contributors Forks Stargazers Issues project_license LinkedIn


mini-compiler

project_description
Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

About The Project

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: int and float for variables and literals.
  • Control Flow: if-else and while statements.
  • 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.

(back to top)

Built With

  • C
  • Flex
  • Bison
  • Python
  • GNU Make

(back to top)

Getting Started

Follow the steps below to set up and build the project locally.

Prerequisites

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

Installation

  1. Clone the repository:
    git clone https://github.com/diegohommer/mini-compiler.git
    cd mini-compiler
  2. Build the project using make:
    make
  3. 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

(back to top)

Usage

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.

Example

# Generate x86 assembly (default)
./bin/mini_compiler < source_code.txt

# Generate ILOC code
./bin/mini_compiler --iloc < source_code.txt

Roadmap

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

(back to top)

Contributing

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

Top contributors:

contrib.rocks image

License

Distributed under the MIT license. See LICENSE.txt for more information.

(back to top)

Contact

Diego Hommerding Amorim - LinkedIn - diegohommeramorim@gmail.com

Project Link: https://github.com/diegohommer/mini-compiler

(back to top)

Acknowledgments

(back to top)

About

A simple compiler developed as part of the Compilers course at UFRGS. It includes a full frontend with lexical, syntactic, and semantic analysis, and generates both ILOC and x86 assembly code.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Contributors

Languages