1. <legend id='FuYyE'><style id='FuYyE'><dir id='FuYyE'><q id='FuYyE'></q></dir></style></legend>
    2. <tfoot id='FuYyE'></tfoot>

        <bdo id='FuYyE'></bdo><ul id='FuYyE'></ul>
      <i id='FuYyE'><tr id='FuYyE'><dt id='FuYyE'><q id='FuYyE'><span id='FuYyE'><b id='FuYyE'><form id='FuYyE'><ins id='FuYyE'></ins><ul id='FuYyE'></ul><sub id='FuYyE'></sub></form><legend id='FuYyE'></legend><bdo id='FuYyE'><pre id='FuYyE'><center id='FuYyE'></center></pre></bdo></b><th id='FuYyE'></th></span></q></dt></tr></i><div id='FuYyE'><tfoot id='FuYyE'></tfoot><dl id='FuYyE'><fieldset id='FuYyE'></fieldset></dl></div>

      <small id='FuYyE'></small><noframes id='FuYyE'>

      1. 语义动作在 boost::spirit 解析中多次运行

        时间:2023-07-18

        <small id='vXm1s'></small><noframes id='vXm1s'>

            <bdo id='vXm1s'></bdo><ul id='vXm1s'></ul>
            1. <legend id='vXm1s'><style id='vXm1s'><dir id='vXm1s'><q id='vXm1s'></q></dir></style></legend>
              1. <tfoot id='vXm1s'></tfoot>

                  <tbody id='vXm1s'></tbody>

                <i id='vXm1s'><tr id='vXm1s'><dt id='vXm1s'><q id='vXm1s'><span id='vXm1s'><b id='vXm1s'><form id='vXm1s'><ins id='vXm1s'></ins><ul id='vXm1s'></ul><sub id='vXm1s'></sub></form><legend id='vXm1s'></legend><bdo id='vXm1s'><pre id='vXm1s'><center id='vXm1s'></center></pre></bdo></b><th id='vXm1s'></th></span></q></dt></tr></i><div id='vXm1s'><tfoot id='vXm1s'></tfoot><dl id='vXm1s'><fieldset id='vXm1s'></fieldset></dl></div>
                  本文介绍了语义动作在 boost::spirit 解析中多次运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在使用 boost::spirit 解析时创建具有语义规则的 AST.AST 必须只为输入的一部分构建,输入的另一部分应该在没有语法树的情况下进行解析.

                  I am trying to create AST with semantic rules while parsing with boost::spirit. AST must be built only for piece of the input, another part of the input should be parsed without sintax tree.

                  例如,对于这样的输入字符串:self.usedFoo(Bar).filter(self.baz > baz)"或self.Foo.filter(true)">)" AST 应该只为粗体部分构建.

                  For example, for such input strings: "self.usedFoo(Bar).filter(self.baz > baz)" or "self.Foo.filter(true)" AST should be build only for bold part.

                  还有一个问题:解析器运行多次解析语法并多次调用语义动作(实例化 AST 节点),所以我得到了可怕的内存泄漏.

                  And there is a problem: parser runs multimple times parsing grammar and calling semantic action (instatntiating AST nodes) multimple times too, so I got terrible memory leaks.

                  简单的源代码:

                  语法:

                  line = stmt | stmt >> "filter.(" >> filter >> ')';
                  filter %= (filterterm)
                  filterterm %= (filterfactor)
                  filterfactor = value [phoenix::bind(&ValueFilterSemanticNode::Instantiate, qi::_val, qi::_1)];
                  

                  实例化节点:

                    static void ValueFilterSemanticNode::Instantiate(QVariant &res, QVariant &value)
                      {
                          qDebug() << "   Creating new Value Node...";
                          ValueFilterSemanticNode *n = new ValueFilterSemanticNode();
                          qDebug() << "   " << n;
                  
                          n->value = QVariant(value.toInt());
                          res = QVariant::fromValue(n);
                      }
                  

                  输入:

                  self.filter(1)

                  self.filter(1)

                  调试:

                     Creating new Value Node...
                      0x22fdfd0
                     Creating new Value Node...
                      0x22fe030
                     Creating new Value Node...
                      0x22fde50
                     [...many many lines...]
                     Creating new Value Node...
                      0x22fe238
                     Creating new Value Node...
                      0x22fe218
                  Running Filter test
                         Value node running... 0x22fe218
                  Check result =  QVariant(int, 1)
                  

                  因此,如您所见,节点实例化过多会导致内存泄漏.

                  So, as you can see, nodes instantiating too many times that causes mem leaks.

                  推荐答案

                  即使稍后有回溯,也会触发语义操作.

                  Semantic actions fire even if there's backtracking later.

                  解析器表达式可能会抛出异常.

                  Parser expressions might throw.

                  仅出于这些原因,在语义操作中进行动态分配并不是一个好主意.如果需要,请使用智能指针(尽管这仍然效率低下).

                  For these reasons alone, it's not a good idea to do dynamic allocations in your semantic actions. If you need to, use smart pointers (though this will still be inefficient).

                  • 提升精神:语义动作是邪恶的"?
                  • 如何使用多态属性boost::spirit::qi 解析器?

                  这篇关于语义动作在 boost::spirit 解析中多次运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Boost-log 链接器错误 下一篇:将属性传递给子规则以提升精神

                  相关文章

                      • <bdo id='mSAYC'></bdo><ul id='mSAYC'></ul>
                      <legend id='mSAYC'><style id='mSAYC'><dir id='mSAYC'><q id='mSAYC'></q></dir></style></legend>

                    1. <tfoot id='mSAYC'></tfoot>

                      <small id='mSAYC'></small><noframes id='mSAYC'>

                      <i id='mSAYC'><tr id='mSAYC'><dt id='mSAYC'><q id='mSAYC'><span id='mSAYC'><b id='mSAYC'><form id='mSAYC'><ins id='mSAYC'></ins><ul id='mSAYC'></ul><sub id='mSAYC'></sub></form><legend id='mSAYC'></legend><bdo id='mSAYC'><pre id='mSAYC'><center id='mSAYC'></center></pre></bdo></b><th id='mSAYC'></th></span></q></dt></tr></i><div id='mSAYC'><tfoot id='mSAYC'></tfoot><dl id='mSAYC'><fieldset id='mSAYC'></fieldset></dl></div>