Transpiler : A compiler for Source to Source compilation.

DIKSHA MALI
4 min readJan 1, 2021

--

Has there been a need to learn a new programming language just for a small part of your project? Very often isn’t it? Or despite being an expert in a programming language you are forced to write in another one? This mostly happens when you are working in a team. Lets say , you are very much familiar with javascript, but you need to deploy a web service quick so you pick flask and hence need python equivalent of your code.

The simplest solution would be to learn new language from scratch. But is it feasible? This would be feasible only if you see yourself using the language frequently in future. But what if it’s a one-time requirement or you don’t have enough time? This is where transpilers come in picture!

What is a Transpiler?

A source-to-source translator or transpiler or transcompiler converts between programming languages that operate at approximately the same level of abstraction i.e. same level of complexity. Basically, what a transpiler does is, it takes one programming language as input which is the source language and , and gives an output as another programming language of same level which is the target code.

Transpiler Vs Compiler

The working of compiler is pretty state forward, it take high level code as input , scans it as whole translate and gives an output as lower level equivalent code, while transpiler just convert langauges from one to anotehr on same compelxity level . Following diagram lists where some of our more popular programming languages fall in this high-level vs. low-level spectrum explaining compilation and transpilation.

Compiler VS Transpiler

Working of Tranpiler

The transformation is not performed directly on the code but on the a representaion of the code which is the abstarct syntax tree(AST)

1. Parsing stage: obtain a AST using a parser from source code done by using parser generators like ANTLR

2. Transformation stage: we will transform in one or more steps the AST of the original language into the corresponding AST of the target language. The number of steps depend on how similar the source language and target language are. More similarity lesser steps

3. Generation stage: generating target code from transformed AST using code generators

A transpiler either can keep the structure of target code as close as possible to the source code , or do completely opposite of this and the target code would not look like source code depending on the frequency of work where it finds itself in use

Why Transpiler?

· Migration: Migrate legacy code to a modern language.

· Compatibility: Generate a code of newer version from older version for better features. Eg: Python 3 from python 2. Or a code conforming older version while from a newer version.

· Coding Skills: One can choose the source code according to their preference to convert into same target code. There is more flexibility For example, those who are from OOP background prefer TypeScript. Python coders prefer CoffeeScript instead. In both cases, code is transpiled to JavaScript.

Performance: the target language has a better compiler that can generate more optimized code.

Examples of Transpilers:

J2CL allows seamless use of Java in your JavaScript applications. one in every of the items that makes J2CL distinctive is: it provides you the entire freedom of choice! you’ll be able to use J2CL to simply make some Java code accessible from JavaScript or go all the thanks to create an entire application with it; no matter most closely fits your desires.

HipHop takes your PHP source code and converts it to a C++ equivalent, then compiles the generated C++ code and runs it within an embedded HTTP server. Similarly,

• Cfront: C++ to c source code.

• Babel: TypeScript to JavaScript.(Typescript transpiler converts Typescript code to JavaScript.)

• J2ObjC: Java to objective-C. etc

There are many more transpilers which are very useful and are used oftenly.

Conclusion

Transpilers can be a powerful class of tools in the world of development. Many organizations and developers are not really aware that it is possible to write transpilers. Writing a good transpiler is just as much work as writing a good compiler, because they basically do the same thing. Most of those who do know that this is possible have no idea how to get started. In this blog, we tried to explain what are they, when should they have used, and how should they be designed.

--

--