Skip to content

Instantly share code, notes, and snippets.

@forestbelton
Created February 19, 2014 06:05
Show Gist options
  • Save forestbelton/9086832 to your computer and use it in GitHub Desktop.
Save forestbelton/9086832 to your computer and use it in GitHub Desktop.
remove left recursion
var rm_lr = function(name, grammar) {
var p_with = grammar[name].filter(function(x) { return x[0] == name; }),
p_wo = grammar[name].filter(function(x) { return x[0] != name; });
grammar[name] = p_wo.concat(p_wo.map(function(wo) {
return wo.concat([name + "'"]);
}));
grammar[name + "'"] = [['e']].concat(p_with.map(function(x) {
return x.slice(1).concat([name + "'"]);
}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment