问题
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?
回答1:
Most compilers are written in the programming language they target (bootstrapping).
There are of course numerous exceptions.
回答2:
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++.
回答3:
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).
回答4:
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.
回答5:
There's a native Python compiler for Python called pypy.
回答6:
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.
回答7:
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.
来源:https://stackoverflow.com/questions/2544106/what-are-modern-and-old-compilers-written-in