问题
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.
回答1:
You might want to look at (or even join) an open-source compiler project like Haxe. It already can compile from Haxe source code to Java, JavaScript, C#, C++, Neko VM, Flash, LUA, Python and HashLink (ByteCode and C). The Haxe compiler is build with OCAML, if that matters.
More info:
- https://haxe.org
- https://github.com/haxefoundation/haxe
回答2:
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..
回答3:
Yes, this is possible, and compilers are often written this way.
The general idea is to create an "intermediate language", then write several one-way translators:
C# => intermediate
JavaScript => intermediate
intermediate => C#
intermediate => JavaScript
Putting the output from one, into the next, you can translate from one language to another:
C# => intermediate => JavaScript
JavaScript => intermediate => C#
Unfortunately the code generated will probably not be human-friendly.
来源:https://stackoverflow.com/questions/29193069/how-do-i-start-writing-a-transpiler-is-it-even-possible