Search code examples
compiler-constructionlanguage-agnosticprogramming-languagescode-translation

Translating a language to another


I'm studying translation of languages (normally its just compilation). We study what to do to be able to translate from a language to another (say Java into Python but in our case C to assembler). I asked my teacher about the condition that the languages must satisfy to be able to do the translation. The answer was that there isn't any condition and that we can translate from any language to another (for example Java --> XML).

Is that really possible? What if one of the two language isn't Turing-complet language?


Solution

  • In theory you can translate any programming language to any other programming language. The example you gave, however, does not hold up because XML is not a programming language. You could certainly translate Java into an XML syntax but that language would not have any semantics. If you'd like you could define then and write a byte code compiler for your new Java like language with XML syntax. That would not be XML though. It'd be a new language with subset of XML as the syntax and Java's semantics.

    In practise, if you have a real programming language, you can compile any language to it but you might have slightly different properties based on things that turning completeness does not account for. For example you might have really bad runtime on concurrent programs if you compile Haskell to python. In an extreme case, you can't do IO if you compile any normal language to the lambda calculus.