Live data from Hacker News

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

sigbus.info

41–50 of 128 posts

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

#41
Anyone tried using it? How do I use it to generate executable (as per the code it should fork the 'as')?

Getting following error: [ERROR] main.c:144: (null): One of -a, -c, -E or -S must be specified

-c, -E and -S are working fine. Couldn't figure out from code what -a does.

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

#43

> I suspect that he [Dennis Ritchie] invented a syntax, wrote code for it, which turned out to be more complicated than he had expected. And that was eventually standardized by the ANSI committee. It's hard to implement a standardized language because you have to get everything right. It's rather easy to write your own toy language. Love those lines

It's actually a clone of BCPL, which invented keywords & "programmer in charge philosophy." They ported it to PDP's with a few changes for UNIX. Standards came later. Specific details every step of the way are here:

http://pastebin.com/UAQaWuWG

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

#44
post #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.

There's quite a few decent alternatives. I often suggest Wirth's Compiler Construction and Oberon sources because they're straightforward lessons plus give experience with Wirth style of simple, safe, efficient languages. Then, they can improve the Oberon System or compilers for personal projects and improvement.

That said, I typically recommend compilers get written in an ML or LISP given it's so much easier to do in those languages. Ocaml and Racket are my main recommendations for modern work. One can also use techniques for deriving imperative code from functional programs if one wants to re-implement that compiler in specific imperative language on a machine. The constructs are even straight-forward enough for macro assembly for those that want to understand it down to bare metal.

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

#45
post #4

I was a little surprised that the author was able to manage both C11 and the preprocessor in that time. The preprocessor is hard. But there was existing code from a previous version of it, which makes sense. Still, a fantastic achievement! Congrats to the author!

Out of curiosity - why do you consider cpp particularly hard? It's easier than the compiler, actually :)

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

#46
post #32

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

They might, although no production quality C or C++ compiler uses anything other than a hand-rolled recursive descent parser, afaik. The lex, parse and AST directories in Clangs source tree are ~100,000 LOC combined, and all hand-written.

Semantic Designs toolkit uses GLR to handle about everything one could think of:

http://www.semanticdesigns.com/Products/DMS/DMSToolkit.html

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

#47

> I suspect that he [Dennis Ritchie] invented a syntax, wrote code for it, which turned out to be more complicated than he had expected. And that was eventually standardized by the ANSI committee. It's hard to implement a standardized language because you have to get everything right. It's rather easy to write your own toy language. Love those lines

It's actually a clone of BCPL, which invented keywords & "programmer in charge philosophy." They ported it to PDP's with a few changes for UNIX. Standards came later. Specific details every step of the way are here: http://pastebin.com/UAQaWuWG

That vimeo talk was rich.

ps: https://vimeo.com/132192250

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

#48

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

Here's a complete C frontend that uses the lex/yacc approach - https://github.com/eliben/pycparser (the Python ports of them, that is)

FWIW, if I had to do this again today I would certainly go for hand-written recursive descent. lex/yacc charm you in but eventually prove to be much more difficult to tweak and reason about.

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

#50

Earlier quoted context omitted.

It's actually a clone of BCPL, which invented keywords & "programmer in charge philosophy." They ported it to PDP's with a few changes for UNIX. Standards came later. Specific details every step of the way are here: http://pastebin.com/UAQaWuWG

That vimeo talk was rich. ps: https://vimeo.com/132192250

It's in my references.
Post reply on HN