WritingCompilers

ThoughtStorms Wiki

Context : ProgrammingStuff

Quora Answer : Why should compilers only be programmed in C?

Mar 4, 2019

You shouldn't write a compiler in C.

The only real requirement of a language for compilers is that it needs to be able to give you accurate byte-level control over output files.

Any language that can do that can be used to write a compiler.

There are a couple reasons that C has been popular for writing compilers in the past :

Often C is one of the first languages to get to a new platform. So if your compiler is in C, it can run on the new platform. And probably target it.

C is low-level and fast. And compilers do a lot of work. Back when computers were slower, you wouldn't have necessarily wanted to be programming in a language where the compiler itself added a lot of overhead. Programming is tedious enough without having to wait around even longer for compiles.

C programmers tend to know more about the low level details of the system architecture than people who only work in higher level languages. And obviously you do have to understand that kind of thing, because that's what the output of the compiler is targeting.

But really, if you understand the low-level machine you are targeting, then use the highest level language that can give you byte level output. Use Haskell or Ocaml or Clojure or Scheme or Prolog. Something nice and high level. To write your compiler.

Backlinks (1 items)