Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 652
ajsie Avatar asked Jan 13 '10 07:01

ajsie


2 Answers

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.
like image 107
cletus Avatar answered Sep 20 '22 07:09

cletus


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.

like image 20
Ignacio Vazquez-Abrams Avatar answered Sep 20 '22 07:09

Ignacio Vazquez-Abrams