【问题标题】:Temporary attribute in boost spirit needed for parsing解析所需的 boost 精神中的临时属性
【发布时间】:2013-02-25 11:04:28
【问题描述】:

我有一个格式为“$number_of_elements $e1 $e2 $e3”的文件。我创建了以下解析器:

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/boost_tuple.hpp>


int main(int argc, char *argv[])
{
  std::string input("2 3 3\n");

  using boost::phoenix::at_c;
  using boost::spirit::qi::_1;
  using boost::spirit::qi::_r1;
  using boost::spirit::qi::double_;
  using boost::spirit::qi::omit;
  using boost::spirit::qi::int_;
  using boost::spirit::qi::repeat;
  using boost::spirit::qi::rule;
  using boost::spirit::qi::space;
  using boost::spirit::qi::space_type;
  using boost::spirit::qi::_val;

  rule<std::string::iterator, double(), space_type> r0 = double_;
  r0.name("r0");
  rule<std::string::iterator, std::vector<double>(size_t), space_type> r1 = repeat(_r1)[r0];
  r1.name("r1");
  rule<std::string::iterator, boost::tuple<size_t, std::vector<double> >(), space_type> r2
    = int_ >> r1(at_c<0>(_val));
  r2.name("r2");
  rule<std::string::iterator, std::vector<double>(), space_type> r3
    = r2[_val = at_c<1>(_1)];
  r3.name("r3");
  debug(r0);
  debug(r1);
  debug(r2);
  debug(r3);
  std::vector<double> res;
  bool success = boost::spirit::qi::phrase_parse(input.begin(),
                                                 input.end(),
                                                 r3,
                                                 space,
                                                 res);
  if (success) {
    for(std::vector<double>::iterator it = res.begin(); it != res.end(); it++) {
      std::cout << *it << " " << std::endl;
    }
  }
  return !success;
}

我想知道是否有机会避免复制。我不知道 boost phoenix (或 gcc) 说编译器是否会应用临时对象删除优化(数据量可能非常大,因此可能会影响性能)。

另外 - 是否可以将 r1 规则更改为类似的内容(除非通过重复这样做):

  rule<std::string::iterator, std::vector<double>(size_t), space_type> r1
    = eps[_val.reserve(_r1)] >> repeat(_r1)[r0];

(此行无法编译)。

附言。存储的数量可能非常大,因此虽然此时复制/重新分配可能会产生一些影响并不重要 - 但是我希望在完全致力于设计之前知道这种优化涉及什么。

PPS。我使用的是 gcc 4.4,所以我可以访问 std::move,但没有很多其他 C++11 功能。

【问题讨论】:

    标签: c++ boost boost-spirit boost-spirit-qi


    【解决方案1】:

    哇,这个我花了一些时间才弄好,首先我必须清理代码以便我可以正确阅读它(我希望你不介意),其余的都是通过语义操作完成的:

    #define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
    
    #include <boost/spirit/include/qi.hpp>  
    #include <boost/spirit/include/phoenix_core.hpp>
    #include <boost/spirit/include/phoenix_bind.hpp>
    #include <boost/spirit/include/phoenix_stl.hpp>
    
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    
    int main() {
        namespace qi=boost::spirit::qi;
        namespace phx=boost::phoenix;
    
        //we use no predefined terms to speed up compile times
        qi::double_type double_;
        qi::int_type    int_;
        qi::_1_type     _1;
        qi::_val_type   _val;
    
        std::string in="3 3.4 5.6 6.7";
        auto first=in.cbegin(),
             last =in.cend();
    
        std::vector<double> out;
    
        qi::rule<std::string::const_iterator, std::vector<double>()> r1=
            int_     
            [ 
                phx::bind(&std::vector<double>::reserve, _val, _1)
            ]
            >>    ' ' %
                double_
                [ 
                    phx::push_back(_val, _1) 
                ]
            ;
    
        qi::parse(first, last, r1, out);
    
        std::copy(out.cbegin(), out.cend(),
            std::ostream_iterator<double>(std::cout, "\n"));
    }
    

    输出:

    3.4
    5.6
    6.7
    

    可以在这里看到工作:

    http://liveworkspace.org/code/QBPdC$1

    【讨论】:

      【解决方案2】:

      boost::pheonix::swapstd::swap 可以避免复制。

      #include <boost/spirit/include/qi.hpp>
      #include <boost/spirit/include/phoenix.hpp>
      #include <boost/fusion/include/boost_tuple.hpp>
      
      
      namespace ph = boost::phoenix;
      namespace qi = boost::spirit::qi;
      
      int main(int argc, char *argv[])
      {
        std::string input("2 3 3\n");
      
        qi::rule<std::string::iterator, qi::space_type, double()> r0
          = qi::double_;
        r0.name("r0");
        // I'm not sure why this qi::omit is needed but without it the output is empty
        qi::rule<std::string::iterator, qi::space_type, std::vector<double>(size_t)> r1
          = qi::omit[qi::eps[ph::reserve(qi::_val, qi::_r1)]] >> qi::repeat(qi::_r1)[r0];
        r1.name("r1");
        qi::rule<std::string::iterator, qi::space_type, boost::tuple<size_t, std::vector<double> >()> r2
          = qi::int_ >> r1(ph::at_c<0>(qi::_val));
        r2.name("r2");
        qi::rule<std::string::iterator, qi::space_type, std::vector<double>()> r3
          = r2[ph::swap(qi::_val, ph::at_c<1>(qi::_1))];
        r3.name("r3");
        std::vector<double> res;
        bool success = qi::phrase_parse(input.begin(),
                        input.end(),
                        r3,
                        qi::space,
                        res);
        if (success) {
          for(std::vector<double>::iterator it = res.begin(); it != res.end(); it++) {
            std::cout << *it << " " << std::endl;
          }
        }
        return !success;
      }
      

      (抱歉,代码不清楚,但我将其视为boost::spirit 的游乐场)

      【讨论】:

      • @llonesmiz:如果您将其发布为我会接受的答案 - 这个问题是关于当地人的。
      猜你喜欢
      • 2019-06-04
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      相关资源
      最近更新 更多