8
$\begingroup$

There are plenty of libraries out there that provide tools like LL parsers, combinator parsers, ebnf parsers and more. However, these may not always be suited to the grammar of a programming language.

What language constructs/grammar features would create a situation where hand-writing a parser would be more beneficial than using a parsing library?

$\endgroup$
1

1 Answer 1

6
$\begingroup$

Writing a recursive descent parser is pretty simple, but the downside is that you it is hard to figure out the exact spec for the grammar when looking at the code. Another downside is that it's often easy to break ambiguities in a recursive descent parser by hacking some special behaviour. Unfortunately this can end up causing things being parsed in unforeseen ways.

If you need to feed your grammar into some tool (for instance you're making a language plugin) it's also beneficial to have an actual grammar for it.

When you want to error report on the other hand, you might want to parse more than is valid in your language in order to produce good error messages.

So we roughly end up with the following cases:

  1. You're writing the main compiler for a general purpose language: use a hand written parser to have good control over error messages and recovery.
  2. You are just parsing a small DSL: use a parser generator if you want.
  3. You're writing a tool that works on correct code and transforms it: a parser generator is sufficient.
  4. You want to iterate on your grammar and see that it is consistent: use a parser generator.
$\endgroup$
2
  • 1
    $\begingroup$ "it is hard to figure out the exact spec for the grammar when looking at the code" ─ not sure about this. The code structure of a recursive descent parser will closely mirror the BNF or PEG for the language grammar. For my own language MJr, I wrote the PEG after the parser, and found it fairly straightforward to do so. $\endgroup$
    – kaya3
    Commented May 20, 2023 at 13:16
  • $\begingroup$ Although the general spec will be simple, special cases will not be. I have several examples with my language. $\endgroup$
    – Nuoji
    Commented May 20, 2023 at 18:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .