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

I am exposed to C due to embedded system programming, and I find this to be one of the great languages ​​in this area. However, why is it used to write compilers? If the reason gcc is implemented in C / C ++ is because there are not many good languages ​​at the time, there is no excuse for why clang uses the same path (using C / C ++).

Is it for performance reasons? Mostly interpreted languages ​​are slightly slower compared to compiled languages, but I think the difference is almost negligible in CoffeeScript (JavaScript) due to Node.js.

From the point of view of developers, I believe that it is much easier to write a single compiler using high-level languages. Unfortunately, most compilers are written there in C / C ++. Is it just due to outdated code?

Reply to comments:

  • Bootstrapping is just one way to show that this language is powerful enough to write a single compiler. This should not be the main reason why we choose the language for implementing the compiler.

  • I agree with the assumption below that "most compiler developers will answer because most compiler-related tools (bison, yacc) emit C code." However, neither GCC nor Clang used the created parser; they themselves implemented it. This external process is independent of targeting architecture and should not be strong C / C ++.

  • There is more or less consensus that performance is one of the key factors. Indeed, even for GCC and Clang, creating a reasonable size C project (Linux kernel) is time consuming. This is due to the interface or the external interface. I must admit that I did not have much experience working with back-end compilers, since we completed the compiler course with the generated LLVM code.

+4
source share
5 answers

I am exposed to C due to embedded system programming, and I think this is one great language in this area.

Yes. This is better than Java.

However, why is it used to write compilers?

This question cannot be answered without asking the developers. I suspect most of them will tell you that ordinary compiler writing software (yacc, flex, bison, etc.) generates C code.

If the reason for gcc is that there are not many good languages, there is no excuse for the clan.

GCC is not a programming language, nor is it a Clang. They are both implementations of the C programming language.

Is it for performance reasons?

Do not confuse an implementation with a specification. Speed ​​is an attribute introduced by your compiler and computer, not a programming language. GCC creates fairly efficient machine code that can influence developers to use C as their main programming language ... but in ten years, it * can be such that node.js is more efficient machine code than GCC. Don't forget that Stackoverflow is forever.

* could, but most likely will not. See Ira Baxters comment below for more information.

Mostly interpreted languages ​​are slightly slower compared to compiled languages, but I think the difference is almost negligible in CoffeeScript (JavaScript), due to node.js.

Similarly, interpretation or compilation is not a choice of language, but an implementation of the language. For example, GCC and Clang choose to compile C into machine code. Ch and CINT are two interpreters that translate C code directly to behavior, and not to machine code. Java was once primarily translated using interpretation, but now it is mostly compiled into JVM bytecode. Javascript seems to be phasing into the prevailing compilation. Who knows? You may see compilers written primarily in Javascript in ten years ...

From the point of view of the developers, I believe it is much easier to write a single compiler using high-level languages.

All of these programming languages ​​are technically high. They are mainly defined in terms of an abstract machine; They are, of course, not low.

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

I do not find it annoying that C ++ is used for writing software; This is not a bad programming language.

Is this just due to outdated code?

I believe that outdated code may affect a programmer’s decision. In the end, although, as I said, you have to ask the developers. They may simply decide to use C or C ++ because C or C ++ is their favorite programming language ... Why do you speak English?

+9
source

Compilers are very complex software in general. The front end is pretty simple (parsing), but the backend part (planning, code generation, optimization, register allocation) is associated with NP-complete problems (of course, compilers try to approximate solutions to these problems). Thus, a C implementation will help compile time. C is also very good at bitwise operators and other low-level materials, which is useful for writing a compiler.

Note that not all compilers are written in C, though. For example, the Haskell GHC compiler is written in Haskell using the bootstrap method.

Javascript is asynchronous, which is not suitable for writing a compiler.

+3
source

I see many reasons:

  • Javascript has no elegant way to handle bit-accurate code.
  • You cannot easily write binaries in Javascript, so part of the compiler assembly must be in a lower level language.
  • The huge base of JS code is very heavy to load into memory (this simple text, remember?)
  • Writing optimizing routines for compilers requires a processor intensively, which is not yet very compatible with Javascript
  • You cannot compile your compiler with it (bootstrap) because you need a Javascript interpreter for your compiler. The bootstrap phase will not be "clean":
    JS Compiler compiles NodeJS -> NodeJS runs your new Compiler -> new JS Compiler
+3
source

gcc is mainly implemented in C, but this does not apply to all compilers, including some of them that are quite standard. This is a generic compiler template that must be implemented in the language that it compiles. ghc is written primarily in Haskell. In recent versions of guile , a compiler is used, implemented mainly on the circuit.

+2
source

nope, coffeescript, etc. are still much slower than the original compiled (and optimized) C code. Even if you take a subset of javscript that can be optimized (asm.js), it is still twice as slow as native C.

What do you hear when people say that node.js is as fast as the C code, it means it is as fast as part of a common system that does other things like reading from disk, wait for data from the network, etc. In these systems, the CPU is not used enough (especially with today's ultrafast processors), so the performance problem is not an untreated ability to process the language. Therefore, the node.js server is just as fast as the C server if they both get stuck waiting for a network call to return data. The type of system written in node.js waits a lot for the network, so people use node.js. The type of system written in C does not match the spelling in node.js

+1
source

Source: https://habr.com/ru/post/1487308/


All Articles