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?
-
23I'm hesitant to second-guess your needs without hearing more, but if you "were asked to write a code translator" (by someone like one of my worst bosses), that suggests a misunderstanding at some level. My initial reaction is that (a) someone thinks "Python=slow, C=fast" always, and (b) a code translator for Python would be an easy thing to write. I'd go back and ask why they want this. – Ken Mar 26 '10 at 18:01
-
possible duplicate of [Convert Python program to C/C++ code?](http://stackoverflow.com/questions/4650243/convert-python-program-to-c-c-code) – Anderson Green Jun 15 '15 at 05:56
4 Answers
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/

- 384,516
- 81
- 508
- 779
There's a fundamental question here: is the intent to basically create a Python compiler that uses C as a back-end, or to convert the program to C and maintain the C afterward?
Writing a compiler that produces (really ugly) C as its output probably isn't trivial -- a compiler rarely is, and generating code for Python will be more difficult than for a lot of other languages (dynamic typing, in particular, is hard to compile, at least to very efficient output). OTOH, at least the parser will be a lot easier than for some languages.
If by "translating", you mean converting Python to C that's readable and maintainable, that's a whole different question -- it's substantially more difficult, to put it mildly. Realistically, I doubt any machine translation will be worth much -- there are just too large of differences in how you normally approach problems in Python and C for there to be much hope of a decent machine translation.

- 476,176
- 80
- 629
- 1,111
-
8+1: Python is not C with a different Syntax. It's a fundamentally different language with unique semantics. If it was "C with a different syntax", someone would already have written that transformation. – S.Lott Mar 26 '10 at 18:14
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/

- 49,756
- 17
- 74
- 82
Have a look at Shedskin. It does exactly that (well, to C++ and for a subset of Python and its modules). But it should be able to provide valuable insight as how to approach this particular problem (although writing your own will certainly not be a trivial task).

- 112,638
- 29
- 165
- 179
-
Thank you, Christopher. I will study the source code of Shedskin now. – bodacydo Mar 26 '10 at 18:00