Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing code translator from Python to C? [closed]

I was asked to write a code translator that would take a Python program and produce a C program. Do you have any ideas how could I approach this problem or is it even possible?

like image 501
bodacydo Avatar asked Mar 26 '10 17:03

bodacydo


People also ask

Can I convert Python code to C?

Python code can make calls directly into C modules. Those C modules can be either generic C libraries or libraries built specifically to work with Python. Cython generates the second kind of module: C libraries that talk to Python's internals, and that can be bundled with existing Python code.

Is Python interpreted to C?

Python is an interpreted language, which means the source code of a Python program is converted into bytecode that is then executed by the Python virtual machine. Python is different from major compiled languages, such as C and C + +, as Python code is not required to be built and linked like code for these languages.

Can C and Python work together?

Extending Python with C or C++ It is quite easy to add new built-in modules to Python, if you know how to program in C. Such extension modules can do two things that can't be done directly in Python: they can implement new built-in object types, and they can call C library functions and system calls.


2 Answers

It's hard to believe that nobody has mentioned Cython -- pretty much the de facto standard for this type of job, in my opinion: http://www.cython.org/

like image 130
carl Avatar answered Sep 26 '22 18:09

carl


Shedskin: http://code.google.com/p/shedskin/

Boost Python: http://www.boost.org/doc/libs/1_42_0/libs/python/doc/index.html

PyCXX: http://cxx.sourceforge.net/

Cython: http://www.cython.org/

from http://wiki.python.org/moin/compile%20Python%20to%20C, there's a list of related projects.

Pyrex: http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/

psyco: http://psyco.sourceforge.net/

RPython: http://code.google.com/p/rpython/

like image 31
S.Lott Avatar answered Sep 22 '22 18:09

S.Lott