Live data from Hacker News

How I wrote a self-hosting C compiler in 40 days

sigbus.info

21–30 of 128 posts

Re: How I wrote a self-hosting C compiler in 40 days

#21
post #3

Thought this was going to be an inspiration to me to continue with my pet project of writing my own little programming language. But it starts off on day 8 with him already having written a basic compiler, with no explanation of how he did any of the basics. Still interesting, just not what I thought it was.

I think the best inspiration to take here is that the best way to write a compiler in 40 days is to first write one in 400 days. From the first entry: > Implementing these features is easy because this is the second time for me.

"If you want to be a Millionaire, start with a billion dollars and launch a new airline ." -- Richard Branson

Re: How I wrote a self-hosting C compiler in 40 days

#22
I really enjoyed reading this. It's informative, fun, and has a refreshingly honest tone. Too often, stories passed around by computer scientists entail clever solutions and elegant insight striking the protagonist like lightning in the hour of need. Rarely does the programmer express regret, make self-corrections, and confront fear and doubt along the way:

>I should have written beautiful code from the beginning, but because I needed to learn by writing a working code, the rewriting was unavoidable.

>I should probably change my mind to implement all the features from end to end. I may find it fun as I'm approaching the goal. Sometimes, I have to write more code than I want to write in order to achieve a goal.

>In a tough situation like this, I probably should recall the fact that the compiler was just in one file, to see how much progress I've made in a month. It just reads an integer with scanf() and prints it out with printf(). Really, I made so much progress in one month. Yeah, I think I can do this.

Re: How I wrote a self-hosting C compiler in 40 days

#23
post #8
post #3

Thought this was going to be an inspiration to me to continue with my pet project of writing my own little programming language. But it starts off on day 8 with him already having written a basic compiler, with no explanation of how he did any of the basics. Still interesting, just not what I thought it was.

Same here. I have been trying the same project as you... and get stuck on the grammar, every.single.time. Are you done with that part yet?

The C grammar is printed in BNF in the back of K&R and available in machine-readable form in the standards.

Re: How I wrote a self-hosting C compiler in 40 days

#25
post #20

Now repeat for C++.

Considering that a considerably large subset of C is valid C++, it should be easy to modify 8cc to be valid C++ (e.g. by replacing implicit void*-casts to explicit ones), and then you already have a "self-hosting C++ compiler", but that could be considered cheating by some...

Re: How I wrote a self-hosting C compiler in 40 days

#26

Long ago UNIX had compiler writing tools like yacc and lex. I wonder if they are useful for exercises like this.

For writing a general compiler (or anything similar to that), they're extremely useful because they produce very good lexers and/or parsers. The GNU versions of those two are 'bison' and 'flex'. Really, just about anything that requires parsing text can gain from using both of them.

Noting that though, for this specific exercise they're not as useful because the author intended for this compiler to be self-hosting. It would be hard to be self-hosting if the compiler have to be able to compile the C code from yacc or lex, which may do any number of strange things.

Re: How I wrote a self-hosting C compiler in 40 days

#27

Long ago UNIX had compiler writing tools like yacc and lex. I wonder if they are useful for exercises like this.

"Particularly, I'd use yacc instead of writing a parser by hand and introduce an intermediate language early on." Near the end of the article.

Re: How I wrote a self-hosting C compiler in 40 days

#28
For anyone interested in compiler writing and looking for a good resource to start, probably one of the best is the "Dragon Book":

http://www.amazon.com/Compilers-Principles-Techniques-Tools-...

I highly recommend it, but it's heavy stuff. There are probably simpler guides out there that just cover the basics.

Re: How I wrote a self-hosting C compiler in 40 days

#29

For anyone interested in compiler writing and looking for a good resource to start, probably one of the best is the "Dragon Book": http://www.amazon.com/Compilers-Principles-Techniques-Tools-... I highly recommend it, but it's heavy stuff. There are probably simpler guides out there that just cover the basics.

Indeed it is the standard, but I'm pretty sure I have a permanent twitch in the corner of my eye from my university compilers course and that book. It's probably not something I could ever do for fun, but it's extremely important and useful stuff to know. I'm just too stupid to really grok a lot of the concepts required to write a compiler.

Re: How I wrote a self-hosting C compiler in 40 days

#30

For anyone interested in compiler writing and looking for a good resource to start, probably one of the best is the "Dragon Book": http://www.amazon.com/Compilers-Principles-Techniques-Tools-... I highly recommend it, but it's heavy stuff. There are probably simpler guides out there that just cover the basics.

Please stop recommending the Dragon Book already. It is not just heavy, it is mostly outdated and irrelevant.
Post reply on HN