In which language is the C# compiler written?

C#Compiler ConstructionRoslyn

C# Problem Overview


I looked at the source code at http://referencesource.microsoft.com/, and it appears all the source code is in C#.

I also looked at the source code for the new C# compiler platform (Roslyn), and it is also in C#. How is that possible? Is C# language compiler written in C#? Or am I missing something obvious? If C# compiler is written in C# then how does it work?

C# Solutions


Solution 1 - C#

The original C# compiler wasn't written in C#, it was in C and C++. The new Roslyn compiler was written in C#, but was initially compiled with the old compiler. Once the new compiler was done, it was able to compile its own source code: this is called bootstrapping.

Solution 2 - C#

Compilers are utility programs - they turn programming language text into machine code. If the programming language describes software that just happens to be a compiler.....

Compilers can also produce machine code for other architectures. For example, Apple compiles iOS using racks of Intel-based servers. The compiler does not have to run the ARM code it generates, just write it to disk.

Compiler 2.0 must be written in a language compiler 1.0 can process, but it can certainly create compiler 2.0 with newer features like optimization. You can then re-compile the source code using compiler 2.0 and make a better version of itself. Again, the compiler doesn't know it's making another version of itself.

If we go far enough back into the mists of time then we do reach a point where we have no compiler - the very first iteration of a high-level language. Then we have to get out the pencils and opcode books and write the first one in assembly. How did we write the first assembler? Direct machine code entry, probably on punched paper tape, or flipping switches on the front panel.

Solution 3 - C#

A compiler is just a program like any other program. There is nothing magical or special about it. It takes some input and produces some output. In this particular case, the input just happens to be C# and the output just happens to be CIL, but that's no different from the input being a series of tax returns and the output being a report.

Solution 4 - C#

You write a language in whatever is available and create a new compiler for it. Now this program we can call it C# Compiler V 1.0 is able to read and compile any C# code with current set of reserved words. Now, you say, well I want to introduce a new feature that did not exist before, like where statement. Ok, you use C# Compiler V 1.0 which obviously does not have where statement anywhere and compile a code into a new version C# Compiler V 2.0.

You may ask here: but wait, there is no where statement in C# Compiler V 1.0. Now, a compiler is such beast that it does a very specific job for which you do not need more than 20% of what C# can offer anyway. Sure, it is sometimes tricky to think about new features like yield, but unless yield is expressed in simpler terms, you would not be able to implement it easily anyway regardless of what compiling language you use.

Once your C# Compiler V 2.0 is created, even though you do not need where statement and it is maybe not even used anywhere in the Code for C# Compiler V 2.0, you would still recompile it with your new compiler and this C# Compiler V 2.0 produced from the Code for C# Compiler V 2.0 by C# Compiler V 2.0 is your New C# Compiler V 2.0 compiler.

Before you do this since your new compiler can understand new syntax you are entitled to adjust the compiler code itself and add anything that can be compiled into it, if you think that it will improve anything. However, it is a small chance that a new syntax can improve the compiler itself.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCriketerOnSOView Question on Stackoverflow
Solution 1 - C#Thomas LevesqueView Answer on Stackoverflow
Solution 2 - C#paulView Answer on Stackoverflow
Solution 3 - C#Jörg W MittagView Answer on Stackoverflow
Solution 4 - C#alex.peterView Answer on Stackoverflow