Class Earley
java.lang.Object
org.libxmq.imp.ixml.earley.Earley
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 ClassesModifier and TypeClassDescriptionstatic final recordThe result of a parse: a document, or an error at a position. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe start of the error message of an input that is not valid UTF-8, see Chart.InvalidUtf8. -
Method Summary
Modifier and TypeMethodDescriptionstatic 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).static GrammarBuild the Earley grammar from an ixml grammar source.static voidCommand line: [--trace] grammar.ixml (input.txt | -i input).static Earley.ResultParse the input with the grammar.static Earley.ResultParse the input with the grammar.
-
Field Details
-
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
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 grammarinput- the rejected input, as UTF-8position- 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
Parse the input with the grammar.- Parameters:
grammar_source- the ixml grammarinput- the input, as UTF-8- Returns:
- the result
-
parse
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 itbuf- the input as UTF-8start- the first byte of the inputstop- 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
-