Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I start writing a transpiler? Is it even possible?

Due to confidentiality reasons, I might not be able to describe in pin point details but here is the scenario.

Various devices that have streaming apps have different languages with different apis, though they accomplish the same thing. So, when you want to write a streaming app for one platform, you have to start from scratch while copying the same app for other platform, which is writing redundant logic. I want to design a transpiler, that takes code in one language, and produces code for all the native languages, kind of 1 to many, but am not sure how to start, and cannot find enough references on the internet. Is it even feasible to do it? the target languages include variants of javascript and primarily java.

like image 832
sumeet batheja Avatar asked Mar 22 '15 10:03

sumeet batheja


1 Answers

One way to do this is to use infrastructure designed to build translators.

Many people think this means "a parser generator"; that's actually naive. A good translator must parse the language, sure, but translation also needs tracking what symbols mean, verifying that what is said isn't nonsense, generating and optimizing code. You need a lot more machinery than just a parser (generator) to do this well. See Life After Parsing.

Our DMS Software Reengineering Toolkit is a set of tools for building program analyzers and translators, including as just one element a very strong parser generator.

DMS also includes a program transformation engine, enabling one to write translation rules in terms of languages being translated, if you see this, transform it to that. Writing translation rules directly in the surface syntax of the languages of interest makes them easier to write, inspect, debug and maintain.

That said, writing such a translator is not an easy task; you have to enumerate the complete set of syntax constructs with implied semantics, and figure out how to map that to a combination of target language syntax plus extra libraries you might custom build on the target side. This takes man-months, plural, even for experts, per translator.

DMS is agnostic about which programming languages are involved. You do have to define the languages of interest to it; it has a large stable of language definitions (useful as sources or targets, your choice) for standard languages such as Java and JavaScript. This available stable helps to shorten the development cycle, but it isn't generally the dominant cost.

There is a holy grail of building a "universal translator" in which one writes one set of rules and everything is peachy thereafter. That idea is a fantasy. It is worth understanding that a set of translation rules from language A to B, is not actually useful for translating language C to D, because the rules combine knowledge about specific syntax and implied semantics. Nonetheless, if you are going to build multiple "transpilers", doing it on a common foundation is an enormous win in terms of learning curve and long-term maintainability.

Using tools like DMS, one can write extremely accurate translators..

like image 147
Ira Baxter Avatar answered Oct 12 '22 12:10

Ira Baxter