Class EarleyParser

java.lang.Object
org.nineml.coffeegrinder.parser.EarleyParser
All Implemented Interfaces:
GearleyParser

public class EarleyParser extends Object implements GearleyParser
The Earley parser.

The Earley parser compares an input sequence against a grammar and determines if the input is a sentence in the grammar.

This is a fairly literal implementation of the parser in ยง5 of SPPF-Style Parsing From Earley Recognisers.

  • Field Details

  • Method Details

    • getParserType

      public ParserType getParserType()
      Return the parser type.
      Specified by:
      getParserType in interface GearleyParser
      Returns:
      ParserType.Earley
    • getGrammar

      public ParserGrammar getGrammar()
      Get the grammar used by this parser.
      Specified by:
      getGrammar in interface GearleyParser
      Returns:
      the grammar
    • getSeed

      public NonterminalSymbol getSeed()
      Get the NonterminalSymbol seed value used by this parser.
      Specified by:
      getSeed in interface GearleyParser
      Returns:
      the seed
    • parse

      public EarleyResult parse(String input)
      Parse an input string against the grammar.

      This is a shortcut for parsing a sequence of characters.

      Specified by:
      parse in interface GearleyParser
      Parameters:
      input - the input string
      Returns:
      a parse result
    • parse

      public EarleyResult parse(Token[] input)
      Parse an array of tokens against the grammar.

      You must not change the input array.

      Specified by:
      parse in interface GearleyParser
      Parameters:
      input - the input array
      Returns:
      a parse result
    • parse

      public EarleyResult parse(Iterator<Token> input)
      Parse a sequence of tokens against the grammar.
      Specified by:
      parse in interface GearleyParser
      Parameters:
      input - the input sequence
      Returns:
      a parse result
    • hasMoreInput

      public boolean hasMoreInput()
      Is there more input?

      If the parse succeeded, the answer will always be false. But a failed parse can fail because it was unable to process a token or because it ran out of tokens. This method checks if there was any more input after the parse completed.

      Specified by:
      hasMoreInput in interface GearleyParser
      Returns:
      true if parsing failed before the entire input was consumed
    • getLineNumber

      public int getLineNumber()
      Description copied from interface: GearleyParser
      Returns the line number of the last line read by the parser.
      Specified by:
      getLineNumber in interface GearleyParser
      Returns:
      the line number.
    • getColumnNumber

      public int getColumnNumber()
      Description copied from interface: GearleyParser
      Returns the column number of the last character on the last line read by the parser.
      Specified by:
      getColumnNumber in interface GearleyParser
      Returns:
      the column number.
    • getOffset

      public int getOffset()
      Description copied from interface: GearleyParser
      Returns the last offset read by the parser.
      Specified by:
      getOffset in interface GearleyParser
      Returns:
      the offset.