Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance optimization #260

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions lib/jison.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,27 +722,32 @@ lrGeneratorMixin.gotoOperation = function gotoOperation (itemSet, symbol) {
}
});

return gotoSet.isEmpty() ? gotoSet : this.closureOperation(gotoSet);
return gotoSet;
};

/* Create unique set of item sets
* */
lrGeneratorMixin.canonicalCollection = function canonicalCollection () {
var item1 = new this.Item(this.productions[0], 0, [this.EOF]);
var firstState = this.closureOperation(new this.ItemSet(item1)),
var firstStateNoClosure = new this.ItemSet(item1),
firstState = this.closureOperation(firstStateNoClosure),
states = new Set(firstState),
marked = 0,
self = this,
itemSet;
itemSet,
markedSymbols;

states.has = {};
states.has[firstState] = 0;
states.has[firstStateNoClosure.valueOf()] = 0;

while (marked !== states.size()) {
itemSet = states.item(marked); marked++;
markedSymbols = {};
itemSet.forEach(function CC_itemSet_forEach (item) {
if (item.markedSymbol && item.markedSymbol !== self.EOF)
if (item.markedSymbol && !markedSymbols[item.markedSymbol] && item.markedSymbol !== self.EOF){
markedSymbols[item.markedSymbol] = true;
self.canonicalCollectionInsert(item.markedSymbol, itemSet, states, marked-1);
}
});
}

Expand All @@ -751,21 +756,22 @@ lrGeneratorMixin.canonicalCollection = function canonicalCollection () {

// Pushes a unique state into the que. Some parsing algorithms may perform additional operations
lrGeneratorMixin.canonicalCollectionInsert = function canonicalCollectionInsert (symbol, itemSet, states, stateNum) {
var g = this.gotoOperation(itemSet, symbol);
if (!g.predecessors)
g.predecessors = {};
// add g to que if not empty or duplicate
if (!g.isEmpty()) {
var gv = g.valueOf(),
i = states.has[gv];
if (i === -1 || typeof i === 'undefined') {
states.has[gv] = states.size();
var g = this.gotoOperation(itemSet, symbol),
state = states.has[g.valueOf()];

if(state !== undefined){
itemSet.edges[symbol] = state; // store goto transition for table
states.item(state).predecessors[symbol].push(stateNum);
}else{
// add g to que if not empty or duplicate
if (!g.isEmpty()) {
states.has[g.valueOf()] = states.size();
g = this.closureOperation(g);
if (!g.predecessors)
g.predecessors = {};
itemSet.edges[symbol] = states.size(); // store goto transition for table
states.push(g);
g.predecessors[symbol] = [stateNum];
} else {
itemSet.edges[symbol] = i; // store goto transition for table
states.item(i).predecessors[symbol].push(stateNum);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Zach Carter <[email protected]> (http://zaa.ch)",
"name": "jison",
"description": "A parser generator with Bison's API",
"version": "0.4.15",
"version": "0.4.16",
"keywords": [
"jison",
"bison",
Expand Down