Skip to content

Commit

Permalink
Fix the issue of multiple rules existing in same earley state
Browse files Browse the repository at this point in the history
Duplicate earley items should not exist concurrently in a given earley
state, but duplicate _rules_ should be allowed. This means that if two
earley items have the same rule, but different origins or positions,
they should be allowed to exist in the same state.

This will allow certain ambiguous grammars/sentences to successfully
parse that would otherwise fail.
  • Loading branch information
patgrasso committed Aug 3, 2016
1 parent 5b51cae commit f142ca1
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function predict(tokens, states, i, j, grammar) {
grammar.forEach((rule) => {
let stateHasItem = states[i].filter((earleyItem) => {
return earleyItem.rule === rule &&
curr.position === earleyItem.position;
curr.position === 0;//earleyItem.position;
}).length > 0;

if (!stateHasItem) {
Expand All @@ -55,10 +55,6 @@ function predict(tokens, states, i, j, grammar) {
position: 0,
origin : i
});

if (states[i].length > 100) {
throw new Error(JSON.stringify(states[i], null, 2));
}
}
});
}
Expand Down Expand Up @@ -98,7 +94,8 @@ function complete(tokens, states, i, j) {
if (earleyItem.rule[earleyItem.position] === curr.rule.lhs) {
let stateHasItem = states[i].filter((ei) => {
return ei.rule === earleyItem.rule &&
ei.position === earleyItem.position + 1;
ei.position === earleyItem.position + 1 &&
ei.origin === earleyItem.origin;
}).length > 0;

if (stateHasItem) {
Expand Down Expand Up @@ -138,6 +135,9 @@ function dfs(states, tokens) {
return best;
}, null);

if (root == null) {
throw new SyntaxError(`Parsing error near '${tokens[0]}' `);
}
if (root.origin !== tokens.length) {
throw new SyntaxError(`Parsing error near '${tokens[root.origin]}' `);
}
Expand Down

0 comments on commit f142ca1

Please sign in to comment.