←back to thread

429 points rui314 | 2 comments | | HN request time: 0.001s | source
Show context
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 #
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 #
1. 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 #
2. rswier ◴[] No.10735289[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.