I was wondering why C++ is a good choice to write a compiler. Of course C is good for this purpose too, because many compilers are written either in C or C++ but I am more interested in C++ this time. Any good reasons ? I was looking for that in the Internet, but I cannot find any good reasons.
C++ has two sides to it. It has a low-level development side which makes it seem like a natural language for doing low level thing like code generation. It also has a high-level side (which C does not) that lets you structure a complex application (like a compiler) in a logical, object oriented way, while still maintaining performance. Because it has both the low and high level aspects to it, it's a good choice for large application which require low-level features or performance.
My experience does not agree with your premise here. In fact, for high-level general-purpose languages, it is a very common practice to write the compiler in the same language as the source language (the language being compiled). For example:
- Sun's Java compiler is written in Java
- The Scala compiler is written in Scala
- Mono's C# compiler is written in C#
- Squick's Smalltalk compiler is written in Smalltalk
- ... and many more
An exception is compiler front-ends written for existing compiler frameworks, such as GCC, LLVM or Polyglot, which are then written in the framework's language, or compilers that rely on existing parser generators such as Yacc. Since GCC, LLVM and Yacc are common, established tools written in C and C++, it gives an incentive to compiler writers to use them, which might lead to C and C++ getting a large share in the compiler implementation language distribution.
To compile what to what? A compiler transforms a source code from one language (source language) to another (destination language), which doesn't indicate anything about the low-levelness of the destination language.
C or C++ is a popular choice because of the support of compiler related tools (see the answer by Telastyn), and because those two languages allow you to go really native. But there is nothing wrong in choosing another language.
Note that in order to be more geeky, you may pick the source language to write the compiler itself. It's what happened for CoffeeScript compiler and many other compilers. It is also popular with the IDEs: one of the first Visual Studio was built using the same Visual Studio.
I tend to question the basic premise here. While C and C++ work perfectly well for writing compilers, quite a few other languages seem to work perfectly well for the task as well.
A bit depends on the language you're compiling though. For small, simple languages, C and Pascal work quite nicely. If you're going to compile something big and complex, your compiler gets big and complex too -- in which case, C++'s extra features for organizing and working with larger programs obviously come in handy. That isn't really very specific to compiling though, just features useful for larger programs in general.
I think it's also worth mentioning one other point. Beginners (seem to) think of compilers as mostly doing text manipulation, so they think something like Perl will be a massive help in writing compilers. In reality, most of the interesting parts of compilation don't really start until after you've built your AST. While I'm sure Perl can do the job perfectly well, its text manipulation capability doesn't really give it a huge advantage either (text manipulation is mostly in the lexer, and lexer generators for things like C all support REs anyway).
Compilers may be implemented in any modern language. However, one of the most important requirements from a compiler is to be fast.
C++ has a clear advantage here. Optimization in C++ does not come cheap. However, due the the low-level nature of this language, it is possible to manually optimize C++ code more than in any other language (except Assembly which is not portable).
I have experience with this matter. I have written compilers in C and C++. The main difference between C and C++ is that C does not have dynamic memory management in an automatic way. All memory management in C has to be done explicitly. Writting a compiler deals a lot with string processing and array management. In C you are forced to think about the size of every string and every array you declare and also check indexes when you access those objects (if you want your code to be safe and stable). In C you can have dynamic memory management, of course, but nothing is automatic. You have to explicitly allocate and free memory using malloc() and free(), keep the size of your dynamic objects in separated variables in order to be sure you do not access them out of boundaries.
In C++ you can have the same mechanisms but it is really development time efficient because all your memory management can be encapsulated within constructors and destructors which you do not have to call explicitly. So the compiler is allocating and freeing resources for you. The size of your dynamic objects can be encapsulated as well if you create your own classes, and indexes can be checked for boundary access by overloading operator . These abstractions help to make your code cleaner, easier to understand and debug and makes definitely development faster.
If you create a compiler in C it will take you more time for sure. C++ will make you finish your project in less time. C and C++ have same performance but C++ has a lot of advantages that C does not have.
The CompCert project is a research C compiler which is not written in C or in C++, but more in Ocaml and Coq.
Observe that C++ used to be translated to C (in Cfront). Now you may use the GCC front-end to Gimple, then dump the Gimple to some database, then write a Gimple to your assembler translator. But legal reasons (the GCC runtime library exception) requires such a compiler to be open source. Ask your lawyer for details, I am not a lawyer. Old variants of GCC have been written in C (+ several domain specific languages) with a front-end for some variant of C++. OpenWatcom could be a C++ compiler written in C (I leave you to check that).
The source of Compcert are freely available for academic and research purposes. If you want to use it industrially (and legally), you need to get some license from Absint.
If I was tasked in 2020 to write a C (or C++) compiler from scratch (running on Linux, maybe some cross-compiler) I probably won't write it in C++. I would consider writing it using Ocaml, Go, or Rust. And I could base it on Frama-C if so allowed. If required to code in C or C++ I would first code a garbage collector library for it, probably some persistence layer -very useful for whole program optimization- and then I would consider a metaprogramming approach (generating most of the C or C++ code of the compiler with my ad-hoc tools, maybe Bismon or RefPerSys if so allowed).
Notice that recent versions of GCC are technically not coded in pure C++, they are a dozen of domain specific languages involved in GCC (several of them being Turing-complete). See also my old GCC MELT project.
Look also into the MILEPOST GCC project.