C++ 递归变量 [英] C++ Recursion variable

查看:66
本文介绍了C++ 递归变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的问题真的很简单,但我已经尝试了几个小时来解决它,但我似乎没有明白.我有一个 ast 树(用 boost-library 创建)并通过递归遍历它.我将所有节点保存在 NodeDescriptions 列表中,其中包含实际节点的编号、实际节点的名称以及作为实际节点的父节点的节点.但是,我的父节点总是有错误的编号.我想我的变量范围有问题,以错误的方式传递它,或者类似的东西.如果有人可以帮助我,我会很高兴:

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.

推荐答案

问题在于,在您的递归调用中,您将全局变量 counter 中的值作为第二个参数传递.但是,您的递归函数使用第二个参数作为父节点编号"(因为它保存在 remNum 中),并且全局 counter 会递增.这意味着使用递归调用遍历子代的 for 循环将在每次迭代中传入不同的 counter 值,即使每个递归调用都应该来自同一个父母".

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".

当前递归级别应该记住当前的counter值作为其递增之前的节点号,并且这个记住的值应该被传递到for的每次迭代中代码>循环.

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);
   }
}

这篇关于C++ 递归变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆