earleyRecognize
Recognize if tokens can be parsed by a CFG grammar using Earley algorithm.Parameters
Array of tokens to recognize
Parsed CFG grammar from
parseCfgGrammarOverride start symbol from grammar
Returns
true if tokens can be parsed by the grammar, false otherwise.
Example
Algorithm
Earley parsing is a top-down chart parsing algorithm:- Prediction: Add states for rules starting with expected nonterminal
- Scanning: Advance dot past matching terminal
- Completion: Advance parent states when rule is complete
- Works with any CFG (no CNF conversion)
- More efficient for certain grammars
- Natural left-to-right processing
earleyParse
Parse tokens using Earley recognition and chart parsing.Parameters
Array of tokens to parse
Parsed CFG grammar from
parseCfgGrammarMaximum number of parse trees to return
Override start symbol from grammar
Returns
Array of parse trees (up tomaxTrees). Returns empty array if input cannot be parsed.
Each tree has:
label: string - Node label (nonterminal or terminal)children: Array of ParseTree or string - Child nodes or terminal strings
Example
Implementation
- Uses
earleyRecognizeto check if input is parsable - If recognized, uses
chartParseto build parse trees - Returns empty array for unparsable input
parseTextWithEarley
Parse natural language text using Earley algorithm.Parameters
Natural language text to parse
Parsed grammar or grammar text string
Maximum parse trees to return
Override grammar start symbol
Convert tokens to lowercase before parsing
Returns
Array of parse trees. SeeearleyParse for tree structure.
Example
Processing
- Tokenizes text using word tokenizer
- Filters to alphanumeric tokens
- Normalizes to lowercase (if enabled)
- Parses with
earleyParse
When to Use Earley
Prefer Earley parser when:- Grammar has many unary or epsilon rules
- Left-recursive grammars (after transformation)
- Real-time parsing requirements
- Grammar is frequently modified
parseTextWithCfg) when:
- Grammar is fixed and can be optimized
- Need all parse trees efficiently
- Grammar naturally fits CNF