现代和旧的编译器是用什么编写的?
作为编译器(而不是解释器),只需要翻译输入而不运行它,其本身的性能应该不会像解释器那样有问题。 因此,您不会用 Ruby 或 PHP 编写解释器,因为它太慢了。
但是,编译器呢?
如果您用脚本语言编写一个编译器,甚至可能具有快速开发的特点,您可能会将源代码和初始开发时间减少一半,至少我是这么认为的。
可以肯定的是:我所说的脚本语言是指具有典型特征的解释性语言,这些特征可以使程序员的编程更快、更容易、更愉快,至少通常是这样。示例:PHP、Ruby、Python,也许还有 JavaScript,尽管这对于编译器来说可能是一个奇怪的选择
编译器通常是用什么编写的?我想你会用一些低级的东西来回应,比如 C、C++ 甚至汇编器,为什么?
是否有用脚本语言编写的编译器?
使用低级或高级编程语言进行编译器编写的(缺点)优点是什么?
As a compiler, other than an interpreter, only needs to translate the input and not run it the performance of itself should be not that problematic as with an interpreter.
Therefore, you wouldn't write an interpreter in, let's say Ruby or PHP because it would be far too slow.
However, what about compilers?
If you would write a compiler in a scripting language maybe even featuring rapid development you could possibly cut the source code and initial development time by halv, at least I think so.
To be sure: With scripting language I mean interpreted languages having typical features that make programming faster, easier and more enjoyable for the programmer, usually at least. Examples: PHP, Ruby, Python, maybe JavaScript though that may be an odd choice for a compiler
What are compilers normally written in? As I suppose you will respond with something low-level like C, C++ or even Assembler, why?
Are there compilers written in scripting languages?
What are the (dis)advantages of using low or high level programming languages for compiler writing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
大多数编译器都是用其目标编程语言编写的(引导)。
当然也有很多例外。
Most compilers are written in the programming language they target (bootstrapping).
There are of course numerous exceptions.
大多数编译器都是用 C 或 C++ 编写的。即使在今天,编译器的性能仍然很重要。当您必须编译一个 900 个文件的项目时,如果需要 2 分钟或 20 分钟,那就有天壤之别了。
有些编译器是用脚本语言编写的(我想到的一个例子是 Pyjamas - 一种从 Python 到 Javascript 的编译器,用 Python 编写),但绝大多数工业级编译器都是用 C 和 C 语言编写的。 C++。
Most compilers are written in C or C++. Even today, the performance of a compiler matters. When you have to compile a 900-file project, it makes a hell of a difference if it takes 2 minutes or 20 minutes.
Some compilers are written in scripting languages (one example that comes to mind is Pyjamas - a compiler from Python to Javascript, written in Python), but the vast majority of industrial-strength compilers are written in in C & C++.
它们大多是用相当高级的语言(C/C++)编写的。然而,对于现代硬件来说,使用托管语言(C#/Java)、函数式语言(Haskell)或者更好的是托管函数式语言(Nemerle)编写的编译器是完全可以的。
函数式语言受益于一种名为模式匹配的技术,该技术使得处理解析树/AST 变得更加简单。
真正的编译器是为该特定语言编写一个编译器(这个过程称为 bootstrapping )。
They're mostly written in a reasonably high-level language (C/C++). However, with modern hardware it's perfectly fine to have a compiler written in managed language (C#/Java), in functional language (Haskell) or, better yet, managed functional language (Nemerle).
Functional languages benefit from a technique called pattern matching, which makes handling parse trees/ASTs much simpler.
The real compiler-fu is writing a compiler for a language in that particular language (a process called bootstrapping).
编译是您可以在计算机上执行的计算量最大的事情之一,正如 Joel Spolsky 所说:
因此,您不希望编译器尽可能快,这使得 C 和 C++ 成为自然的选择。
Compilation is one of the most computationally intensive things you can do on a computer or as Joel Spolsky puts it:
Hence you wan't the compiler to be as fast as possible which makes C and C++ natural choices.
Python 有一个原生的 Python 编译器,名为 pypy。
There's a native Python compiler for Python called pypy.
有一些专门的编程语言可以有效地实现编译器,例如:
http://www.meta-alternative。 net/mbase.html
另外:Irony、JetBrains MPS 等等。
一般来说,函数式语言在这方面非常高效,尤其是具有代数数据类型、模式匹配柯里化的语言,例如 Haskell、ML(F#、OCaml)、Nemerle、Scala。
There are specialised programming languages for implementing compilers efficiently, e.g.:
http://www.meta-alternative.net/mbase.html
Also: Irony, JetBrains MPS, and some more.
Functional languages in general are quite efficient in this area, especially languages with algebraic data types, pattern matching an currying, for example - Haskell, ML (F#, OCaml), Nemerle, Scala.
SUN/Oracle JVM 的 javac 编译器是用 Java 编写的;正如在 Eclipse IDE 中使用的 Java 编译器,用于在编辑时进行后台编译。许多函数式语言的编译器通常是用该语言编写的,因为函数式语言通常非常适合编写编译器。受限语言(例如,GLSL/OpenCL 等 GPU 编程)的编译器不会以 GPU 上可执行的语言编写。
一个基本问题是,由给定编译器编译的语言可能不是实现编译器的好语言;我不知道有人用 FORTRAN 编写 FORTRAN 编译器。
本质上,编译器的实现语言可能是也可能不是该编译器的输入语言,具体取决于所涉及语言的适用性以及开发时间、所需运行时性能、工具可用性和开发人员熟悉程度等许多其他标准。
The javac compiler from the SUN / Oracle JVM is written in Java; as is the compiler of Java used within the Eclipse IDE for the background compilation as you edit. Compilers for many functional languages are often written in that language, as functional languages are typically quite suited to writing compilers. Compilers for restricted languages (e.g. GPU programming such as GLSL/OpenCL) will not be written in languages executable on GPUs.
One fundamental issue is that the language compiled by a given compiler may not be a good language for implementing a compiler; I don't know of anyone writing compilers for FORTRAN in FORTRAN.
In essence, the implementation language of a compiler may or may not be an input language to that compiler, depending on the suitability of the languages involved and a host of other criteria from development time, required runtime performance, tool availability and developer familiarity.