Reputation: 139
I guess my Problem is really easy, but I tried to fix it for hours now, and I don't seem to get it. I have an ast tree (created with boost-library) and im iterating through it with recursion. I'm saving all Nodes in a List of NodeDescriptions, that contain the number of the actual node, the name of the actual Node, and node that is the parent node to the actual node. However, my parent node always has the wrong number. I guess I'm doing something wrong with the scope of my variables, passing it the wrong way, or anything like this. I would be glad if someone could help me:
void convert_to_parsetree(BOOST_SPIRIT_NAMESPACE::tree_match<iterator_t>::const_tree_iterator parse_node, int calNode) {
int remNum = calNode;
std::string node_value(parse_node->value.begin(), parse_node->value.end());
//First Element: Node-Counter, Second Element, Name of Node, Third Element: Parent Node Number
myList.push_back(NodeDescription(counter++, node_value, remNum));
if (parse_node->children.size() > 0) {
if (parse_node->children.size() > 1) {
//std::string value(parse_node->children[0].value.begin(), parse_node->children[0].value.end());
//std::string value2(parse_node->children[1].value.begin(), parse_node->children[1].value.end());
BOOST_SPIRIT_NAMESPACE::tree_match<iterator_t>::const_tree_iterator children_it = parse_node->children.begin();
for (int i = 0; i < parse_node->children.size(); ++i) {
convert_to_parsetree(children_it, counter);
children_it++;
}
} else {
convert_to_parsetree(parse_node->children.begin(), counter);
}
}
}
Quite simple, but somehow it doesn't work. Thanks in Advance and kind regards.
Upvotes: 4
Views: 1424
Reputation: 70392
The problem is that in your recursive call, you are passing the value in the global variable counter
as the second parameter. However, your recursive function uses the second parameter as the "Parent Node Number" (since it is saved in remNum
), and the global counter
gets incremented. This means the for
loop that iterates over the children with the recursive calls will be passing in a different counter
value at each iteration, even though each recursive call is supposed to be from the same "Parent".
The current level of recursion should remember the current counter
value as its node number before it is incremented, and this remembered value is what should be passed into each iteration of the for
loop.
In the fixed version of the code below, I simplified your function to improve readability.
typedef BOOST_SPIRIT_NAMESPACE::tree_match<iterator_t>::const_tree_iterator
MyTreeIterator;
void convert_to_parsetree (MyTreeIterator parse_node, int parent_number) {
int node_number = counter++;
std::string node_name(parse_node->value.begin(), parse_node->value.end());
myList.push_back(NodeDescription(node_number, node_name, parent_number));
for (MyTreeIterator children_it = parse_node->children.begin();
children_it != parse_node->children.end();
++children_it) {
convert_to_parsetree(children_it, node_number);
}
}
Upvotes: 2