<bdo id='9Ss83'></bdo><ul id='9Ss83'></ul>

<small id='9Ss83'></small><noframes id='9Ss83'>

  • <tfoot id='9Ss83'></tfoot>

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

        boost::spirit::qi 重复解析输出

        时间:2023-06-29
          <legend id='vtZYf'><style id='vtZYf'><dir id='vtZYf'><q id='vtZYf'></q></dir></style></legend>
              <tbody id='vtZYf'></tbody>
          1. <i id='vtZYf'><tr id='vtZYf'><dt id='vtZYf'><q id='vtZYf'><span id='vtZYf'><b id='vtZYf'><form id='vtZYf'><ins id='vtZYf'></ins><ul id='vtZYf'></ul><sub id='vtZYf'></sub></form><legend id='vtZYf'></legend><bdo id='vtZYf'><pre id='vtZYf'><center id='vtZYf'></center></pre></bdo></b><th id='vtZYf'></th></span></q></dt></tr></i><div id='vtZYf'><tfoot id='vtZYf'></tfoot><dl id='vtZYf'><fieldset id='vtZYf'></fieldset></dl></div>
            <tfoot id='vtZYf'></tfoot>

                <bdo id='vtZYf'></bdo><ul id='vtZYf'></ul>

                • <small id='vtZYf'></small><noframes id='vtZYf'>

                • 本文介绍了boost::spirit::qi 重复解析输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个使用 Boost::Spirit 的非常简单的解析器:

                  I have this very simple parser using Boost::Spirit:

                  rule<std::string::iterator, std::string()> zeroTo255 = (string("25") >> char_('0', '5'))
                      | (char_('2') >> char_('0', '4') >> digit)
                      | (char_('1') >> repeat[2](digit))
                      | (char_('1', '9') >> digit) | digit;
                  

                  当我尝试解析时

                  std::string o{"1"};
                  std::string s;
                  parse(o.begin(), o.end(), zeroTo255, s);
                  std::cout << o << ": " << s << std::endl;
                  

                  我有作为输出

                  1: 111
                  

                  我显然做错了什么,但是什么?

                  I'm obviously doing something wrong, but what?

                  推荐答案

                  qi::hold 是一种解决方案,正如@Andrzej 正确提到的

                  qi::hold is one way about it, as correctly mentioned by @Andrzej

                  我想我有一些观察可能会有所帮助,以及更好的解决方案.

                  I think I have a few observations that might help, as well as a better solution.

                  关键是 Spirit 设计上不需要属性的临时"存储.事实上,它不能真正假设该属性首先是可复制的.这就是这里的原因(想象一下将所有内容解析为单个 std::vector<> 并为每个解析器步骤复制?).

                  The point is that Spirit will not require 'temp' storage for attributes by design. In fact, it can't really assume the attribute be copyable in the first place. This is the reason here (imagine parsing everything into a single std::vector<> and copying for each parser step?).

                  在更重要的层面上,在我看来,这里倒退的不是属性处理,而是解析器表达式本身:它没有说明意图,并导致各种处理数字表示的复杂性当......真的不应该.

                  On a more essential level, it looks to me as if it is not the attribute handling that is backwards here, but the parser expression itself: It fails to state the intent, and incurs all kinds of complexity dealing with number representations when... really it shouldn't.

                  我的看法是

                  rule<std::string::iterator, std::string()> zeroTo255, alternatively;
                  
                  alternatively %= raw [ uint_ [ _pass = (_1 <= 255) ] ];
                  

                  你看:你让 Spirit 解析一个数字,实际上只是验证范围,这就是你首先想要做的.

                  You see: you let Spirit parse a number, and indeed just verify the range, which is what you wanted to do in the first place.

                  让我印象深刻的第二件事是规则公开了一个 std::string 属性,而不是 unsigned char 例如这是为什么?

                  The second thing that strikes me as a-typical, is the fact that the rule exposes a std::string attribute, instead of unsigned char e.g. Why is that?

                  假设这是一个有意识的设计决定,您可以通过明智地使用

                  Assuming this was a conscious design decision, you can have it your way by judicious use of

                  • 否定前瞻 (!parser) - 不影响属性
                  • 正向前瞻 (&parser) - 不影响属性
                  • 熟悉qi::as_stringqi::rawqi::lexemeqi::no_skip
                  • 语义动作(不要依赖自动规则)
                  • negative lookahead (!parser) - which doesn't affect attributes
                  • positive lookahead (&parser) - which doesn't affect attributes
                  • Get acquainted with qi::as_string, qi::raw, qi::lexeme and qi::no_skip
                  • semantic actions (don't rely on automatic rules)

                  以下是对您的原始规则进行的最小改动本可以起作用:

                  Here's what minimal change to your original rule would have worked:

                  zeroTo255 = raw [ 
                            ("25" >> char_("0-5"))
                          | ('2' >> char_("0-4") >> digit)
                          | ('1' >> digit >> digit)
                          | (char_("1-9") >> digit) 
                          | digit
                      ];
                  

                  这与使用 qi::hold 的代码效果大致相同,但不是 _hold_ing 属性值的性能缺陷.

                  This has roughly the same effect as the code using qi::hold but not the performance drawback of _hold_ing attribute values.

                  希望这会有所帮助.

                  完整示例:http://liveworkspace.org/code/4v4CQW$0:

                  Full sample: Live on http://liveworkspace.org/code/4v4CQW$0:

                  #include <boost/spirit/include/qi.hpp>
                  #include <boost/spirit/include/phoenix.hpp>
                  
                  namespace qi = boost::spirit::qi;
                  
                  int main()
                  {
                      using namespace qi;
                      rule<std::string::iterator, std::string()> zeroTo255, alternatively;
                  
                      zeroTo255 = raw [ 
                                ("25" >> char_("0-5"))
                              | ('2' >> char_("0-4") >> digit)
                              | ('1' >> digit >> digit)
                              | (char_("1-9") >> digit) 
                              | digit
                          ];
                  
                      alternatively %= raw [ uint_ [ _pass = (_1 <= 255) ] ];
                  
                      for (auto& input : std::vector<std::string> { "255", "249", "178", "30", "4" })
                      {
                          std::string output;
                          std::cout << "zeroTo255:	" << std::boolalpha 
                                    << parse(std::begin(input), std::end(input), zeroTo255, output) 
                                    << ": " << output << std::endl;
                  
                          output.clear();
                          std::cout << "alternatively:	" << std::boolalpha 
                                    << parse(std::begin(input), std::end(input), alternatively, output) 
                                    << ": " << output << std::endl;
                      }
                  
                  }
                  

                  输出

                  zeroTo255:      true: 255
                  alternatively:  true: 255
                  zeroTo255:      true: 249
                  alternatively:  true: 249
                  zeroTo255:      true: 178
                  alternatively:  true: 178
                  zeroTo255:      true: 30
                  alternatively:  true: 30
                  zeroTo255:      true: 4
                  alternatively:  true: 4
                  

                  这篇关于boost::spirit::qi 重复解析输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我可以重新定义一个 C++ 宏然后再定义它吗? 下一篇:如何使用带有成员函数的 boost 绑定

                  相关文章

                  <tfoot id='BZ0WK'></tfoot><legend id='BZ0WK'><style id='BZ0WK'><dir id='BZ0WK'><q id='BZ0WK'></q></dir></style></legend>

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

                        <bdo id='BZ0WK'></bdo><ul id='BZ0WK'></ul>