What are modern and old compilers written in?

2019-04-01 02:28发布

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?

7条回答
该账号已被封号
2楼-- · 2019-04-01 02:37

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.

查看更多
冷血范
3楼-- · 2019-04-01 02:47

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

There are of course numerous exceptions.

查看更多
爷、活的狠高调
4楼-- · 2019-04-01 02:49

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楼-- · 2019-04-01 02:55

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

查看更多
迷人小祖宗
6楼-- · 2019-04-01 03:01

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

查看更多
三岁会撩人
7楼-- · 2019-04-01 03:04

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

查看更多
登录 后发表回答