Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why compilers are written in C/C++ instead of using CoffeeScript (JavaScript, Node JS)?

I am exposed to C because of embedded system programming, and I think it's one wonderful language in this field. However, why is it used to write compilers? If the reason why gcc is implemented in C/C++ is that there aren't many good languages at that time, there's no excuse for why clang is taking the same path (using C/C++).

Is it for performance reasons? Mostly interpreted languages are a bit slower compared with compiled languages, but I guess the difference is almost negligible in CoffeeScript (JavaScript), because of Node.js.

From the perspective of developers, I suppose it's much easier to write one compiler using high level languages. Unfortunately, most of compilers out there are written in C/C++. Is it just because of legacy code?

Response to comments:

  • Bootstrapping is just one way to illustrate that this language is powerful enough to write one compiler. It shouldn't the dominant reason why we choose the language to implement the compiler.

  • I agree with the guess given below, that "most compiler developers would answer because most of compiler related tools (bison, yacc) emit C code". However, neither GCC nor Clang use generated parser, they implemented one themselves. This front-end process is independent of targeting architecture, and should not be C/C++'s strength.

  • There's more or less consensus that performance is one key factor. Indeed, even for GCC and Clang, building a reasonable size of C project (Linux kernel) takes a lot of time. Is it because of the front-end or the back-end. I have to admit that I didn't have much experience on backe-end of compilers, as we finished the course on compiler with generated LLVM code.

like image 774
Albert Netymk Avatar asked Nov 29 '22 12:11

Albert Netymk


2 Answers

Compilers are very complex software in general. The front end part is pretty simple (parsing), but the backend part (scheduling, code generation, optimizations, register allocations) involve NP-complete problems (of course compilers try to approximate solutions to these problems). Thus, implementing in C would help compile times. C is also very good at bitwise operators and other low level stuff, which is useful for writing a compiler.

Note that not all compilers are written in C though. For example, Haskell GHC compiler is written in Haskell using bootstrapping technique.

Javascript is async, which doesn't suit compiler writing.

like image 31
jh314 Avatar answered Dec 05 '22 02:12

jh314


I am exposed to C because of embedded system programming, and I think it's one wonderful language in this field.

Yes. It's better than Java.

However, why is it used to write compilers?

This question can't be answered without asking the developers. I suspect that the majority of them will tell you that common compiler-writing software (yacc, flex, bison, etc) produce C code.

If the reason for gcc is that there aren't many good languages, there's no excuse for clang.

GCC isn't a programming language, and neither is Clang. They're both implementations of the C programming language.

Is it for performance reasons?

Don't confuse implementation with specification. Speed is an attribute introduced by your compiler and your computer, not by the programming language. GCC happens to produce fairly efficient machine code, which might influence developers to use C as their primary programming language... but in ten years time, it could* be that node.js produces more efficient machine code than GCC. Don't forget, StackOverflow is forever.

* could, but most likely won't. See Ira Baxters comment below for more info.

Mostly interpreted languages are a bit slower compared with compiled languages, but I guess the difference is almost negligible in CoffeeScript (JavaScript), because of Node.js.

Similarly, interpretation or compilation isn't the choice of the language, but of the implementation of the language. For example, GCC and Clang choose to compile C to machine code. Ch and CINT are two interpreters that translate C code directly to behaviour, rather than machine code. Java was once predominantly translated using interpretation, too, but is now predominantly compiled into JVM bytecode. Javascript seems to be phasing towards predominant compilation, too. Who knows? Maybe you'll see compilers written predominantly in Javascript in ten years time...

From the perspective of developers, I suppose it's much easier to write one compiler using high level languages.

All of these programming languages are technically high level. They're mostly defined in terms of an abstract machine; They're certainly not low level.

Unfortunately, most of compilers out there are written in C/C++.

I don't consider it unfortunate that C++ is used to write software; It's not a bad programming language.

Is it just because of legacy code?

I suppose legacy code might influence the decision of a programmer. In the end though, as I said, you'd have to ask the developers. They might just decide to use C or C++ because C or C++ is their favourite programming language... Why do you speak English?

like image 102
autistic Avatar answered Dec 05 '22 03:12

autistic