Class Earley

java.lang.Object
org.libxmq.imp.ixml.earley.Earley

public final class Earley extends Object
Parse an input with an ixml grammar using the Earley parser, in four steps: 1. EarleyParser.recognize scan the UTF-8 bytes of the input into tokens (Chart) and build the Earley sets for them. 2. EarleyParser.removeForbiddenByNots drop items that a not forbids (to be implemented). 3. TreeBuilder.build compose the lowest cost tree from the sets. 4. XmlWriter.toXml serialize the tree following the ixml marks. EarleyTrace writes the sets and the tree from the result, as a trace document (xmq --traceixml=file) or in the line format of the C parser (xmq --debug=ixml.). Run it from the command line to look at the result: java -cp build/classes org.libxmq.imp.ixml.earley.Earley [--trace] grammar.ixml input.txt java -cp build/classes org.libxmq.imp.ixml.earley.Earley [--trace] grammar.ixml -i 'input'
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    The result of a parse: a document, or an error at a position.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The start of the error message of an input that is not valid UTF-8, see Chart.InvalidUtf8.
  • Method Summary

    Modifier and Type
    Method
    Description
    expectedAt(String grammar_source, byte[] input, int position)
    What the Earley parser expected where it rejected the input, for the failure message of another parser (IXMLAnalyzer).
    static Grammar
    grammar(String grammar_source)
    Build the Earley grammar from an ixml grammar source.
    static void
    main(String[] args)
    Command line: [--trace] grammar.ixml (input.txt | -i input).
    parse(String grammar_source, byte[] input)
    Parse the input with the grammar.
    parse(Grammar grammar, byte[] buf, int start, int stop)
    Parse the input with the grammar.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • INVALID_UTF8

      public static final String INVALID_UTF8
      The start of the error message of an input that is not valid UTF-8, see Chart.InvalidUtf8.
      See Also:
  • Method Details

    • grammar

      public static Grammar grammar(String grammar_source)
      Build the Earley grammar from an ixml grammar source. The grammar can be used for many parses, it keeps the tokens of the code points seen in the inputs (see Tokens), but not by several threads at the same time.
      Parameters:
      grammar_source - the ixml grammar
      Returns:
      the grammar
    • expectedAt

      public static List<EarleyParser.Expected> expectedAt(String grammar_source, byte[] input, int position)
      What the Earley parser expected where it rejected the input, for the failure message of another parser (IXMLAnalyzer).
      Parameters:
      grammar_source - the ixml grammar
      input - the rejected input, as UTF-8
      position - the code point position where the other parser failed
      Returns:
      what could come next, or an empty list if the Earley parser does not reject the input at the same position (eg a not, which it does not check yet)
    • parse

      public static Earley.Result parse(String grammar_source, byte[] input)
      Parse the input with the grammar.
      Parameters:
      grammar_source - the ixml grammar
      input - the input, as UTF-8
      Returns:
      the result
    • parse

      public static Earley.Result parse(Grammar grammar, byte[] buf, int start, int stop)
      Parse the input with the grammar. The code points are decoded directly from the bytes, the input is never turned into a String.
      Parameters:
      grammar - the grammar, from grammar(), the tokens of the input are added to it
      buf - the input as UTF-8
      start - the first byte of the input
      stop - the end of the input, exclusive
      Returns:
      the result, an input that is not valid UTF-8 gives an error starting with INVALID_UTF8
    • main

      public static void main(String[] args) throws Exception
      Command line: [--trace] grammar.ixml (input.txt | -i input). Prints the tree as xmq, with --trace the trace (EarleyTrace) is written to stderr.
      Parameters:
      args - the arguments
      Throws:
      Exception - on io errors