Py2C is an educational compiler project that translates a well-defined subset of Python into readable, idiomatic C code. The goal of this project is not full Python compatibility, but to demonstrate core compiler concepts end‑to‑end: parsing, IR construction, optimization, dead‑code elimination, and code generation.
This project is intentionally designed to be simple, inspectable, and academically meaningful, making it suitable as a systems/PL portfolio project.
- Dive into the Documentation of the project at Py2C/docs
-
Python → C compilation pipeline
-
Explicit Intermediate Representation (IR)
-
Support for:
- Integer variables
- Arithmetic expressions
if / elif / elseforloops usingrange()whileloopsbreak/continue- Functions with integer parameters & return values
print()→printf()lowering
-
Compiler optimizations:
- Constant Folding
- Dead Code Elimination (DCE)
-
Clean, readable generated C code
Python Source
│
▼
AST (Python ast module)
│
▼
Intermediate Representation (IR)
│
├── Constant Folding
├── Dead Code Elimination
│
▼
C Code Generation
Each phase is explicitly separated, making the compiler easy to extend and reason about.
Py2C/
├── py2c/
│ ├── parser.py # Python AST → IR
│ ├── ir.py # IR node definitions
│ ├── optimizer.py # Constant folding
│ ├── dce.py # Dead code elimination
│ ├── codegen.py # IR → C code generator
│ └── __init__.py
├── examples/
│ └── input.py # Sample Python program
├── main.py # Compiler entry point
├── README.md
└── LICENSE
Edit the example program:
examples/input.py
python main.pyThe generated C code is printed to stdout:
==== Generated C Code ====
#include <stdio.h>
int add(int a, int b) {
return (a + b);
}
int square(int x) {
return (x * x);
}
int main() {
int x = 5;
int y = 40;
int z = (x + y);
int result = add(z, 5);
int squared = square(result);
if (result > 40) {
int flag = 1;
}
else {
flag = 0;
}
int sum = 0;
for (int i = 0; i < 5; i += 1) {
sum = (sum + i);
}
if (flag == 1) {
printf("%d\n", squared);
}
else {
printf("%d\n", result);
}
printf("%d\n", sum);
return 0;
}def add(a, b):
return a + b
def square(x):
return x * x
# ---- Constant Folding Demo ----
x = 2 + 3
y = 10 * 4
z = x + y
# ---- Dead Code Elimination Demo ----
unused = 999
temp = 12345 # removed by DCE
# ---- Function Calls ----
result = add(z, 5)
squared = square(result)
# ---- If / Else Demo ----
if result > 40:
flag = 1
else:
flag = 0
# ---- Loop Demo ----
sum = 0
for i in range(0, 5):
sum = sum + i
# ---- Conditional Output ----
if flag == 1:
print(squared)
else:
print(result)
print(sum)| Feature | Supported |
|---|---|
| Integers | ✅ |
Arithmetic (+ - * / %) |
✅ |
| Variables | ✅ |
| Functions | ✅ (int only) |
if / else |
✅ |
for range() |
✅ |
while |
✅ |
break / continue |
✅ |
| Lists / dicts | ❌ |
| Classes | ❌ |
| Floats / strings | ❌ |
Compile‑time evaluation of constant expressions:
x = 2 + 3 * 4Becomes:
int x = 14;Unused variable assignments are removed automatically:
a = 10
b = 20
print(a)b is eliminated during compilation.
This project was built to demonstrate:
- Understanding of compiler pipelines
- Experience with ASTs and IRs
- Basic program analysis & optimization
- Ability to design clean, extensible systems
It is not intended to be a production compiler, but a clear and honest educational artifact.
- Control Flow Graph (CFG) construction
- SSA form
- Liveness analysis for registers
- Type inference
- Arrays and pointers
- Emitting
.cfiles directly
This project is open-source and available under the MIT License.