如果“翻译”是指将 Python 转换为可读且可维护的 C,那么这是一个完全不同的问题 - 温和地说,这实质上更加困难。实际上,我怀疑任何机器翻译都会有太大的价值——在 Python 和 C 中通常处理问题的方式存在太大差异,因此对像样的机器翻译抱有很大希望。
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.
请查看 Shedskin。它正是这样做的(对于 C++ 以及 Python 及其模块的子集)。但它应该能够提供有关如何解决这个特定问题的宝贵见解(尽管编写自己的问题肯定不是是一项微不足道的任务)。
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).
发布评论
评论(4)
Shedskin:http://code.google.com/p/shedskin/
Boost Python:< a href="http://www.boost.org/doc/libs/1_42_0/libs/python/doc/index.html" rel="noreferrer">http://www.boost.org/doc/libs/ 1_42_0/libs/python/doc/index.html
PyCXX:http://cxx.sourceforge.net/< /a>
Cython:http://www.cython.org/
来自 http://wiki.python.org/moin/compile%20Python%20to%20C,有一个列表的相关项目。
耐热玻璃: http://www.cosc.canterbury.ac.nz/ greg.ewing/python/Pyrex/
psyco:http://psyco.sourceforge.net/
RPython:http://code.google.com/p/rpython/
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/
这里有一个基本问题:基本上是想创建一个使用 C 作为后端的 Python 编译器,还是将程序转换为 C 并在之后维护 C?
编写一个生成(非常丑陋的)C 作为其输出的编译器可能并不简单——编译器很少是这样的,并且为 Python 生成代码将比许多其他语言更困难(尤其是动态类型)编译,至少得到非常有效的输出)。 OTOH,至少解析器会比某些语言容易得多。
如果“翻译”是指将 Python 转换为可读且可维护的 C,那么这是一个完全不同的问题 - 温和地说,这实质上更加困难。实际上,我怀疑任何机器翻译都会有太大的价值——在 Python 和 C 中通常处理问题的方式存在太大差异,因此对像样的机器翻译抱有很大希望。
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.
很难相信没有人提到过 Cython——在我看来,Cython 几乎是此类工作的事实上的标准: http://www.cython.org/
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/
请查看 Shedskin。它正是这样做的(对于 C++ 以及 Python 及其模块的子集)。但它应该能够提供有关如何解决这个特定问题的宝贵见解(尽管编写自己的问题肯定不是是一项微不足道的任务)。
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).