Skip to content
 
 

Repository files navigation

Rust-like Compiler

A compiler for a subset of the Rust programming language, developed in C as a team project for the Compilers course at FCUP (University of Porto) in 2019.

The compiler parses a small Rust-like language, builds an abstract syntax tree, generates an intermediate representation, and translates it into MIPS assembly.

Features

The supported language subset includes:

  • Integer arithmetic
  • Boolean expressions and logical operators
  • Variable declarations and assignments
  • if / else statements
  • while loops
  • Basic input and output
  • Variable validation through a symbol table

Compiler pipeline

The project follows a simple compiler pipeline:

Source code
    ↓
Lexical analysis (Flex)
    ↓
Parsing (Bison)
    ↓
Abstract Syntax Tree
    ↓
Intermediate Code
    ↓
MIPS Assembly

The implementation uses:

  • C for the compiler implementation
  • Flex for lexical analysis
  • Bison for parsing
  • A hash-table-based symbol table
  • An intermediate representation before MIPS code generation

Building

The project requires a C compiler, Flex, Bison and Make.

make

This generates the compiler executable.

To compile one of the example programs:

./compiler example1.txt

The generated MIPS assembly is printed to the standard output.

The included examples can also be run with:

make test

My contributions

This was a collaborative university project. My contributions included work on:

  • Intermediate-code generation and boolean-expression support
  • Integration between intermediate code and MIPS generation
  • Pseudo-register handling
  • MIPS and intermediate-code bug fixes
  • Intermediate-code optimisations using immediate instructions such as addi, subi, andi and ori

The original development history and commits have been kept as they were produced during the course.

Context

This repository contains the original implementation produced for the Compilers course at the Faculty of Sciences of the University of Porto (FCUP) in 2019. It is preserved as an academic project rather than maintained as a production compiler.

About

Compiler for a subset of Rust, with intermediate-code and MIPS generation — FCUP Compilers project (2019)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages