This project is an educational compiler for a simple programming language called Cimple. The compiler is implemented in Python and developed using Visual Studio 2022. It performs several phases of compilation:
- Lexical Analysis: Tokenizes the source code.
- Syntax Analysis: Uses a recursive descent parser to generate intermediate code (quads) and build a symbol table.
- Intermediate Code Generation: Produces quads representing program operations.
- Assembly Code Generation: Translates the quads into MIPS-like assembly using symbol table info.
-
Comprehensive Analysis
Performs lexical analysis, syntax analysis, intermediate code generation, and assembly code generation. -
Symbol Table Management
Builds a dynamic symbol table during parsing for variables, parameters, and temporaries. -
Intermediate Code
Generates intermediate code (quads) saved in theint/directory. -
Assembly Code
Produces MIPS-like assembly code stored in theasm/directory. -
Unit Testing
Includes unit tests using Python'sunittestmodule.
-
Lexical Analysis
Converts the source file into tokens (identifiers, numbers, keywords, etc). -
Syntax Analysis
Parses tokens based on Cimple grammar to build quads and maintain the symbol table. -
Intermediate Code Generation
Produces.intfiles representing assignments, expressions, and function calls. -
Assembly Code Generation
Translates.intfiles into.asmMIPS-like code using memory offset data.
- Python 3.x
- Visual Studio 2022 (for development)
The main file is cimple_compiler_2025.py. It now accepts a source file as a command-line argument.
python3 cimple_compiler_2025.py example.ciThis will:
- Perform lexical and syntax analysis
- Generate
.intcode in theint/folder - Generate
.asmcode in theasm/folder - Print the symbol table and quads to the console
python3 -m unittest discoverThis command will auto-discover and run all unit tests.
cimple_compiler_2025.py— Main compiler file.tests/ci/— Sample input files (fibonacci.ci,factorial.ci, etc).int/— Output directory for intermediate code.asm/— Output directory for assembly code.test_cimple_compiler_2025.py— Unit tests for validation.