private CflParse LeftDerivative(CflParse[] tokens, string input) { var length = tokens.Length; var dv = new LinkedList <CflParse>(); var at = -1; while (at < length) { while (dv.Count >= 2) { var left = dv.Last.Previous.Value; var right = dv.Last.Value; var reductions = Grammar.Lookup(left.Productions, right.Productions); if (reductions != null) { var last = new CflParse(reductions, left, right); dv.RemoveLast(); dv.RemoveLast(); dv.AddLast(last); } else { break; } } at++; if (at < length) { dv.AddLast(tokens[at]); } } return(dv.Count == 1 ? dv.Last.Value : Error(input)); }
public override CflParse Parse(string input) { var tokens = Tokenize(input); var length = tokens.Length; var matrix = new CflParse[length, length]; for (var s = 1; s <= length; s++) { matrix[0, s - 1] = tokens[s - 1]; } for (var l = 2; l <= length; l++) { for (var s = 1; s <= length - l + 1; s++) { for (var p = 1; p <= l - 1; p++) { var left = matrix[p - 1, s - 1]; var right = matrix[l - p - 1, s + p - 1]; if ((left != null) && (right != null)) { var reductions = Grammar.Lookup(left.Productions, right.Productions); if (reductions != null) { if (matrix[l - 1, s - 1] != null) { matrix[l - 1, s - 1].Productions.UnionWith(reductions); } else { matrix[l - 1, s - 1] = new CflParse(reductions, left, right); } } } } } } matrix[length - 1, 0] = matrix[length - 1, 0] ?? Error(input); return(matrix[length - 1, 0]); }
private CflParse RightDerivative(CflParse[] tokens, string input) { var length = tokens.Length; var dv = new LinkedList <CflParse>(); var at = length; while (at >= 0) { while (dv.Count >= 2) { // Rewriting via R2 // Lc Rc ~> Lc (U head(Rc) head(tail(Rc))) tail(tail(Rc)) var left = dv.First.Value; var right = dv.First.Next.Value; var reductions = Grammar.Lookup(left.Productions, right.Productions); if (reductions != null) { var first = new CflParse(reductions, left, right); dv.RemoveFirst(); dv.RemoveFirst(); dv.AddFirst(first); } else { break; } } at--; if (at >= 0) { // Rewriting via R1 // Lc Rc ~> (Lc \ w) (U w) Rc dv.AddFirst(tokens[at]); } } return(dv.Count == 1 ? dv.First.Value : Error(input)); }