编译与转译 [英] Compiling vs Transpiling
问题描述
在搜索差异时,我发现了以下定义:
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.
我理解抽象是什么.
但是在上面的定义中类似的抽象级别"是什么意思?我们如何找到语言的抽象级别?
But what does "similar level of abstraction" mean in the above definition? And how do we find the level of abstraction in a language?
推荐答案
您在上面引用的定义过于笼统,初学者无法完全理解,所以让我将其简化为我们实际看到的内容.
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.
编译器: 是一个总称,用于描述一个程序,该程序采用以一种语言编写的源代码并以其他语言生成(或多个)输出文件.在实践中,我们主要使用这个术语来描述一个编译器,比如 gcc,它以 C 代码作为输入,并产生一个二进制可执行文件(机器代码)作为输出.
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.
一些转译器的例子:
- Emscripten:将 C/C++ 转换为 JavaScript
- Babel:将 ES6+ 代码转换为 ES5(ES6 和 ES5 是 JavaScript 语言的不同版本或代)莉>
- Emscripten: Transpiles C/C++ to JavaScript
- Babel: Transpiles ES6+ code to ES5 (ES6 and ES5 are different versions or generations of the JavaScript language)
现在,类似的抽象级别"是什么意思:正如我所说,它编译/转译为源文件,人们可以争辩说汇编语言也是源文件,因此 gcc 也是转译器.所以,这个论点是这种相似的抽象层次所没有的.
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.
像汇编这样的低级语言非常接近处理器架构,即不同的处理器有不同的指令.而 C/C++/Java/JavaScript,将所有这些抽象出来提供更多的抽象.
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).
这篇关于编译与转译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!