65

During a ReactJS session that I was attending, the presenter used a term transpiler for some code conversion/porting happening. In the past, I have heard the terms compiler and interpreter. I've used in the context of converting a user written language code to a runnable form on a computer system. Transpiler is new to me. How is a transpiler different from a compiler or an interpreter and why is it needed?

RBT
  • 24,161
  • 21
  • 159
  • 240
  • 9
    You're right - transpiler is a redundant term, and should never be used. Any compiler is a "transpiler". – SK-logic Aug 31 '16 at 11:09
  • 7
    Dear downvoter - If you can leave an appropriate feedback/comment then I can take an appropriate action to improve the post. – RBT Apr 23 '18 at 06:30

7 Answers7

54

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"
Jim M
  • 4,261
  • 1
  • 18
  • 15
  • Quick question, if I create a language using C++, and I turn that language's source code into C++ as part of my "compiler", would that really just be transpiling? – Alex Aug 30 '20 at 22:44
  • Where do you see Python fit in this interpretation? Python compiles source code into bytecode hen executes using a bytecode interpreter. Given all of this, I see Python at the same level as Java: a compiler. – Tyson Jun 09 '22 at 21:54
35

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.

Ajay
  • 18,086
  • 12
  • 59
  • 105
22

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.
Agustin Meriles
  • 4,866
  • 3
  • 29
  • 44
fuma
  • 5,067
  • 4
  • 35
  • 39
  • What about SQL to java code? is it a compiler or transpiler? from the definition above it sounds to me like a compiler. isn't it? – user1870400 Aug 27 '19 at 08:07
  • @user1870400 My opinion here for SQL is to use the the term "code generation" or "migration". Thats what you are likely to here in case of SQL and such. But depends on what exact you might want to do. I'd say SQL is not a programming language but a "query language" (hope no one is going to rost me for that statement). Also when you think about OpenAPI/swagger you can generate code out of a definition format. That's probably not what should be called compiled but "generated". – fuma Sep 02 '19 at 10:20
10

I've been building such tools since the 1980s.

We called them "Source to source program transformation systems".

That term served fine, AFAICT, for about 45 years. The idea goes back far before that; see Val Schorre's 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.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • 1
    Agreed. I built one in 1979. It is essentially just a compiler with a different kind of target language. – user207421 Sep 01 '16 at 01:49
  • 25
    And computer used to be called a "difference engine". Mouse used to be called "X-Y position indicator for a display system". A predecessor to cars was called "fire engine for transporting wagons and especially artillery". It is in the nature of language to shorten things: see Zipf's Law and linguistic economy. "It was good enough for us back in the day, these youngsters..." is rubbish. It's called transpiler because "source-to-source transforming compiler" is too long for everyday use, now that transpilers *are* everyday use (TypeScript/CoffeeScript/ES6 and SCSS being the prominent examples). – Amadan Sep 01 '16 at 04:09
  • 8
    Your examples are all "this is the clumsy phrase we used when we only had a few"; it wasn't long before the standard terminology was established and people have not invented replacement terms for mouse. My point is this technology has been with us 50+ years. "Transformation System" is the way one shortens the longer phrase if you want linguistic economy. And these have been in everyday use in many areas of computing; perhaps just you didn't notice them. Having TypeScript appear doesn't suddenly change the concept, and cause a need for a new term. You sound to me like one of those priests. – Ira Baxter Sep 01 '16 at 08:26
  • 5
    @Amadan You aren't even accurate. Babbage's device was called an Analytic Engine. Its predecessor was called a difference engine, but it wasn't a computer. This was 1837. The term 'computer' as presently understood has been in use for seventy years, – user207421 Sep 01 '16 at 22:53
  • 1
    Whether my exact examples are correct or not is irrelevant. I just wanted to point out that it is not a conspiracy, it is just language being language - ask any linguist. Language changes, new words come into existence all the time, and survive if they manage to be useful to a sizable enough chunk of the population. "Priests"? Give me a break. The issue with TypeScript is not that it is a new concept; but that the concept broke out from the micropopulation it was in before, and spread to masses: that's what drives the linguistic change. – Amadan Sep 02 '16 at 00:54
  • 6
    @Amadan I don't think any of your examples are accurate actually, and as your argument is fundamentally anecdotal it stands or falls by your examples. It is not in dispute that language is dynamic. The question is whether we need a new term for an existing concept. Negative reaction to language change is just as legitimate a force in language evaluation as innovation: a point that linguists invariable overlook. – user207421 Sep 07 '16 at 13:01
  • They overlook it because natural language changes NATURALLY. The whole point is that the masses decide, not the minority programmers from 1979. Believe me it's better to embrace it and be content rather than spend your time and effort debating the frivolous. – Phil Jun 20 '17 at 06:20
  • I get my vote too about how the langauge evolves, but it doesn't get counted if I don't complain. – Ira Baxter Jun 20 '17 at 16:27
  • 6
    Non of @Amadan examples are correct actually. Was rooting for him, even the mouse was not named that, it was "described" as that. https://en.wikipedia.org/wiki/Douglas_Engelbart And Ira Baxter, checked out your linkedin profile. You're a sage. Much respect. – Cozzbie Oct 04 '17 at 16:19
  • 3
    @Cozzbie: You're quoting Wikipedia. I'm quoting [the actual patent](https://www.google.com/patents/US3541541) that Engelbart filed. But again, and I stand by it: whether my specific examples are correct or not is irrelevant. I only reacted to note how I would respect this answer, and the huge experience behind it, so much more if it resisted the urge to diss people for no reason other than using language differently than him. The last paragraph of the answer is ugly and beneath him; it is not objective, and not worthy of being a StackOverflow answer. – Amadan Oct 05 '17 at 06:54
7

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.
shaahiin
  • 1,243
  • 1
  • 16
  • 26
3

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.

Valera Grishin
  • 441
  • 3
  • 9
3

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
RBT
  • 24,161
  • 21
  • 159
  • 240
Jeyanth
  • 531
  • 2
  • 6
  • 19