Skip to content

malachyoconnor/Python-Transpiler-CPP

Repository files navigation

Python-to-C Transpiler

A lightweight, single-pass transpiler written in C++ that converts a subset of Python syntax into C. Includes lexical analysis, recursive descent parsing, and strategic code emission.

🛠 Supported Grammar

The transpiler currently supports the following syntax mapping:

Feature Python Syntax Generated C Equivalent
Printing print("hello") or print(x) printf("hello\n"); or printf("%.2f\n", x);
Loops for i in range(1, 10): for (float i = 1; i < 10; ++i) { ... }
Conditionals if x > 5: if (x > 5) { ... }
Input x = input() float x; scanf("%f", &x);
Assignments y = (10 + 5) * 2 float y = (10 + 5) * 2;

💻 Code Snippet Example

Input: example.py

total = 0
iterations = input()

for i in range(iterations):
   if i > 5:
      print(i)
      total = total + i

print("Finished")

Output: out.c

#include <stdio.h>

int main() {
   float total = 0;
   float iterations;
   scanf("%f", &iterations);

   for (float i = 0; i < iterations; ++i) {
      if (i > 5) {
         printf("%.2f\n", i);
         total = (total + i);
      }
   }

   printf("Finished\n");
}

Building and running

This project uses CMake to manage the build process. Ensure you have a C++20 compatible compiler installed.

Configure the Project

Create a build directory, then generate the build files with this command:

mkdir build
cd build
cmake ..

About

C++ transpiler for a small subset of Python to C.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors