News for hackers. Stuff that matters.
Re:Lessons from Writing a Compiler by zetalyrae on Thursday July 14, 2022 @07:59PM (#32102848)
The time investment of writing a good hand-written parser is only worth it if you have users whose workflow would be improved by performance/error reporting gains once you have exhausted what a grammar-derived parser can provide.

For Clang and GCC is it absolutely a no brainer to use a hand-written parser.

If you're trying to get to an MVP compiler, or you're building a bootstrapping compiler, using a grammar-derived parser to get to the finish line faster is far more sensible.

Re:Lessons from Writing a Compiler by eatonphil on Thursday July 14, 2022 @08:06PM (#32102907)
Using parser generators doesn't make a lot of sense to me.

It's far easier to handwrite parsers.

Unless you're a midsized company with the resources and who doesn't mind bad error messages I would not recommend anyone use parser generators.

Re:Lessons from Writing a Compiler by zetalyrae on Thursday July 14, 2022 @08:19PM (#32102995)
What could be easier than writing a grammar in EBNF-ish notation? This gives you the added benefit that the grammar is explicitly defined, and not implicit in the parser code.
Re:Lessons from Writing a Compiler by westoncb on Thursday July 14, 2022 @09:16PM (#32103483)
> This gives you the added benefit that the grammar is explicitly defined

Especially if you're still refining the syntax: it is so comparatively easy to modify a grammar vs parser.

Re:Lessons from Writing a Compiler by eatonphil on Thursday July 14, 2022 @08:21PM (#32103009)
> What could be easier than writing a grammar in EBNF-ish notation?

Not learning a new language/build system/coding paradigm. :)

Re:Lessons from Writing a Compiler by kaba0 on Friday July 15, 2022 @04:31PM (#32112446)
ANTLR took me an afternoon, before I could use it for a simple language, including adding it to my build tool.
Re:Lessons from Writing a Compiler by pizza234 on Friday July 15, 2022 @05:16AM (#32106278)
> It's far easier to handwrite parsers.

It's also far easier to make buggy handwritten parsers. I don't have much experience, but in the project that I've seen, where a handwritten parser was used, the issue tracker was full of parser bugs.

Re:Lessons from Writing a Compiler by rhdunn on Friday July 15, 2022 @08:45AM (#32107536)
I tend to write lots of tests at each level -- lexer, parser, AST, etc. -- to cover the different BNF grammar statements. I've done this with an XQuery and XPath parser I wrote for IntelliJ.

You can cover the different lexical variations in the lexical tests, and ensure that the lexer is reporting the correct things in the lexer tests. For these, I generally ensure I cover each token in a given BNF grammar statement, even if that means repeating checks for a given keyword, as it keeps the tests self-contained and easier to verify.

You can cover the different parser/grammar variations (different branches/paths in the BNF, missing tokens for error reporting and recovery, etc.) in the parser tests. These can make use of serializing the tree to a file, so your input is an example program and the output is the parsed AST tree.

The AST tests can then test the actual data model your AST provides, such as access to the value of a literal, or information about a type.

Higher-level tests such as constant folding can then be done using the AST API, or things like serializing the AST to a textual representation.

Re:Lessons from Writing a Compiler by userbinator on Thursday July 14, 2022 @09:07PM (#32103401)
It's also far easier to debug handwritten parsers.
Re:Lessons from Writing a Compiler by tomcam on Thursday July 14, 2022 @08:49PM (#32103241)
a parser generator can be a very good first pass at determining whether your language is properly formed
Re:Lessons from Writing a Compiler by eatonphil on Thursday July 14, 2022 @08:52PM (#32103267)
What do you mean by properly formed?
Re:Lessons from Writing a Compiler by Calavar on Thursday July 14, 2022 @09:05PM (#32103383)
Probably that it doesn't have ambiguities
Re:Lessons from Writing a Compiler by tomcam on Friday July 15, 2022 @03:42AM (#32105727)
Thanks, that’s a better way of putting it.
Re:Lessons from Writing a Compiler by eatonphil on Thursday July 14, 2022 @09:15PM (#32103475)
Is that an important property? If so, why do you (*whoever) think so?
Re:Lessons from Writing a Compiler by Calavar on Thursday July 14, 2022 @09:48PM (#32103693)
As always, it depends on what your goal is.

An unambiguous parser is easier to write tests for. It leads to fewer subtle "gotchas" where something syntax checks and compiles but is interpreted by the compiler as something very different than what the programmer intended. It's easier for third parties to write their own parsers and tooling for the language if they can target a formal grammar as opposed to the behavior of your Turing complete code.

But sometimes you don't care about any of those things. Especially if you're talking about a language with a small surface area. Or if you have to deal with legacy stuff (for example, parsing something like C++ that is heavily context sensitive, in which case trying to shoehorn things into a formal CFG so you can feed it to a generator might actually make things much harder than they have to be).

Re:Lessons from Writing a Compiler by tomcam on Friday July 15, 2022 @03:45AM (#32105742)
Not always, and that’s a great question! But I can help you catch things that might not be obvious when your first create the grammar, even such simple things as ambiguous nested if statements or errors in string formatting DSL syntax.
Re:Lessons from Writing a Compiler by tomcam on Friday July 15, 2022 @08:25PM (#32114164)
* it can, not I can
Re:Lessons from Writing a Compiler by wyago on Thursday July 14, 2022 @08:10PM (#32102935)
It takes 1 or 2 hours to make a decent pratt parser for even somewhat unusual syntax.