开发者

What are modern and old compilers written in?

开发者 https://www.devze.com 2022-12-25 16:00 出处:网络
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.

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?


Most compilers are written in the programming language they target (bootstrapping).

There are of course numerous exceptions.


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


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


Compilation is one of the most computationally intensive things you can do on a computer or as Joel Spolsky puts it:

Writing code in a compiled language is one of the last things that still can't be done instantly on a garden variety home computer.

Hence you wan't the compiler to be as fast as possible which makes C and C++ natural choices.


There's a native Python compiler for Python called pypy.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号