First C++ Compiler – How Could It Be Written in C++?

ccompilerhistoryprogramming-languages

Stroustrup claims that Cfront, the first C++ compiler, was written in C++ (Stroustrup FAQ).

However, how is it even possible that the first C++ compiler be written in C++?

The code that makes up the compiler needs to be compiled too, and thus the first C++ compiler couldn't have been written in C++, could it?

Best Answer

The key is right here:

The first C++ compiler (Cfront) was written in C++. To build that, I first used C to write a "C with Classes"-to-C preprocessor. "C with Classes" was a C dialect that became the immediate ancestor to C++. That preprocessor translated "C with Classes" constructs (such as classes and constructors) into C. It was a traditional preprocessor that didn't understand all of the language, left most of the type checking for the C compiler to do, and translated individual constructs without complete knowledge. I then wrote the first version of Cfront in "C with Classes".

So the first version of Cfront wasn't written in C++, rather in the intermediate language. The ability to create C compilers and preprocessors directly in C led to many of the innovations (and massive security holes) in C. So you write your new preprosessor that turns your "C with Classes" code into straight C (because straight C can do anything) and then you use "C with Classes" to write a C++ compiler (not that you couldn't do it in C, just it would take awhile) and then you use that C++ compiler to write a more effecient/complete compiler in C++. Got it?

Related Topic