Struggling to understand parser combinators

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):

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 and convert it to something in C++ using PEGTL or even with the 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.

1 Like

have you read Haskell tutorials about Parser Combinators?

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

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.