# Struggling to understand parser combinators

**URL:** <https://users.rust-lang.org/t/struggling-to-understand-parser-combinators/67165>\
**Category:** tutorials\
**Created:** [November 11, 2021, 4:44am UTC](https://users.rust-lang.org/t/struggling-to-understand-parser-combinators/67165 "2021-11-11T04:44:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ethindp](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/ethindp/32/15953_2.png) [@Ethindp](https://users.rust-lang.org/u/Ethindp)\
**Post date:** [November 11, 2021, 4:44am UTC](https://users.rust-lang.org/t/struggling-to-understand-parser-combinators/67165/1 "2021-11-11T04:44:48Z")

</div>

I'm used to writing PEGs or grammars with tools like Bison or Flex, where I can practically write something like (as an example, from the Ada specification in its form of EBNF):

```nohighlight
identifier ::=
identifier_start {identifier_start | identifier_extend}
identifier_start ::=
letter_uppercase
| letter_lowercase
| letter_titlecase
| letter_modifier
| letter_other
| number_letter
identifier_extend ::=
mark_non_spacing
| mark_spacing_combining
| number_decimal
| punctuation_connector
...
loop_statement ::=
[loop_statement_identifier:]
[iteration_scheme] loop
sequence_of_statements
end loop [loop_identifier];
iteration_scheme ::= while condition
| for loop_parameter_specification
| for iterator_specification
loop_parameter_specification ::=
defining_identifier in [reverse] discrete_subtype_definition
iterator_specification ::=
defining_identifier in [reverse] iterator_name
| defining_identifier [: subtype_indication] of [reverse] iterable_name

```

I'm definitely not used to the idea of using a bunch of different functions and combining them to make a parser. Do you guys know of any helpful resources that, e.g., offer EBNF/ABNF/BNF equivalents to what is achievable with a parser combinator? Or some kind of transitioning guide? I can see the amount of power that a parser combinator offers -- and its especially nice because you can practically encode your entire parser and lexer in the type system -- but it just isn't clicking for me and I don't really understand what I'm missing.  
Edit: as clarification, I can take a grammar for, say, pascal, like [this one](https://www.cs.kent.edu/~durand/CS43101Fall2004/resources/Pascal-EBNF.html) and convert it to something in C++ using [PEGTL](https://github.com/taocpp/PEGTL) or even with the [peg](https://crates.io/crates/peg) rust crate. But when I look at other parser examples that use parser combinators, I get really confused because I don't know how to take all the combinators and combine them into something that matches what I'm trying to parse. I've struggled with all the good parser combinator libraries I know of (e.g. Nom, Combine, and nwo Chumsky). They all offer a lot of power but I still can't really figure out how to build a parser with them that makes sense in my mind.

---

<div class="post-metadata">

**Author:** ![tiye](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/tiye/32/20514_2.png) [@tiye](https://users.rust-lang.org/u/tiye)\
**Post date:** [November 11, 2021, 4:55am UTC](https://users.rust-lang.org/t/struggling-to-understand-parser-combinators/67165/2 "2021-11-11T04:55:56Z")

</div>

have you read Haskell tutorials about Parser Combinators?

---

<div class="post-metadata">

**Author:** ![Ethindp](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/ethindp/32/15953_2.png) [@Ethindp](https://users.rust-lang.org/u/Ethindp)\
**Post date:** [November 11, 2021, 7:01am UTC](https://users.rust-lang.org/t/struggling-to-understand-parser-combinators/67165/3 "2021-11-11T07:01:48Z")

</div>

No... Haskell hasn't really clicked with me, but maybe I've been over-avoiding Haskell and maybe those tutorials would help.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [February 9, 2022, 7:01am UTC](https://users.rust-lang.org/t/struggling-to-understand-parser-combinators/67165/4 "2022-02-09T07:01:55Z")

</div>

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
