views:

154

answers:

4

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?

+5  A: 

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.

Ignacio Vazquez-Abrams
+1 for acid hehe
Filip Ekberg
but it works?:)
weng
Can't attest to how quickly or well, but sure.
Ignacio Vazquez-Abrams
This gives a whole new meaning to the ACID test!
dsimcha
+1  A: 

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

darren
i dont quite get it...what does it do in plain english?
weng
+8  A: 

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.
cletus
+2  A: 

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

Aaron Digulla
+1, never heard of the term transpiler before. Sounds corny as hell, but useful to know.
Shane MacLaughlin