4 ms·
Likely controversial opinion: Parser combinators are fun but feel a little too magic for me. Maybe I’m being stubborn, but I prefer to write lexers and parsers
by jonpalmisc 4y ago
Likely controversial opinion: Parser combinators are fun but feel a little too magic for me.
Maybe I’m being stubborn, but I prefer to write lexers and parsers by hand. Although, I would like to get around to writing a “common lexer and parser utility library” one day to abstract away some common patterns while simultaneously not forcing design decisions. Perhaps I’ll end up with a parser combinator library by the time I’m done and I’ll have to eat my words.
- mhh__ 4y agoI think lexers shouldn't be written by hand but parsers often have to be to at least some extent.
- foldr 4y agoI tend to agree. If you're using an imperative language then the basic primitives of structured programming (loops, conditionals and functions) are all you need to write a recursive descent parser. Combinators like Parsec's 'sepBy' can make your parser more concise, but at the cost of making it more difficult to reason about performance and more difficult to read for anyone who hasn't memorized all the combinators. Most parser combinator libraries give you lots of help with the easy stuff (e.g. parse a list of integers separate by commas), but not so much help with the hard stuff like generating good error messages or avoiding unbounded backtracking.
- zesterer 4y agoMy advice has always been: 'Write at least one parser by hand before using parser combinators. Then, never write parsers by hand ever again'.