Projects

C-to-MIPS Compiler

Compilers
C++
Language Processing
Code Generation
Team Project

A feature-complete C compiler targeting MIPS assembly, implementing lexical analysis, parsing, and code generation with support for advanced language features.

Abstract representation of code transformation from C to MIPS assembly

The C-to-MIPS Compiler project represents a month-long intensive development effort to build a fully functional compiler capable of translating C source code into MIPS assembly language. Developed collaboratively with a partner, this project demonstrates mastery of compiler design principles from lexical analysis through code generation.

The compiler architecture follows the traditional three-phase design with distinct lexer, parser, and code generator components. The lexical analyzer tokenizes input source code, identifying keywords, operators, literals, and identifiers while handling whitespace and comments. The parser constructs an abstract syntax tree representation of the program structure, enforcing C language grammar rules and detecting syntax errors.

Code generation transforms the AST into optimized MIPS assembly code, implementing a sophisticated register allocation strategy to minimize memory access. The system handles function calling conventions including parameter passing through both registers and stack frames, local variable management, and return value handling. Support for functions with more than four parameters demonstrates proper stack manipulation and calling convention adherence.

Language feature support spans basic, intermediate, and advanced categories. Basic features include local and global variable declarations, arithmetic and logical expressions, if-then-else statements, and while loops. Intermediate features extend to multiple function definitions with parameter passing, for loops, array declarations both global and local, recursive function calls, and the enum, break, and continue keywords.

Advanced features include support for multiple data types (double, float, char, unsigned), external function declarations for linking with separately compiled code, locally scoped variable declarations within statement blocks, and the sizeof operator for both types and variables. The implementation demonstrates careful attention to type system semantics and scope resolution.

Testing methodology employed 87 comprehensive test cases covering each implemented feature across various input scenarios and edge cases. The test suite validates correct code generation, proper handling of type conversions, accurate memory addressing for arrays, and correct implementation of control flow structures.

The project utilized modern compiler construction tools including Flex for lexical analysis and Bison for parser generation. The Abstract Syntax Tree implementation employs a clean object-oriented design with distinct node classes for each language construct, enabling straightforward tree traversal during code generation.

Performance optimization includes efficient register utilization, minimal redundant memory operations, and proper instruction selection for each operation type. The generated assembly code follows MIPS calling conventions and can be linked with external libraries, enabling integration with larger software systems.

This compiler project demonstrates comprehensive understanding of language processing, code optimization techniques, and the ability to implement complex software systems through systematic development and rigorous testing methodologies.