Compiler vs Interpreter vs Transpiler

Compiler ConstructionLanguage AgnosticTerminologyInterpreterTranspiler

Compiler Construction Problem Overview


During a reactJS session that I was attending, the presenter used a term transpiler for some code conversion/porting happening. I've always used and heard the terms compiler and interpreter when it comes to converting a language code to a runnable form on a computer system/machine. Transpiler is new to me. How is a transpiler different from a compiler or an interpreter and why it is really needed?

Compiler Construction Solutions


Solution 1 - Compiler Construction

Compiler - compiles code to a lower level code.

Example:

  • "Developer code" -> "Machine code"
  • PHP -> C
  • Java -> bytecode

Transpiler - compiles code to same level of code/abstraction.

Example:

  • "Developer code" -> "Another developer code or version"
  • JavaScript ES2015+ -> JavaScript ES5

Interpreter - interprets code, not really in the same class/league/context with the two above.

Example: php.exe

  • "Your PHP code/scripts inside index.php" -> "Results to html or just like pure index.html"

Solution 2 - Compiler Construction

As is mentioned in this Wiki article, it is a type of compiler which translates source code from one programming language to another programming language. The source code might be in some language no longer used, or doesn't support latest hardware/software advancements, or as per programmer's convenience/favoritism.

A VB6 to VB.NET converter can be thought of as a Transpiler. I might think of COBOL to C# / C++ / Java tool as a transpiler.

Solution 3 - Compiler Construction

It is often called 'transpiling', when you translate code with JS-preprocessors like CoffeeScript, TypeScript (you name it) to plain JavaScript. But it really isn't a JS exclusive thing. It applies to all kind of programming languages. Mostly it's just called compiling.

> 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.

According to https://www.stevefenton.co.uk/2012/11/compiling-vs-transpiling/

So in your case:

  • 'compile' JSX => JavaScript (and HTML), which I think matches the definition above.
  • Therefore it can be called 'transpiling'. Though calling it 'compiling' would also be ok.

Another example:

  • CoffeeScript / TypeScript / ...whatEverScript.. => JavaScript and vice versa.

Solution 4 - Compiler Construction

I've been https://www.scribd.com/document/94698972/TMM-IEEE-1986">building such tools since the 1980s.

We called them "Source to source https://en.wikipedia.org/wiki/Program_transformation">program transformation systems".

That term served fine, AFAICT, for about 45 years. The idea goes back far before that; see Val Schorre's https://en.wikipedia.org/wiki/META_II">Meta II Compiler-compiler work for a 1963 version of this idea.

Now we have this new term; I started see it a few years ago. It adds nothing, but it sounds mysterious and cool. This is how priests establish their worthiness; they invent new vocabulary for old ideas.

Solution 5 - Compiler Construction

> A source-to-source compiler translates between programming languages that operate at approximately the same level of abstraction, while a traditional compiler translates from a higher level programming language to a lower level programming language.

Source : Wikipedia

  • Compiler - translates source code from higher level language to lower level language.
    Example: C compilers (C to machine code), javac tool of JDK (java to byte code)
  • Transpiler - a type of compiler that translates between source codes at the same level of abstraction.
    Example: Babel (ES6+ to ES5) - which you can use to write ES6 code while still supporting older browsers like IE 11 and below.

Solution 6 - Compiler Construction

By definition transpiler is a special form of translator.

Compiler converts high level source code to a code of lower level of abstraction. Typically, but not necessarily, the goal of compilation is machine code. That is, a code that can be executed directly by CPU. Compiler can also produce bytecode which is a simulation of machine code, but is later interpreted by so called virtual machine (i.e. Java bytecode for Java VM). Yet the term compiler can apply to a tool that converts the code to another programming language which is not a machine executable code. Distinctive difference of a compiler is it lowers the level of abstraction.

Translator converts the source code from one programming language to another programming language of the same or different level of abstraction. Note that result can be a machine code, if source code was also a machine code.

Transpiler is very similar to translator, but specifically converts the source code between programming languages of the same level of abstraction. Note that programming languages differ and a lot in what they abstract; differ in level of abstraction especially as it applies to different concepts they support as an abstraction. Due to that, the conversion (transpilation) is often between the similar, not the same levels of abstraction.

Solution 7 - Compiler Construction

Compiler - It acts as an interface between human and computer for converting human understanding language to machine understanding language.

Types of Compiler

  • Native Code Compiler: The compiler used to compile a source code for same type of platform only. The output generated by this type of compiler can only be run on the same type of computer system and operating system(OS) that the compiler itself runs on.
  • Cross Compiler: The compiler used to compile a source code for different kinds platform. Used in making software’s for embedded systems that can be used on multiple platforms.
  • Source to Source Compiler: Converts HLL(High Level Language) or Source Language to LLL(Low Level Language) or Machine Language.
  • Transpiler: Converts HLL (High Level Language) to another HLL

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
QuestionRBTView Question on Stackoverflow
Solution 1 - Compiler ConstructionJim MView Answer on Stackoverflow
Solution 2 - Compiler ConstructionAjayView Answer on Stackoverflow
Solution 3 - Compiler ConstructionfumaView Answer on Stackoverflow
Solution 4 - Compiler ConstructionIra BaxterView Answer on Stackoverflow
Solution 5 - Compiler ConstructionshaahiinView Answer on Stackoverflow
Solution 6 - Compiler ConstructionValera GrishinView Answer on Stackoverflow
Solution 7 - Compiler ConstructionJeyanthView Answer on Stackoverflow