Skip to content

A teaching compiler written in Python that parses a small C‑like language (minimal++), generates equivalent C code, and then produces x86 assembly via GCC. The project implements lexical analysis, parsing, semantic checks, and code generation to demonstrate end‑to‑end compiler construction.

License

Notifications You must be signed in to change notification settings

joanisprifti/Compiler

Repository files navigation

Compiler

A teaching compiler written in Python that parses a small C‑like language (minimal++), generates equivalent C code, and then produces x86 assembly via GCC. The project implements lexical analysis, parsing, semantic checks, and code generation to demonstrate end‑to‑end compiler construction (github.com).

📋 Table of Contents

  1. Overview
  2. Features
  3. Test Programs
  4. Technologies
  5. Installation
  6. Usage
  7. Architecture
  8. Report & Grammar
  9. Contributing
  10. License
  11. Contact

Overview

This project is an educational compiler implemented in Python 3 that takes as input programs written in minimal++ (a small C‑like grammar), translates them to C code, and then invokes GCC to produce x86 assembly and executable output. It covers all major compiler phases—lexing, parsing, semantic analysis, intermediate code generation, and final code emission. The goal is to understand how high‑level language constructs map to low‑level code and how an OS executes compiled programs.

Features

  • Lexical Analysis: Tokenizes minimal++ source using regex‑based scanner.
  • Parser: Recursive‑descent parser for minimal++ grammar (operators, control flow, functions).
  • Semantic Checks: Type checking, symbol table management, scope rules enforcement.
  • Code Generation: Emits equivalent C code and then x86 assembly via gcc -S.
  • Test Suite: Includes sample .min programs with expected C, assembly, and execution output.
  • Extensible: Grammar and code‑gen modules separated for easy extension.

Test Programs

Sample minimal++ programs are provided under /:

File Description
test_1.min Arithmetic expressions and variable usage
test_2.min Control flow: if, while loops
test_3.min Functions and recursion
test_4.min Array and pointer operations

Technologies

Component Arithmetic expressions and variable usage
Language Control flow: if, while loops
Compiler frontend Functions and recursion
Code generation Array and pointer operations

Installation

  1. Clone the repo
git clone https://github.com/johnprif/Compiler.git
cd Compiler
  1. Install dependencies (PLY)
pip install ply
  1. Ensure GCC is installed and on your PATH for C and assembly generation

Usage

Compile a .min file end‑to‑end:

python3 minimal++.py tests/test_1.min
# Generates test_1_quads.c, test_1_final.asm, and executable via gcc
./a.out

Control flags are available in the script for debug tracing of lexer/parser.

Architecture

┌─────────────┐     ┌──────────┐     ┌──────────────┐
│ Source (.min)│ --> │ Lexer    │ --> │ Parser       │
└─────────────┘     └──────────┘     └──────────────┘
                                          │
                                          v
                                    ┌─────────────┐
                                    │ Semantic    │
                                    │ Analyzer    │
                                    └─────────────┘
                                          │
                                          v
                                    ┌─────────────┐
                                    │ Code Gen    │
                                    └─────────────┘
                                          │
                                          v
                         ┌───────────────────────────┐
                         │ Generated C & Assembly     │
                         └───────────────────────────┘

Follows classical compiler phases: scanning → parsing → semantic analysis → code generation.

Report & Grammar

Detailed design, algorithmic choices, and test results are documented in report.pdf. The minimal++ grammar is defined in Η γραμματική της minimal++.v1.2.pdf under the repo root.

Contributing

Contributions are welcome! Please fork, create a feature branch, and submit a PR. For major changes, open an issue first to discuss.

License

This project is licensed under the MIT License. See LICENSE for full text.

Contact


Implemented as part of an academic workshop on compiler construction.

About

A teaching compiler written in Python that parses a small C‑like language (minimal++), generates equivalent C code, and then produces x86 assembly via GCC. The project implements lexical analysis, parsing, semantic checks, and code generation to demonstrate end‑to‑end compiler construction.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages