Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This seems to have been subject to waxing and waning fashion. I had a similar experience recently -- someone sent me a PR with a hand-written parser. I commented "do people write parsers by hand any more?". After some push back, I searched online and found a ton of articles "why you should write your own parser and not use a parser generator". Still beats me as to why though.


I've tried to write PEG grammars/parsers and found that I am not quite smart enough to figure it out.

Hand-written lexer/parser means you can be dumb as a doornail and just walk through the logic.

As an aside, it also gives you a better ability to write error messages, and an easier time debugging compared to autogenerated solutions.


> it also gives you a better ability to write error messages

This is not true. Instead of trying to use error recovery feature of parser genrator, you can just add error case to your grammar, and it works as well as hand-written parser. Parser generator error recovery is pure bonus on top.

> and an easier time debugging compared to autogenerated solutions

This is also not true. Most parser generators (certainly lex/yacc) have good enough debug support that you never see generated code while you debug.


There are a few reasons for this. Some languages arent Context Free Grammars (the classic example for this is "A * B;" is this multiplication of the A and B variables or is it a declaration of the B variable with the type being a pointer to an A) which make writing the grammar much harder in the first place. Some solutions to this are using a GLR parser instead, but that has the downside of super high execution time in the worst case. Lastly, even if you overcome all of these with the proper structure and hints, you run into pretty bad error cases. I.e. handing an invalid input is hard and giving an error to the user that makes sense is even more difficult. (which of the possible branches in the grammar rules is the one thats wrong? in simpler grammars you can probably give a good error, but a C grammar is going to have a much harder time) This is why gcc moved away from using a grammar to a custom parser IIRC.


I've heard the "useful error message" argument, but my personal experience coding modern languages suggests that not much progress has been made on that front. The compiler error message is almost never "useful".


You might want to check out the Elm compiler: https://elm-lang.org/news/compilers-as-assistants


I write parsers all the time. It is quick for me to do and it has many advantages. Including much better error messaging and easy to debug code. The code generated by most parser generators are not really human readable. Making fixing complicated parsing bugs super hard.


In my compilers class at university we wrote a recursive descent parser by hand but we wrote the LALR and LL parsers using yacc or bison I think - that was what the three assignments said to do and the professor said using a parser generator was the only sensible approach for LALR and LL parsers.


You must be from the same vintage as me!


This was around a decade ago but my professor was not a young man (early 60s I'd guess).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: