Earley Nullable Chart Parsing
Follow a chart parser through nullable productions, left recursion and ambiguous derivations.
By Riano · Published · Updated · Report a correction
Earley Nullable Chart Parsing Theory
The Earley parser is an algorithm for parsing strings that belong to a given context-free grammar. Unlike naive top-down or bottom-up approaches, it handles left-recursive rules, nullable productions, and ambiguous grammars gracefully, typically running in O(N³) time for general CFGs and O(N²) for unambiguous ones.
Worked Example
Let us trace the Earley algorithm for the grammar Matched pairs: S → a S b | ε on the word aabb of length 4. The initial state adds the start rule to Chart 0.
- Chart 0 (Initial): We predict
S → a S bandS → ε. SinceScan be empty, we also bypass the nullable symbol and completeSTART → S •(0). - Chart 1 (Token a): The parser scans
afromS → • a S bin Chart 0, addingS → a • S b(0) to Chart 1. We then predict the same rules forSin Chart 1. - Chart 2 (Token a): We scan the second
a, creatingS → a • S b(1) in Chart 2. Again, we predict rules forS. - Chart 3 (Token b): Scanning
brequires completing anSfirst. The nullable predictionS → •(2) in Chart 2 is instantly completed, advancingS → a S • b(1) in Chart 2. This rule can now scan theb, addingS → a S b •(1) to Chart 3. - Chart 4 (Token b): In Chart 3, completing
Striggers completion in Chart 1, creatingS → a S • b(0) in Chart 3. Scanning the finalbproducesS → a S b •(0) in Chart 4. Finally,START → S •(0) is completed in Chart 4, proving the word is accepted. The parse count is exactly 1.
Experiment
Open the Earley Nullable Chart Parsing interactive module to explore how the chart states change with different grammars and input words.
- Select the Grammar "Ambiguous concatenation" (S → S S | a).
- Change the Word to "aaabbb". Observe how the parser immediately halts expansion for tokens not matching the grammar, leaving later charts empty and producing a "No" for acceptance.
- Change the Word back to "aa". The parser populates multiple states tracking the ambiguities. The independent parse tree count for "aa" is evaluated to 1, while "aaa" (or higher) will show Catalan number growth.
Limitations
The visualization assumes a finite predefined set of grammars and fixed input tokens to demonstrate nullable closures and recursive descents. It is not a general-purpose grammar editor. While Earley parsing can technically handle any CFG, tracking the exact parse tree counts for highly ambiguous grammars (like S → S S | ε) without loops requires more advanced shared packed parse forest (SPPF) extraction. Here, parse counts are computed independently via dynamic programming or Catalan numbers for the known domain. Numerical limits apply to precision if string lengths exceed Javascript's exact integer capabilities.
References
The Earley parser was introduced by Jay Earley in his seminal 1970 paper. For historical context and deep dive into the algorithm's origins:
Earley, J. (1968). "An Efficient Context-Free Parsing Algorithm"— This technical report describes the foundational mechanism for maintaining sets of items to parse general context-free grammars efficiently, establishing the core predictability and completion phases simulated in the interactive charts.
Experience it interactively
Adjust parameters, observe in real time, and build deep intuition with Riano’s interactive Earley Nullable Chart Parsing module.
Try Earley Nullable Chart Parsing on Riano →