开发者

translate one language to another?

is it possible to translate one language to another with an interpreter?

heard that quercus could translate php to java? at first, i thought it was a cheap lousy solution which could give code errors, but it seems that it´s fully possible to do so.

could 开发者_运维技巧you translate php to other languages, like python or ruby? c++ to java and so on?


Translating one language to another is just a special case for the class of programs called compilers, interpreters and translators.

This class of program will take a stream of input symbols ("source code") that can usually be described by a formal grammar and will output a stream of symbols.

That output stream of symbols can be:

  • Native assembly code, usually for the operating system and hardware the machine is running on. If so, the program is referred to as a compiler;
  • Native assembly code for a different OS and/or hardware. This can be called a compiler too but is often referred to as a cross-compiler;
  • To an intermediate form that can be executed by a virtual machine of some kind. This isn't a true compiler but is often called a compiler anyway. The Java, C#, F#, VB.NET, etc "compilers" all fall into this category;
  • To another language entirely. This is called a translator and there are examples of, say, Java to C# translators. They typically have varying degrees of success because idioms often aren't readily translatable;
  • Interpreters follow the same principle but typically execute the processed form in-place rather than saving it somewhere. Perl, PHP and shell scripts all fall into this category. PHP for example will store opcodes in an opcode cache as an intermediate form (if opcoding caching is enabled) but this intermediate form isn't stored so it's still safe to call PHP an interpreter.


The problem comes when you have idioms that don't translate well, either from or to. You get code that is syntactically valid, but looks like it was written by someone on acid.


Search SO for "transpiler" for some pointers: https://stackoverflow.com/search?q=transpiler


This may not be what you are after but there is SWIG http://en.wikipedia.org/wiki/SWIG

We've used it in one of our projects to create python, java and ruby bindings / wrappers for a C++ framework. See it in action at http://marsyas.sness.net

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜