Skip to content
CoreSource checked

Parser Combinators

Builds parsers by composing small parser functions into larger grammar rules.

Quick Facts

Compact metadata for implementation and discovery.

Category
Parsing
Difficulty
Intermediate
Family
Composable Parsing
Tier
Core
Status
Verified
Review
Source Checked
Sources
2
Formulas
1
Relationships
1
Code example
Ready
Visualization
Ready

Overview

Parser combinators represent grammar pieces as functions that can be sequenced, alternated, repeated, and transformed directly in code.

Core Concepts

parser functioncompositionbacktrackinggrammar rule

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(n)

Average

O(n * rules)

Worst

O(exponential)

Space

O(parse state)

When to Use

  • Use when language parsing needs parser function.
  • Use when the input matches the assumptions behind composable Parsing.

When Not to Use

  • Avoid when the grammar class does not match the parser's assumptions.
  • Avoid when a simpler algorithm gives the required result with less setup.

What It Does

  • Builds parsers by composing small parser functions into larger grammar rules
  • Uses parser function and composition to guide the work.
  • Produces parse tree.

Use Cases

  • Language parsing
  • Compiler front ends
  • Structured text processing

Inputs

  • Tokens or grammar
  • Parser state

Outputs

  • Parse tree
  • Automaton
  • Grammar table or parse result

Key Formulas (1)

Sequence combinator

parse(A then B) = parse(B, parse(A, input).rest)
parse(A then B) = parse(B, parse(A, input).rest)

Runs the second parser on the remaining input from the first parser.

Pseudocode

  1. 1.Prepare tokens, grammar rules, tables, or automaton states
  2. 2.Advance through tokens or grammar items according to the parsing rule
  3. 3.Create parse nodes, state transitions, or table entries when a rule applies
  4. 4.Report an error or return the completed structure

Canonical Example

Input
tokens = ['id', '+', 'id', '*', 'id'] with a tiny grammar or precedence table
Output
Parse tree
Explanation
Parser Combinators applies parser function to produce parse tree.

Implementation Notes

  • Report errors with token position and expected grammar symbols.
  • Keep the parser function step separate enough to test independently.
  • Keep lexical analysis separate from parsing unless the method explicitly combines them.

Common Pitfalls

  • Ambiguity and left recursion must be handled according to the parser type.
  • Test edge cases, not only clean examples.
  • Mixing token positions, character offsets, and grammar states can produce misleading parse errors.

Interactive Mechanism Walkthrough

Sequence Window & Automaton Alignment

Show grammar items, parser states, and consumed input as parsing progresses.

Input:ABABABCABAB
Window:ABAB
Step 1 of 4: Prepare tokens, grammar rules, tables, or automaton states
parsing-mechanism

Notes

Parser Combinators should be used with the assumptions and tradeoffs of composable Parsing in mind.

Sources (2)

What each reference supports for this entry.

Parsing Techniques

Book

Parsing reference for Parser Combinators's control flow, grammar assumptions, and parser construction tradeoffs.

Open source

Monadic Parsing in Haskell

Paper

Specific reference for parser combinators as first-class parsers with sequencing, choice, repetition, and monadic composition.

Open source

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

Conceptual neighborRecursive Descent Parsing

Parser combinators and recursive descent both build parsers directly from grammar-like structure.

Tags

ParsingTop-Down ParsingParser GeneratorFunctional Programming