translate one language to another?
Asked Answered
M

4

7

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?

Mallet answered 13/1, 2010 at 7:54 Comment(3)
See my <a href="#3455956 answer on how to translate between languages</a>Pronucleus
You can have a cheap lousy solution that produces lousy but working solutions. As a general rule, you can't translate between langauges easily, and you can't do a good job with lousy foundational machinery.Pronucleus
One standard bad answer is, "translate the orginal language into calls on a target langauge library that simulates the original languages' semantics"; essentialy you are bulding an interpreter. You can always do this; one Turing machine can always emulate another. But the result of such translations is usually pretty horrible to read, doesn't use the target langauge in ways the target-language-trained maintainers expect, and often have high overhead (all those simulation calls) to boot. So, you can always do this, but you shouldn't.Pronucleus
P
12

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.
Pyridoxine answered 13/1, 2010 at 8:2 Comment(1)
Is there a book, tutorial or example you can suggest? Even a simple oneTankage
B
5

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.

Baeda answered 13/1, 2010 at 7:56 Comment(2)
Can't attest to how quickly or well, but sure.Baeda
This gives a whole new meaning to the ACID test!Khedive
E
2

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

Enchanting answered 13/1, 2010 at 8:3 Comment(3)
+1, never heard of the term transpiler before. Sounds corny as hell, but useful to know.Silsby
-1, I've been building tranlation tools a long time and whenever you hear some screwball term like "transpiler" or "transcoder" rather than just "translator", the answer itself is screwball and isn't really a good solution if works at all. The ones I have looked at actually produce answers, but only if by answer you mean "translates and runs and is an outright nightmare to maintain". See discussion on NACA which produces JOBOL: #1030474Pronucleus
Sorry that this wasn't in your education; my teachers taught me this term.Enchanting
M
1

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

Mump answered 13/1, 2010 at 7:58 Comment(1)
i dont quite get it...what does it do in plain english?Mallet

© 2022 - 2024 — McMap. All rights reserved.