문제

I am using Irony parser for NET in order to get a simple structure for an algebraic-like syntax:

2 + 3 * 5
7 + (2 * 5) a.s.o.

The parsing works fine and I am using ParseTreeNode in order to get a reference for each node in my input. How can I get for a given ParseTreeNode node the parent node?

도움이 되었습니까?

해결책

Well, it's pretty simple. You just traverse all the nodes, and pick the parent :P.

Stack<ParseTreeNode> stack = new Stack { yourRootTreeNode };
while(!stack.Empty)
{
   var current = stack.Pop();
   if(current.ChildNodes != null)
   {
     if(current.ChildNodes.Contains(yourChildNode))
        return current; /*parent of yourChildNode */

     foreach(var child in current.ChildNodes)
       stack.Push(child);

   }
}

I have taken a look at ParseTreeNode and quite frankly, I can't see anything that will give you the information you need. You can either download the source and add the functionality, or use the code I have given.

Alternatively, you can build AST tree which allows your nodes to have all the properties you ever want, and make sure that nodes always have Parent property.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top