public virtual void Walk(IParseTreeListener listener, IParseTree t) { if (t is IErrorNode) { listener.VisitErrorNode((IErrorNode)t); return; } else { if (t is ITerminalNode) { listener.VisitTerminal((ITerminalNode)t); return; } } IRuleNode r = (IRuleNode)t; EnterRule(listener, r); int n = r.ChildCount; for (int i = 0; i < n; i++) { Walk(listener, r.GetChild(i)); } ExitRule(listener, r); }
public virtual string Walk(IParseTreeListener listener, IParseTree t) { counter++; if (t is IErrorNode) { listener.VisitErrorNode((IErrorNode)t); txt = txt + t.ToString(); return(txt); } else { if (t is ITerminalNode) { listener.VisitTerminal((ITerminalNode)t); return(txt); } } IRuleNode r = (IRuleNode)t; string parentname = r.GetText(); //Console.WriteLine("##### PARENT " + counter + "####" + parentname + "#####"); EnterRule(listener, r); int n = r.ChildCount; for (int i = 0; i < n; i++) { Walk(listener, r.GetChild(i)); Console.WriteLine("##### CHILD " + i + " of " + parentname + "####" + r.GetChild(i).GetText() + "#####"); } ExitRule(listener, r); return(txt); }
public virtual void Walk(IParseTreeListener listener, IParseTree t) { if (t is IErrorNode) { listener.VisitErrorNode((IErrorNode)t); return; } else { if (t is ITerminalNode) { listener.VisitTerminal((ITerminalNode)t); return; } } IRuleNode r = (IRuleNode)t; EnterRule(listener, r); int n = r.ChildCount; for (int i = 0; i < n; i++) { Walk(listener, r.GetChild(i)); } ExitRule(listener, r); }
public virtual void Walk(IParseTreeListener listener, IParseTree t) { Stack<IParseTree> nodeStack = new Stack<IParseTree>(); Stack<int> indexStack = new Stack<int>(); IParseTree currentNode = t; int currentIndex = 0; while (currentNode != null) { // pre-order visit if (currentNode is IErrorNode) { listener.VisitErrorNode((IErrorNode)currentNode); } else { if (currentNode is ITerminalNode) { listener.VisitTerminal((ITerminalNode)currentNode); } else { IRuleNode r = (IRuleNode)currentNode; EnterRule(listener, r); } } // Move down to first child, if exists if (currentNode.ChildCount > 0) { nodeStack.Push(currentNode); indexStack.Push(currentIndex); currentIndex = 0; currentNode = currentNode.GetChild(0); continue; } do { // No child nodes, so walk tree // post-order visit if (currentNode is IRuleNode) { ExitRule(listener, (IRuleNode)currentNode); } // No parent, so no siblings if (nodeStack.Count == 0) { currentNode = null; currentIndex = 0; break; } // Move to next sibling if possible currentNode = nodeStack.Peek().GetChild(++currentIndex); if (currentNode != null) { break; } // No next sibling, so move up currentNode = nodeStack.Pop(); currentIndex = indexStack.Pop(); } while (currentNode != null); } }
public virtual void Walk(IParseTreeListener listener, IParseTree t) { Stack <IParseTree> nodeStack = new Stack <IParseTree>(); Stack <int> indexStack = new Stack <int>(); IParseTree currentNode = t; int currentIndex = 0; while (currentNode != null) { // pre-order visit if (currentNode is IErrorNode) { listener.VisitErrorNode((IErrorNode)currentNode); } else { if (currentNode is ITerminalNode) { listener.VisitTerminal((ITerminalNode)currentNode); } else { IRuleNode r = (IRuleNode)currentNode; EnterRule(listener, r); } } // Move down to first child, if exists if (currentNode.ChildCount > 0) { nodeStack.Push(currentNode); indexStack.Push(currentIndex); currentIndex = 0; currentNode = currentNode.GetChild(0); continue; } do { // No child nodes, so walk tree // post-order visit if (currentNode is IRuleNode) { ExitRule(listener, (IRuleNode)currentNode); } // No parent, so no siblings if (nodeStack.Count == 0) { currentNode = null; currentIndex = 0; break; } // Move to next sibling if possible currentNode = nodeStack.Peek().GetChild(++currentIndex); if (currentNode != null) { break; } // No next sibling, so move up currentNode = nodeStack.Pop(); currentIndex = indexStack.Pop(); }while (currentNode != null); } }