//TODO this is so horribly terrible. There's got to be a better way of thinking about this structure private void AnnotateWithProductions(SppfNode node, HashSet <SppfNode> seen = null, InteriorNode parent = null, int place = 0) { if (seen == null) { seen = new HashSet <SppfNode>(); } if (node is IntermediateNode) { var intermediateNode = (IntermediateNode)node; var production = intermediateNode.Item.Production; if (intermediateNode.Item.CurrentPosition == production.Rhs.Count - 1) { parent.AddChild(place, production); } } if (seen.Contains(node)) { return; } seen.Add(node); var l = node.Families; for (int i = 0; i < l.Count; i++) { var alternative = l[i]; if (!(node is InteriorNode)) { throw new Exception(); } var members = l[i].Members; if (members.Count == 1) { var child = members[0]; AnnotateWithProductionsChildren((InteriorNode)node, seen, child, i); } else if (members.Count == 2) { var left = members[0]; var right = members[1]; AnnotateWithProductionsChildren((InteriorNode)node, seen, left, right, i); } else { throw new Exception("Should only be 0--2 children"); } } }
private void AnnotateWithProductionsChildren(InteriorNode parent, HashSet <SppfNode> seen, SppfNode child, int place) { Word parentSymbol = null; if (parent is SymbolNode) { var symbolParent = (SymbolNode)parent; parentSymbol = symbolParent.Symbol; } else { var intermediateParent = (IntermediateNode)parent; if (intermediateParent.Item.CurrentPosition != 1) { throw new Exception("Expected to be at beginning of item"); } parentSymbol = intermediateParent.Item.Production.Rhs[0]; } if (child is SymbolNode) { var symbolChild = (SymbolNode)child; if (parent is SymbolNode) { var symbolParent = (SymbolNode)parent; var production = _grammar.FindProduction((Nonterminal)parentSymbol, new Sentence { symbolChild.Symbol }); symbolParent.AddChild(place, production); } AnnotateWithProductions(symbolChild, seen, parent, place); return; } else if (child is IntermediateNode) { throw new Exception("Don't handle intermediate"); } else if (child is LeafNode) { if (parentSymbol is Nonterminal) { var leafChild = (LeafNode)child; var childSentence = leafChild.GetSentence(); var production = _grammar.FindProduction((Nonterminal)parentSymbol, childSentence); parent.AddChild(place, production); } return; } throw new Exception(); }