←back to thread

429 points rui314 | 7 comments | | HN request time: 0.925s | source | bottom
1. xigency ◴[] No.10733108[source]
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 post by one of the Rust developers interesting. Writing your own parser generator is actually much easier than writing a parser by-hand (depending on the complexity of the language, here not that complex and still difficult), and I think it's more instructive than using a free or open parser-generator or compiler compiler. The downside is that it is less practical, because almost none of the important aspects of language implementation involve the parser.

replies(2): >>10733439 #>>10733989 #
2. chrisseaton ◴[] No.10733439[source]
> Writing your own parser generator is actually much easier than writing a parser by-hand

Surely to write your own parser generator you will need to write a parser for your grammar language? So you are now writing that parser, and then your actual parser using your new grammar language? That can't be easier than writing one by hand.

replies(2): >>10733636 #>>10733642 #
3. adrianN ◴[] No.10733636[source]
I have written a parser generator to generate a parser for a compiler project for a class. It is indeed the easier route. Your parser specification language is usually a lot simpler than your target language. Your parser generator also just needs the features required for your particular grammar, which means that dirty hacks and regex parsing are a viable route.
4. ◴[] No.10733642[source]
5. marssaxman ◴[] No.10733989[source]
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 important aspects of language implementation involve the parser - and that's why I write 'em by hand! It doesn't take much time, and it's much easier to debug whatever problems you run into when you can use your ordinary debugging tools.

replies(1): >>10734257 #
6. xigency ◴[] No.10734257[source]
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.

replies(1): >>10735289 #
7. rswier ◴[] No.10735289{3}[source]
Many years ago I played around with something called PRECC - A PREttier Compiler Compiler. I remember it was(is) pretty elegant. I wonder if anyone else has experience using it.

http://preccx.sourceforge.net/

This was long before I discovered the simple joys of squeezing out unnecessary CPU cycles from my own hand-rolled self-directed lexer/parsers.