News for hackers. Stuff that matters.
Re:Lessons from Writing a Compiler by zetalyrae on Thursday July 14, 2022 @07:52PM (#32102794)
I think writing a parser by hand should be reserved for when

1. You have users.

2. You can demonstrate a marked improvement in performance and/or error reporting with the hand-written parser.

Otherwise, deriving a parser from a grammar should absolutely be the default. Writing parsers by hand is error-prone, causes security vulnerabilities[0], and is very tedious work.

[0]: http://langsec.org/

Re:Lessons from Writing a Compiler by pianoben on Friday July 15, 2022 @01:00AM (#32104762)
Having written production-level compilers, my advice: use ANTLR for as long as humanly possible. It can remain adequate far, far longer than you think, and will save you months/years of stress.

When you outgrow it, you'll know - and by then, hopefully you'll have funding/users/contributors/etc to support the dev effort of rolling your own parser.

Re:Lessons from Writing a Compiler by matijash on Friday July 15, 2022 @04:15AM (#32105939)
Our compiler stack is in Haskell so we initially started with Parsec (parser combinator library) and then eventually switched to Happy (parser generator that Haskell uses itself) since at first we didn't have a formal grammar defined so that made us write a lot of code for every little addition to the language (it's a simple declarative DSL, https://github.com/wasp-lang/wasp).

The problem we're now facing with Happy is that it doesn't really have support for error recovery. We're trying to implement LSP so recovery is important in order to have proper syntax highlighting and error reporting in an IDE.

Wondering how well ANTLR covers this, I was quite surprised to find out that Happy has so little support for it.

Re:Lessons from Writing a Compiler by DylanSp on Friday July 15, 2022 @02:53PM (#32111549)
IIRC Happy (and Alex, the accompanying lexer generator) are pretty much straight ports of lex/yacc to Haskell; they predate some of the conveniences of newer parser generators like ANTLR.
Re:Lessons from Writing a Compiler by rowanG077 on Friday July 15, 2022 @09:01AM (#32107678)
I would have the opposite advice. Derived parsers have shit errors. Which is arguebly the most important feature of a parser besides being able to parse. And writing parsers by hand is trivial anyway. In a lot of cases if you squint a little you can see the BNF.
Re:Lessons from Writing a Compiler by t-3 on Thursday July 14, 2022 @10:03PM (#32103789)
3. Your language has very little/no syntax.

A parser for a Forth is a one-liner or a short loop in most languages.

Re:Lessons from Writing a Compiler by mhzsh on Thursday July 14, 2022 @11:59PM (#32104464)
4. You want to learn how to implement a parser by hand.