<h2>Motivation</h2>
<p>There seems to be something popular in the rust community called parser combinators.</p>
<p>I temporarily use a traditional recursive descent parser, my syntax is not yet stable, composition means flexible, I am considering whether to use the parser combinator library to rewrite.</p>
<h2>Question</h2>
<p>I'm wondering if there is a mainstream language or compiler front end using parser combinators?</p>
<p>What are the advantages over traditional hand-written recursive descent parsers? Such as runtime speed, maintainability, traceability of semantic errors, etc.</p>

<parsing>
What are the advantages of parser combinators?
CC BY-SA 4.0
2023-07-10T03:15:52.990Z
I don't know thoroughly, but Haskell chose `readPrec :: Read a => ReadPrec a` over `readsPrec :: Read a => Int -> ReadS a` because "backtracking parsers become inefficient easily."
Dannyu NDos
CC BY-SA 4.0
2023-07-10T10:31:40.073Z
You're asking 2 questions here: does anyone use them, and what are the advantages. I'd suggest editing your question to focus on the 2nd, because the first is not that interesting (the answer is: The main disadvantage of parser combinators is that they can easily cause your parser to take exponential time due to the backtracking. For this reason, you probably won't find any production compiler using them. [There are ways around this](https://aclanthology.org/W07-2215.pdf), but AFAIK they can only reduce time complexity to cubic at best, so it's just easier to switch to a faster parsing method)
pxeger
CC BY-SA 4.0
2023-07-10T19:01:31.277Z
Rust community primarily uses parser generators for programming languages, not parser combinators, IE [Pest](https://github.com/pest-parser/pest) things like Nom are used for small domain specific languages.
Krupip
CC BY-SA 4.0
2023-07-12T07:27:57.850Z
@Krupip Another parser combinator crate [chumsky](https://docs.rs/chumsky/latest/chumsky/) has been gaining traction lately. It has built-in support for error reporting and error recovery, which is super handy when you want decent error messages.
Bubbler
CC BY-SA 4.0
2023-07-12T07:42:46.267Z
@pxeger: "exponential time due to the backtracking" — I don't know about its time complexity, but [`combine`](https://lib.rs/crates/combine) does LL(1) by default, so minimal backtracking.
user570286
CC BY-SA 4.0
2023-07-12T07:48:52.960Z
@Krupip: "Rust community primarily uses parser generators for programming languages, not parser combinators, IE Pest things like Nom" — I don't think this is accurate. Nom's short description is "A byte-oriented, zero-copy, parser combinators library". [LALRPOP](https://lib.rs/crates/lalrpop) is an example of an actual Rust parser generator.
user570286
CC BY-SA 4.0
2023-07-12T07:57:48.353Z
@user570286 I'm confused, are you saying me pointing out Nom as a parser combinator is *not* true, ie Nom is *not* a parser combinator?
Krupip
CC BY-SA 4.0
2023-07-12T21:01:41.207Z
@Krupip: no, I thought you were saying that Nom is a parser generator.  I see now that (ironically?) I misparsed your syntax, for which I apologize.
user570286
<p>&quot;Parser combinators&quot; and &quot;recursive descent parsers&quot; are complementary.  In fact, &quot;parser combinators&quot; are what you get if you try to write a recursive descent parser in Haskell without going mad.</p>
<p>The main difference is that a traditional recursive descent parser involves writing new parser functions that use the usual control structures of the underlying language to explicitly invoke lower-level parsers, coordinate their execution, and combine their results.  The parser combinator approach involves calling functions that take parsers (i.e., parser functions) as arguments and return a parser (i.e., a parser function) that invokes the argument parsers, coordinates their execution, and combines their results.</p>
<p>If you like, you can think of parser combinators as &quot;templates&quot; for recursive descent parser functions that themselves can be composed into larger &quot;templates&quot;.</p>
<p>The advantages primarily have to do with reusability of the combinators.  As a concrete example, if you are writing a traditional recursive descent parsing function to parse a sequence of statements separated by semicolons, you'll probably write a function that tries to parse statements in a loop, stopping when it fails to parse an intervening semicolon.  When it comes time to write a parsing function to parse a sequence of expressions separated by commas, you'll probably write another function that tries to parse expressions in a loop, stopping when it fails to parse an intervening comma.</p>
<p>If you're using parser combinators, you'll probably define these parsing functions with a couple of one-liners:</p>
<pre><code>let program = sep_by(statement, semicolon);
let arglist = sep_by(expression, comma);
</code></pre>
<p>(If your approach to writing recursive descent parsers allows you to achieve this level of code reuse, congratulations, you've independently implemented parser combinators.)</p>
<p>Anyway, the resulting parser code tends to be easier to read and easier to maintain by virtue of the reusability of combinator components.  With well named components, the definition of a parser can read a lot like a high-level grammar, which is also appealing.  However, in practice, this works really well in Haskell and less well in other languages that don't share Haskell's curried functions, applicative operators, and &quot;call by juxtaposition&quot; syntax.  I'd still say that parser combinators are a net win for maintainability in non-Haskell languages, but the syntax may be ugly and distracting.</p>
<p>With respect to runtime speed, parser combinators should perform similarly to recursive descent parsers, because they're basically doing the same thing.  There's nothing intrinsic to parser combinators that causes exponential performance problems or inefficient backtracking.  It will depend on the library.  Some libraries don't backtrack by default                              , and you must use a <code>try</code> combinator on a case-by-case basis to enable backtracking.  For these libraries, you <strong>must</strong> learn and understand their method of implementing backtracking (which usually boils down to the difference between &quot;failing without consuming input&quot; and &quot;failing after consuming input&quot;) or you will create fragile, buggy parsers that you'll never be able to get right.</p>
<p>Some libraries (e.g., nom, I think, unless I'm misunderstanding the documentation) <em>do</em> backtrack by default, and you must become skilled at using the <code>cut</code> combinator to curb backtracking, or you're likely to create a disastrously slow parser by accident.</p>
<p>That's the main disadvantage of the parser combinator approach -- it involves a complicated contract between parsers and combinators, and the details of that contract are often not entirely clear in the library documentation.  You can write &quot;toy&quot; parsers without really understanding what you're doing, but when you make the switch to real world parsers, a firm understanding of the details of parser behavior is absolutely essentially, and you'll probably need to read some library code to really understand what's going on.  (This is really bad in the Haskell community, where &quot;types are documentation&quot;, ha ha.  A well documented Rust crate might avoid this problem.)</p>
<p>With respect to traceability of semantic errors, I assume you're talking about errors in parse input that can only be detected after parsing is complete and some semantic analysis has been performed -- can you then trace them back to specific bits of code?  Most libraries will have a method of retrieving position information and attaching it to the parser abstract syntax tree.  It's usually not an automatic process, and you'll have to decide exactly how you want to attach that position information to your AST.  I note that &quot;nom&quot; has a &quot;nom_locate&quot; crate specifically designed to help you attach location &quot;span&quot; data to your AST.</p>
<p>That brings us to another disadvantage of parser combinators.  They're generally part of a library that you yourself will not have written.  So, unless you want to dig into library code, you'll be dealing with a lot of abstract concepts and opaque functions and types.  If the library doesn't provide a feature that you need, you'll have to dig in and implement it, and it'll probably be harder than if you'd written a traditional recursive descent parser yourself.  You'll want to pick a well-maintained, &quot;serious&quot; parser library here.  I think &quot;nom&quot; might fit the bill, but I don't have any first hand experience with it.  Working through some tutorials and studying some example parsers might be worthwhile.</p>

CC BY-SA 4.0
2023-07-11T23:00:24.470Z
2023-07-12T03:05:02.050Z
2023-07-12T03:05:02.050Z
K. A. Buhr
<p>To add to the good and long answer:</p>
<p>If your language definition uses denotational semantics, your code will reflect that or show errors in your language definition.</p>
<p>From my experience, they are horrible when it comes to good input error handling and they tempt you to make bad decisions in your language design as most of these libraries are, like handwritten recursive decent, turing-complete and not LL(1) or similar.</p>

CC BY-SA 4.0
2023-07-12T07:20:40.010Z
2023-07-12T07:20:40.010Z
feldentm
# What are the advantages of parser combinators?
Posted by: username_0 on 2023-07-10
## Motivation

There seems to be something popular in the rust community called parser combinators.

I temporarily use a traditional recursive descent parser, my syntax is not yet stable, composition means flexible, I am considering whether to use the parser combinator library to rewrite.

## Question

I'm wondering if there is a mainstream language or compiler front end using parser combinators?

What are the advantages over traditional hand-written recursive descent parsers? Such as runtime speed, maintainability, traceability of semantic errors, etc.
**Tags:** parsing
> **Comments:**
> **username_1**: I don't know thoroughly, but Haskell chose `readPrec :: Read a => ReadPrec a` over `readsPrec :: Read a => Int -> ReadS a` because "backtracking parsers become inefficient easily."  
> **username_2**: You're asking 2 questions here: does anyone use them, and what are the advantages. I'd suggest editing your question to focus on the 2nd, because the first is not that interesting (the answer is: The main disadvantage of parser combinators is that they can easily cause your parser to take exponential time due to the backtracking. For this reason, you probably won't find any production compiler using them. [There are ways around this](https://aclanthology.org/W07-2215.pdf), but AFAIK they can only reduce time complexity to cubic at best, so it's just easier to switch to a faster parsing method)  
> **username_3**: Rust community primarily uses parser generators for programming languages, not parser combinators, IE [Pest](https://github.com/pest-parser/pest) things like Nom are used for small domain specific languages.  
> **username_4**: @Krupip Another parser combinator crate [chumsky](https://docs.rs/chumsky/latest/chumsky/) has been gaining traction lately. It has built-in support for error reporting and error recovery, which is super handy when you want decent error messages.  
> **username_5**: @pxeger: "exponential time due to the backtracking" — I don't know about its time complexity, but [`combine`](https://lib.rs/crates/combine) does LL(1) by default, so minimal backtracking.  
> **username_5**: @Krupip: "Rust community primarily uses parser generators for programming languages, not parser combinators, IE Pest things like Nom" — I don't think this is accurate. Nom's short description is "A byte-oriented, zero-copy, parser combinators library". [LALRPOP](https://lib.rs/crates/lalrpop) is an example of an actual Rust parser generator.  
> **username_3**: @user570286 I'm confused, are you saying me pointing out Nom as a parser combinator is *not* true, ie Nom is *not* a parser combinator?  
> **username_5**: @Krupip: no, I thought you were saying that Nom is a parser generator.  I see now that (ironically?) I misparsed your syntax, for which I apologize.  
---
## 2 answers
### Answer by username_6 on 2023-07-11. Score: 11
"Parser combinators" and "recursive descent parsers" are complementary.  In fact, "parser combinators" are what you get if you try to write a recursive descent parser in Haskell without going mad.
The main difference is that a traditional recursive descent parser involves writing new parser functions that use the usual control structures of the underlying language to explicitly invoke lower-level parsers, coordinate their execution, and combine their results.  The parser combinator approach involves calling functions that take parsers (i.e., parser functions) as arguments and return a parser (i.e., a parser function) that invokes the argument parsers, coordinates their execution, and combines their results.
If you like, you can think of parser combinators as "templates" for recursive descent parser functions that themselves can be composed into larger "templates".
The advantages primarily have to do with reusability of the combinators.  As a concrete example, if you are writing a traditional recursive descent parsing function to parse a sequence of statements separated by semicolons, you'll probably write a function that tries to parse statements in a loop, stopping when it fails to parse an intervening semicolon.  When it comes time to write a parsing function to parse a sequence of expressions separated by commas, you'll probably write another function that tries to parse expressions in a loop, stopping when it fails to parse an intervening comma.
If you're using parser combinators, you'll probably define these parsing functions with a couple of one-liners:
let program = sep_by(statement, semicolon);
let arglist = sep_by(expression, comma);

(If your approach to writing recursive descent parsers allows you to achieve this level of code reuse, congratulations, you've independently implemented parser combinators.)
Anyway, the resulting parser code tends to be easier to read and easier to maintain by virtue of the reusability of combinator components.  With well named components, the definition of a parser can read a lot like a high-level grammar, which is also appealing.  However, in practice, this works really well in Haskell and less well in other languages that don't share Haskell's curried functions, applicative operators, and "call by juxtaposition" syntax.  I'd still say that parser combinators are a net win for maintainability in non-Haskell languages, but the syntax may be ugly and distracting.
With respect to runtime speed, parser combinators should perform similarly to recursive descent parsers, because they're basically doing the same thing.  There's nothing intrinsic to parser combinators that causes exponential performance problems or inefficient backtracking.  It will depend on the library.  Some libraries don't backtrack by default                              , and you must use a try combinator on a case-by-case basis to enable backtracking.  For these libraries, you must learn and understand their method of implementing backtracking (which usually boils down to the difference between "failing without consuming input" and "failing after consuming input") or you will create fragile, buggy parsers that you'll never be able to get right.
Some libraries (e.g., nom, I think, unless I'm misunderstanding the documentation) do backtrack by default, and you must become skilled at using the cut combinator to curb backtracking, or you're likely to create a disastrously slow parser by accident.
That's the main disadvantage of the parser combinator approach -- it involves a complicated contract between parsers and combinators, and the details of that contract are often not entirely clear in the library documentation.  You can write "toy" parsers without really understanding what you're doing, but when you make the switch to real world parsers, a firm understanding of the details of parser behavior is absolutely essentially, and you'll probably need to read some library code to really understand what's going on.  (This is really bad in the Haskell community, where "types are documentation", ha ha.  A well documented Rust crate might avoid this problem.)
With respect to traceability of semantic errors, I assume you're talking about errors in parse input that can only be detected after parsing is complete and some semantic analysis has been performed -- can you then trace them back to specific bits of code?  Most libraries will have a method of retrieving position information and attaching it to the parser abstract syntax tree.  It's usually not an automatic process, and you'll have to decide exactly how you want to attach that position information to your AST.  I note that "nom" has a "nom_locate" crate specifically designed to help you attach location "span" data to your AST.
That brings us to another disadvantage of parser combinators.  They're generally part of a library that you yourself will not have written.  So, unless you want to dig into library code, you'll be dealing with a lot of abstract concepts and opaque functions and types.  If the library doesn't provide a feature that you need, you'll have to dig in and implement it, and it'll probably be harder than if you'd written a traditional recursive descent parser yourself.  You'll want to pick a well-maintained, "serious" parser library here.  I think "nom" might fit the bill, but I don't have any first hand experience with it.  Working through some tutorials and studying some example parsers might be worthwhile.
---
### Answer by username_7 on 2023-07-12. Score: 2
To add to the good and long answer:

If your language definition uses denotational semantics, your code will reflect that or show errors in your language definition.

From my experience, they are horrible when it comes to good input error handling and they tempt you to make bad decisions in your language design as most of these libraries are, like handwritten recursive decent, turing-complete and not LL(1) or similar.
---
