Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating source to source translator

I want to know that what are the strategies to create a source to source translator i.e creating translation from one high level language to another. The two ways that come into my mind are

1- Changing syntax tree of one language to other language syntax tree 2- Changing it to intermediate language and then converting that to other high level language

My question is that is it possible to do the conversion using both strategies and which is more feasible to do, can anyone give some refernces to any theory or implementation done by some converter like any of above methods. And is there any standard xml based intermediate language, i know that xmlvm uses xml as intermediate language but it does not provide any proper specification of the intermediate language.

like image 570
Zabi Avatar asked Aug 22 '11 10:08

Zabi


2 Answers

Any compiler is, roughly, a source-to-source translator. Target language can be an assembly language (or directly a binary machine code language), or C, or whatever high level language you fancy. So, the general compilers theory is applicable.

And just as a word of advice - one intermediate language is normally not nearly enough. Use more. Use dozens of intermediate languages, each different from a previous one in just one tiny aspect. This way any language-to-language translation is nothing but trivial.

Another word of advice (anticipating downvotes here) - stay away from XML, especially as a representation for ASTs.

like image 171
SK-logic Avatar answered Nov 16 '22 06:11

SK-logic


I would look at LLVM, which can do source to source. Although the output isn't pretty, it might provide some good ideas.

like image 30
J. M. Becker Avatar answered Nov 16 '22 04:11

J. M. Becker