Skip to content
SpecializedSource checked

Earley Parser

Parses general context-free grammars with a chart of partial parse states.

Quick Facts

Compact metadata for implementation and discovery.

Category
Parsing
Difficulty
Advanced
Family
Chart Parsing
Tier
Specialized
Status
Verified
Review
Source Checked
Sources
2
Formulas
0
Relationships
3
Code example
Not ready
Visualization
Ready

Overview

The Earley Parser tracks predicted, scanned, and completed grammar states at each input position.

Core Concepts

parse statechart statesgrammar productiondynamic programmingchart parsingtoken streamgrammar rule

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(n)

Average

O(n^2)

Worst

O(n^3)

Space

O(n^2)

When to Use

  • Use Earley Parser when you need to store partial parse states in chart columns.
  • Use it for ambiguous grammars with inputs like token stream.
  • Use it when text or tokens must be turned into a reliable structured representation.
  • Use it when the Chart Parsing tradeoff is acceptable for the problem size.

When Not to Use

  • Avoid it when the state table would exceed available memory.
  • Avoid it when the grammar is ambiguous or left-recursive in ways the parser cannot handle.
  • Avoid it when O(n^3) worst-case behavior is too costly for the expected input size.
  • Avoid it when the required output is not parse forest or recognition result.

What It Does

  • Stores partial parse states in chart columns
  • Predicts and completes grammar rules

Use Cases

  • Ambiguous grammars
  • Natural language parsing
  • General grammar recognition

Inputs

  • Token stream
  • Context-free grammar

Outputs

  • Parse forest or recognition result

Pseudocode

  1. 1.Create a chart column for each input position
  2. 2.Predict rules for expected nonterminals
  3. 3.Scan matching terminals into the next column
  4. 4.Complete finished rules into waiting states
  5. 5.Accept if the start state completes at the end

Canonical Example

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

Implementation Notes

  • Keep the input contract explicit: Token stream; Context-free grammar.
  • Use this step as the implementation spine: Create a chart column for each input position.
  • Then preserve the next state change: Predict rules for expected nonterminals.
  • Name the DP state and transition before writing loops.
  • Keep tokenization separate from parsing unless the algorithm explicitly combines them.

Common Pitfalls

  • Earley parsing is flexible but heavier than specialized deterministic parsers.
  • Small grammar changes can introduce ambiguity or precedence mistakes.
  • Producing unclear errors that do not point to the token or rule that failed.
  • Returning parse forest or recognition result without handling empty, invalid, or boundary inputs.

Interactive Mechanism Walkthrough

Sequence Window & Automaton Alignment

Show chart columns filling with dotted grammar states.

Input:ABABABCABAB
Window:ABAB
Step 1 of 5: Create a chart column for each input position
earley-chart

Notes

Earley Parser operates in O(n^2) time and O(n^2) space within the Chart Parsing family. Often compared with CYK Algorithm and Packrat Parsing.

Sources (2)

What each reference supports for this entry.

Earley Parsing

Paper

Original Earley parser paper supporting chart states, prediction, scanning, and completion.

Open source

Parsing Techniques

Book

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

Open source

Explore Next

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

Alternative toCYK Algorithm

Earley and CYK parsing both recognize context-free grammars with dynamic programming.

Tags

ParsingChart ParsingGrammarDynamic Programming