Compiling vs Transpiling

Compiler ConstructionAbstractionTranspiler

Compiler Construction Problem Overview


While searching about the difference, I came across these definitions:

Compiling is the general term for taking source code written in one language and transforming into another.

Transpiling is a specific term for taking source code written in one language and transforming into another language that has a similar level of abstraction.

I understand what Abstraction is.

But what does "similar level of abstraction" mean in the above definition? And how do we find the level of abstraction in a language?

Compiler Construction Solutions


Solution 1 - Compiler Construction

The definition you have quoted above is too general for a beginner to understand completely and so let me just simplify it to something we see practically.

Compiler: is an umbrella term to describe a program that takes source code written in one language and produce a (or many) output file in some other language. In practice we mostly use this term to describe a compiler such as gcc which takes in C code as input and produces a binary executable (machine code) as output.

Transpilers are also known as source-to-source compilers. So in essence they are a subset of compilers which take in a source code file and convert it to another source code file in some other language or a different version of the same language. The ouput is generally understandable by a human. This output still has to go through a compiler or interpreter to be able to run on the machine.

Some examples of transpilers:

  1. Emscripten: Transpiles C/C++ to JavaScript
  2. Babel: Transpiles ES6+ code to ES5 (ES6 and ES5 are different versions or generations of the JavaScript language)

Now, what do they mean by "similar level of abstraction": As I said it compiles/transpiles to a source file, one can argue that assembly language is also a source file and thus gcc is also a transpiler. So, this argument is what this similar level of abstraction voids.

The notion of categorizing languages into lower, middle and higher level is based on the level of abstraction they provide from the actual working of the machine/architecture.

Lower level languages like assembly are very close to the processor architecture i.e. have different instructions for different processors. While C/C++/Java/JavaScript, abstract all this away providing more abstraction.

So, a transpiler compiles to a language that is closer to the language you started with in the terms of this abstraction (or is closer to the level of that language in the lower-middle-higher level language ladder).

Solution 2 - Compiler Construction

Here's a sort of descriptive way to answer

If you think of layers of abstraction as this example:

(1) CPU-level (actual logic gates on the CPU)
(2)machine code
(3)assembly code
(4)[C/C++, JVM/bytecode]
(5)[JavaScript, Python]

A compiler goes to a lower level (lower number). A transpiler switches from one language (or version of a language) to another at the same number.

Solution 3 - Compiler Construction

Ex: TypeScript ( a Microsoft superset of JavaScript with type safe checking) transpiles to JavaScript code which can run on different types of browsers.

https://en.wikipedia.org/wiki/Microsoft_TypeScript "Microsoft TypeScript is an open-source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript, and adds optional static typing to the language.

TypeScript is designed for development of large applications and transcompiles to JavaScript.[5] As TypeScript is a superset of JavaScript, existing JavaScript programs are also valid TypeScript programs. TypeScript may be used to develop JavaScript applications for both client-side and server-side (Node.js) execution."

Solution 4 - Compiler Construction

I mostly agree with tapananand answer, but...


definition

Words are "made", so they serve a purpose. And this also changes with time.

We now tend to use transpiler to specify a compiler that translates code into some other code "more similar" to the source one, that what a compiler might do. And is used to differentiate both mostly when both of them are mentioned in the same context (again mostly implying that a transpile language will have to get compiled at least once more)


examples

So everything is very subjective. At the time of this writing:

  • Coming from the Java world I could call CoffeeScript/TypeScript transpilers to depict that the resulting code is no more efficient than the original one.
  • CoffeScript documentation says it's a compiler, and babel is a transpiler. The want do say that CoffeeScript, although very similar, is not Javascript. At least not a version of it, as that is what babel produces.
  • Babel calls itself a compiler.

fazit

So transpile is at this time very rarely used anymore, and only to tell two compilers apart.

It will probably dissapear as concept, since compilation is much more complicated than that (same/higher/lower language, version, etc), and the word doesn't seem to be useful anymore ("transpilers" are now ubiquitous)

Solution 5 - Compiler Construction

Meaning of "similar level of abstraction"

Higher level languages are more abstracted than lower level languages. By abstracted I mean easier to understand.

Higher level language(highly abstracted)---- Similar level -----> Higher level Another language(highly abstracted)
(TypeScript Source code to JavaScript Source code)

Mid level language(poorly abstracted) ---- Similar level-----> Mid level language(poorly abstracted)

Higher level language(highly abstracted) ---- Not Similar level of abstraction -----> Mid level Another language(poorly abstracted)

Solution 6 - Compiler Construction

Computers only understand 1s and 0s: You can think of computer as being a box, crammed full of switches - kinda like a light switch: they can either be "on" or "off" - they can either be a "1" or a "0". You can instruct a computer which switches you want on or off, with a series of "1s" and "0s".

People need programming languages: Human beings are not very good at reading/understanding hundreds of thousands of lines of "1s" and "0s"........ but we can be trained to understand human words: like: class and break.

Programming languages allow us to write computer instructions, which can be translated into 1s and 0s (compiling), or which can be translated into other programming languages. for example: if you have elm code, you can easily transform that into javascript (transpiling).

Solution 7 - Compiler Construction

Compilers are used to compile source code written in one language to another. A compiler will often use the same syntax as the input language, but it may also be designed for some other type of input, like bytecode.

Transpilers are different because they translate code from one programming language to another. The original program may have been written in JavaScript, but the transpiler converts it into HTML.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNishi MahtoView Question on Stackoverflow
Solution 1 - Compiler ConstructiontapananandView Answer on Stackoverflow
Solution 2 - Compiler ConstructionAraymerView Answer on Stackoverflow
Solution 3 - Compiler ConstructionBurakView Answer on Stackoverflow
Solution 4 - Compiler ConstructionestaniView Answer on Stackoverflow
Solution 5 - Compiler ConstructionYadab SdView Answer on Stackoverflow
Solution 6 - Compiler ConstructionBenKoshyView Answer on Stackoverflow
Solution 7 - Compiler ConstructionAnvesh kumarView Answer on Stackoverflow