• <tfoot id='1Lvpa'></tfoot>

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

        <small id='1Lvpa'></small><noframes id='1Lvpa'>

        使用语义动作解析逗号分隔的范围和数字列表

        时间:2023-07-19
        <i id='zaba3'><tr id='zaba3'><dt id='zaba3'><q id='zaba3'><span id='zaba3'><b id='zaba3'><form id='zaba3'><ins id='zaba3'></ins><ul id='zaba3'></ul><sub id='zaba3'></sub></form><legend id='zaba3'></legend><bdo id='zaba3'><pre id='zaba3'><center id='zaba3'></center></pre></bdo></b><th id='zaba3'></th></span></q></dt></tr></i><div id='zaba3'><tfoot id='zaba3'></tfoot><dl id='zaba3'><fieldset id='zaba3'></fieldset></dl></div>

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

                <tbody id='zaba3'></tbody>
              <tfoot id='zaba3'></tfoot>

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

              <legend id='zaba3'><style id='zaba3'><dir id='zaba3'><q id='zaba3'></q></dir></style></legend>

                • 本文介绍了使用语义动作解析逗号分隔的范围和数字列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用 Boost.Spirit X3,我想解析逗号- 将范围和单个数字(例如 1-4、6、7、9-12)的分隔列表放入单个 std::vector.这是我想出的:

                  Using Boost.Spirit X3, I want to parse a comma-separated list of ranges and individual numbers (e.g. 1-4, 6, 7, 9-12) into a single std::vector<int>. Here's what I've come up with:

                  namespace ast {
                      struct range 
                      {
                          int first_, last_;    
                      };    
                  
                      using expr = std::vector<int>;    
                  }
                  
                  namespace parser {        
                      template<typename T>
                      auto as_rule = [](auto p) { return x3::rule<struct _, T>{} = x3::as_parser(p); };
                  
                      auto const push = [](auto& ctx) { 
                          x3::_val(ctx).push_back(x3::_attr(ctx)); 
                      };  
                  
                      auto const expand = [](auto& ctx) { 
                          for (auto i = x3::_attr(ctx).first_; i <= x3::_attr(ctx).last_; ++i) 
                              x3::_val(ctx).push_back(i);  
                      }; 
                  
                      auto const number = x3::uint_;
                      auto const range  = as_rule<ast::range> (number >> '-' >> number                   ); 
                      auto const expr   = as_rule<ast::expr>  ( -(range [expand] | number [push] ) % ',' );
                  } 
                  

                  给定输入

                      "1,2,3,4,6,7,9,10,11,12",   // individually enumerated
                      "1-4,6-7,9-12",             // short-hand: using three ranges
                  

                  这被成功解析为 ( Live On Coliru ):

                  this is successfully parsed as ( Live On Coliru ):

                  OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12, 
                  OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12, 
                  

                  问题:我想我明白将语义动作 expand 应用于 range 部分是必要的,但为什么我还必须将语义动作 push 应用到 number 部分?没有它(即,对于 expr,使用简单的 ( -(range [expand] | number) % ',') 规则,单个数字不会传播到AST ( 生活在 Coliru):

                  Question: I think I understand that applying the semantic action expand to the range part is necessary, but why do I also have to apply the semantic action push to the number part? Without it (i.e. with a plain ( -(range [expand] | number) % ',') rule for expr, the individual numbers don't get propagated into the AST ( Live On Coliru ):

                  OK! Parsed: 
                  OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12, 
                  

                  额外问题:我什至需要语义动作来做到这一点吗?Spirit X3 文档似乎让他们望而却步.

                  Bonus Question: do I even need semantic actions at all to do this? The Spirit X3 documentation seems to discourage them.

                  推荐答案

                  语义动作抑制自动属性传播的FAQ.假设是语义动作会处理它.

                  The FAQ of this that semantic actions suppress automatic attribute propagation. The assumption being that the semantic action will take care of it instead.

                  通常有两种方法:

                  • 要么使用operator%= 而不是operator= 将定义分配给规则

                  • either use operator%= instead of operator= to assign the definition to the rule

                  或使用 rule<> 模板的第三个(可选)模板参数,可以将其指定为 true 以强制自动传播语义.

                  or use the third (optional) template argument to the rule<> template, which can be specified as true to force automatic propagation semantics.

                  在这里,我主要通过删除范围规则本身内的语义操作来简化.现在,我们可以完全删除 ast::range 类型.没有更多的融合适应.

                  Here, I simplify mostly by removing the semantic action inside the range rule itself. Now, we can drop the ast::range type altogether. No more fusion adaptation.

                  相反,我们使用 numer>>'-'>>number 的自然"合成属性,它是 int 的融合序列(fusion::deque).

                  Instead we use the "naturally" synthesized attribute of numer>>'-'>>number which is a fusion sequence of ints (fusion::deque<int, int> in this case).

                  现在,剩下要做的就是确保 | 的分支产生兼容的类型.一个简单的 repeat(1)[] 解决了这个问题.

                  Now, all that's left to make it work, is to make sure the branches of | yield compatible types. A simple repeat(1)[] fixes that.

                  生活在 Coliru

                  #include <boost/spirit/home/x3.hpp>
                  #include <iostream>
                  
                  namespace x3 = boost::spirit::x3;
                  
                  namespace ast {
                      using expr = std::vector<int>;    
                  
                      struct printer {
                          std::ostream& out;
                  
                          auto operator()(expr const& e) const {
                              std::copy(std::begin(e), std::end(e), std::ostream_iterator<expr::value_type>(out, ", "));;
                          }
                      };    
                  }
                  
                  namespace parser {        
                      auto const expand = [](auto& ctx) { 
                          using boost::fusion::at_c;
                  
                          for (auto i = at_c<0>(_attr(ctx)); i <= at_c<1>(_attr(ctx)); ++i) 
                              x3::_val(ctx).push_back(i);  
                      }; 
                  
                      auto const number = x3::uint_;
                      auto const range  = x3::rule<struct _r, ast::expr> {} = (number >> '-' >> number) [expand]; 
                      auto const expr   = x3::rule<struct _e, ast::expr> {} = -(range | x3::repeat(1)[number]  ) % ',';
                  } 
                  
                  template<class Phrase, class Grammar, class Skipper, class AST, class Printer>
                  auto test(Phrase const& phrase, Grammar const& grammar, Skipper const& skipper, AST& data, Printer const& print)
                  {
                      auto first = phrase.begin();
                      auto last = phrase.end();
                      auto& out = print.out;
                  
                      auto const ok = phrase_parse(first, last, grammar, skipper, data);
                      if (ok) {
                          out << "OK! Parsed: "; print(data); out << "
                  ";
                      } else {
                          out << "Parse failed:
                  ";
                          out << "	 on input: " << phrase << "
                  ";
                      }
                      if (first != last)
                          out << "	 Remaining unparsed: '" << std::string(first, last) << '
                  ';    
                  }
                  
                  int main() {
                      std::string numeric_tests[] =
                      {
                          "1,2,3,4,6,7,9,10,11,12",   // individually enumerated
                          "1-4,6-7,9-12",             // short-hand: using three ranges
                      };
                  
                      for (auto const& t : numeric_tests) {
                          ast::expr numeric_data;
                          test(t, parser::expr, x3::space, numeric_data, ast::printer{std::cout});
                      }
                  }
                  

                  打印:

                  OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12, 
                  OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12, 
                  

                  这篇关于使用语义动作解析逗号分隔的范围和数字列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:制作 boost::interprocess 共享内存对象的非共享副本 下一篇:带有 boost::any 的 cout 地图

                  相关文章

                  <tfoot id='arf3z'></tfoot>

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

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