Skip to content
ExtendedSource checked

Packrat Parsing

Parses with memoized recursive descent for linear-time parsing expression grammars.

Quick Facts

Compact metadata for implementation and discovery.

Category
Parsing
Difficulty
Advanced
Family
Memoized Parsing
Tier
Extended
Status
Verified
Review
Source Checked
Sources
1
Formulas
0
Relationships
2
Code example
Not ready
Visualization
Ready

Overview

Packrat Parsing caches each parser result at each input position so backtracking does not repeat work.

Core Concepts

parse statememoized parse resulttop-down expansionmemoized parsingtoken streamgrammar rule

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(n)

Average

O(n)

Worst

O(n)

Space

O(nr)

When to Use

  • Use Packrat Parsing when you need to run parser expressions recursively.
  • Use it for pEG parsers with inputs like token or character stream.
  • Use it when text or tokens must be turned into a reliable structured representation.
  • Use it when the Memoized Parsing tradeoff is acceptable for the problem size.

When Not to Use

  • Avoid it when the grammar is ambiguous or left-recursive in ways the parser cannot handle.
  • Avoid it when the required output is not parse tree.

What It Does

  • Runs parser expressions recursively
  • Memoizes parser results by rule and position

Use Cases

  • PEG parsers
  • Expression-heavy languages
  • Backtracking grammar parsing

Inputs

  • Token or character stream
  • Parsing expression grammar

Outputs

  • Parse tree
  • Syntax error

Pseudocode

  1. 1.Call the start parsing expression
  2. 2.Before parsing a rule at a position, check the memo table
  3. 3.Store success or failure with the new position
  4. 4.Reuse memoized results during backtracking
  5. 5.Return the final parse result

Canonical Example

Input
tokens = ['2', '+', '3', '*', '4']
Output
structured expression tree or evaluated result
Explanation
Packrat Parsing: The parser uses grammar or precedence rules to recover structure from tokens.

Implementation Notes

  • Keep the input contract explicit: Token or character stream; Parsing expression grammar.
  • Use this step as the implementation spine: Call the start parsing expression.
  • Then preserve the next state change: Before parsing a rule at a position, check the memo table.
  • Keep tokenization separate from parsing unless the algorithm explicitly combines them.

Common Pitfalls

  • Packrat parsers trade memory for predictable linear runtime.
  • Producing unclear errors that do not point to the token or rule that failed.
  • Returning parse tree without handling empty, invalid, or boundary inputs.

Interactive Mechanism Walkthrough

Sequence Window & Automaton Alignment

Show parser calls being cached by input position.

Input:ABABABCABAB
Window:ABAB
Step 1 of 5: Call the start parsing expression
memoized-parser

Notes

Packrat Parsing operates in O(n) time and O(nr) space within the Memoized Parsing family. Often compared with Recursive Descent Parsing and Earley Parser.

Sources (1)

What each reference supports for this entry.

Packrat Parsing

Paper

Reference for packrat parsing, memoized recursive descent, and linear-time PEG-style parsing.

Open source

Explore Next

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

Variant ofPEG Parsing

Packrat parsing memoizes PEG-style recursive descent to guarantee linear parsing time.

Tags

ParsingMemoizationTop-Down Parsing