Reputation: 9
I am not exactly what you would call familiar with programming, so I guess I am trying to think of this more conceptually.
So I know there are already posts about this:
what is the initial step to write a bootstrapping compiler for a new language?
Writing a compiler in its own language
How can a language's compiler be written in that language?
However, I am specifically wondering about the first step here (see answer for first link):
- Write a compiler for the kernel of X in a different language.
How is this actually done? Like, how is the compiler for the lanuage you want to write actually represented in the compiler in the other language? Is it literally writing the compiler in the language you want translated directly into the other language, or is this compiler in the other language instructions on how to translate what it says into the compiler language you want? I'm finding it hard to wrap my head around this.
Sorry if I cannot describe this in a better way - I just want to try and understand how this is possible, and I cannot find more specific descriptions of this process outside of just "you write the compiler for language X in language Y," which doesn't tell me what the contents of this compiler are such that it can then be used for compiling a compiler in language X.
This might be better for a "eli5" somewhere else, but I wanted to try here first since I imagine there are people who know more about this on this site.
I have looked at a lot of different textbooks, but they largely say what I have mentioned above - not actually delving into what the "bootstrapping compiler" contents actually look like, again being a simple "you write compiler for language X in the language of Y."
In specific, I skimmed books like "Modern compiler design" by Dick Grune et al.; "Compilers: Principles, Techniques and Tools" by Alfred Aho et al.; "Compiler Construction" by K.V.N Sunitha.
Upvotes: 0
Views: 256
Reputation: 3881
You write a compiler in, for instance C, that can compile your language, X, into machine code. This is a fully functional compiler for X and needs the same parts as any other compiler. The difference is that you can cut corners when implementing the bootstrapping compiler. The parser only needs to recognize the constructs used in the compiler written in X, not the full language. The code generator can be naive and written for correctness only, optimizations can be skipped.
This compiler is then used to compile a base version of the compiler written in X into machine code. This base version will be bulky and inefficient, but this is not important since the base version is only used to build the first real X compiler and can then be scrapped.
Upvotes: 1