Live data from Hacker News

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

sigbus.info

91–100 of 128 posts

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

#91

Self-hosting is pointless. Go has it--who cares. I wrote a program 1000x faster than the ruby one (at work) with zero bugs in Go, but I still don't want to use it(Go). Java is fine. Do I care if Java is self-hosting? No. I'll do (another) language in Javacc (my first one is still awesome) or ANTLR.

If you want to create a self sufficient system then it isn't pointless at all. That just happens to be something that isn't in your requirements.

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

#92
post #90
post #85

What do you mean, "as a child"?

The parent poster is suggesting advent calendars are used most often by children. This is true around my area

And I think the person you responded to is implying his exit from childhood did not in any way slow down his use of advent calendars for chocolate-eating purposes.

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

#93
post #34

Earlier quoted context omitted.

Jack Crenshaw, of "Let's Build A Compiler" fame, has an interesting critique of works like these: http://compilers.iecc.com/crenshaw/tutor8.txt The main point for our discussion is last: > Desire for Generality > We have been concentrating on the use of a recursive-descent parser to parse a deterministic grammar, i.e., a grammar that is not ambiguous and, therefore, can be parsed with one level of lookahead. > In pra…

May of these issues are still relevant to embedded systems with limited resources.

If you really want to compile something on an embedded system, you'd be much better off with something like Forth anyway.

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

#94
post #68

Honestly, the most difficult, time consuming, and mundane aspect to this project would have to be the parser, which was apparently written in C by hand. So bravo. Getting to some of the final notes: > ... I'd choose a different design than that if I were to write it again. Particularly, I'd use yacc instead of writing a parser by hand and introduce an intermediate language early on. That's why I found the LALRPOP pos…

This is completely contrary to my experience, in which parser generators have not paid for themselves. Writing a parser by hand in C is generally easier and less time-consuming over the long run than struggling along with a parser generator, especially if you have any interest in producing useful syntax error messages or recovering from other kinds of errors in some useful way. I agree that almost none of the importa…

I wrote a parser-generator in C and it took less than 42 days. Your mileage may vary.

https://github.com/gregtour/parsergenerator

Most of the C code is less than 1.5k LOC for the functional part, and this minimal, self-compiling C compiler has a parser that is nearly 3,000 lines long.

Even if it took 1,000 lines of extra code for the non-LR aspects of C's grammar, I think it would be the same amount of work.

This just leaves you in a better position to write a C++, Go, or Java compiler at the end.

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

#95
post #66

Earlier quoted context omitted.

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 i…

I definitely agree with ML and OCaml, but why do you recommend Lisp for compiler work? I love working withs Lisps, but how do you deal with dynamic typing bugs in compiler work? I prefer OCaml/Haskell/ML for its really strong static type checking for compiler work. Just curious though...

ML got ADTs and static type guarantees, but Lisp got macros, which allows to implement things like http://andykeep.com/pubs/np-preprint.pdf - which is significantly less boilerplate than in ML. An ideal language for compilers would have combined both properties, but to make it happen, an expression problem must be solved first.

See a relevant discussion here from not long ago:

https://news.ycombinator.com/item?id=10712566

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

#96
post #30

Earlier quoted context omitted.

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

Please could you recommend an alternative?

Engineering a Compiler 2nd edition, by Keith D. Cooper and Linda Torczon

The dragon books are better used as references to those who are already familiar with all of the high-level concepts in compiler writing.

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

#97
post #73
post #30

Earlier quoted context omitted.

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

I wouldn't go so far to say that the Dragon Book is outdated and irrelevant. (I'm assuming you're referring to the 2nd edition from 2006.) Unless you're focusing on back-end optimization and code generation techniques (something a new compiler writer typically does NOT do), the bulk of the theory and material you'd cover in a first semester compiler course is fairly static. But if a person is merely looking to bang o…

It is actually an outdated view, to split a compiler into dedicated monolithic front-end and back-end parts. The more modern approach is very much the opposite. It is a nearly continuous, very long sequence of very simple transforms, rewriting a code seamlessly, all the way down from a front-end (i.e., a parser) to a back-end or multiple back-ends. And this approach is very alien to anything you'd find in the Dragon Book.

As for parsing, as I already said elsewhere in this thread, all the techniques from Dragon Book are not practical any more and are not used in the modern compilers. There are far better ways, which are not covered in the book, and they're far simpler, not even deserving a dedicated book at all.

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

#98
post #60
post #42

1) write compiler 2) get a job at google . . 4) profit

I was already working at Google full-time (for an unrelated project) when I was writing this in my spare time.

So I am hearing ...

1) Get a job at Google 2) Write a Compiler in Spare Time 3) ... 4) PROFIT!!!!

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

#99
post #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 :)

Well... easier than the compiler but very hard to do the last 20% (I never had the time; hobby project) because docs are so hard to find. Or at least were--maybe that's changed.

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

#100
post #90

Earlier quoted context omitted.

The parent poster is suggesting advent calendars are used most often by children. This is true around my area

And I think the person you responded to is implying his exit from childhood did not in any way slow down his use of advent calendars for chocolate-eating purposes.

Why would I purchase a device specifically designed to rate limit my chocolate consumption? Advent calendars are the Internet data caps of dessert.
Post reply on HN