Class EarleyParser
java.lang.Object
org.libxmq.imp.ixml.earley.EarleyParser
A plain Earley recognizer.
The parser builds one Earley set for each input position. Set k is filled
by three operations, applied to every item in it until nothing new is added:
- Predict: the item expects a rule B, add (B → · ..., k) for every
production of B: B may start here.
- Scan: the item expects a terminal that matches the code point at k,
add the item with the dot moved over it to set k+1.
- Complete: the item is complete, a rule A matched from its origin j to k.
Every item in set j that was waiting for A moves its dot over A
and is added to set k.
The input is accepted when the last set has a complete start rule item with
origin 0.
An empty match (a nullable rule) completes in the same set it was predicted
in, which plain Earley can miss when the waiting item is added after the
completion. So, as Aycock and Horspool suggest, when an item expects a
nullable rule, the dot is also moved over the rule right away.
Insertions and nots match no input, the dot is moved over them directly.
A not is not checked here, see removeForbiddenByNots, which runs on the
finished chart before the tree is built.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordWhat may come next at a position, in a grammar rule. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns true if the chart accepts the whole input: the last set has a complete item of the start rule that began at position 0.What the parser could read at the position: the terminals right after the dot of the items in the set, with the characters that follow them in the item, and the end of the input if the start rule is complete there.static StringexpectedLines(List<EarleyParser.Expected> expected) The expected as lines, one per rule, like the C version: "expected verb: 'gives', 'goes' or 'is'" and "expected end of input".intfailurePosition(Chart chart) The position of the first code point that could not be parsed: the last set that has items.recognize(byte[] buf, int start, int stop) Run the recognizer over the input.voidremoveForbiddenByNots(Chart chart) Remove the items that a not forbids, from the finished chart, before the tree is built.
-
Constructor Details
-
EarleyParser
Create a parser for the grammar.- Parameters:
grammar- the grammar
-
-
Method Details
-
recognize
Run the recognizer over the input. The input is read directly from its UTF-8 bytes, see Chart.- Parameters:
buf- the input bytesstart- the first byte of the inputstop- the end of the input, exclusive- Returns:
- the chart, check accepted() for the result
- Throws:
Chart.InvalidUtf8- if the input is not valid UTF-8
-
accepted
Returns true if the chart accepts the whole input: the last set has a complete item of the start rule that began at position 0.- Parameters:
chart- the chart from recognize- Returns:
- true if accepted
-
expected
What the parser could read at the position: the terminals right after the dot of the items in the set, with the characters that follow them in the item, and the end of the input if the start rule is complete there. Used for the failure message at failurePosition, and for completion. Sorted by rule and text, by their utf-8 bytes like the C version, the end of the input last.- Parameters:
chart- the chart from recognizeposition- the input position- Returns:
- what may come next, each rule and text once
-
expectedLines
The expected as lines, one per rule, like the C version: "expected verb: 'gives', 'goes' or 'is'" and "expected end of input".- Parameters:
expected- from expected()- Returns:
- the lines, each starting with a newline
-
failurePosition
The position of the first code point that could not be parsed: the last set that has items. If all sets have items, the input ended too early and the position is the input length.- Parameters:
chart- the chart from recognize- Returns:
- the input position
-
removeForbiddenByNots
Remove the items that a not forbids, from the finished chart, before the tree is built. Not implemented yet. An item that moved its dot over a not !x at position k is forbidden if the input at k starts with x. Removing such items, and the items that were made from them, leaves only the derivations that respect the nots. Until then the nots are ignored, as if they were not there.- Parameters:
chart- the chart from recognize
-