c++ - 解析所需的 boost Spirit 中的临时属性

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

我有一个格式为“$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 功能。

最佳答案

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

#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

关于c++ - 解析所需的 boost Spirit 中的临时属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15065503/

相关文章:

c++ - 如何通过 boost spirit 提取 std::string 对象

c++ - 数独输入程序跳过提示

c++ - C++ 中的任何 NIO 框架

c++ - 未在客户端 Winsock 中接收到完整数据

c++ - 如何将 linc 传递给类函数并调用它?

c++ - boost 灵气 : Omit element in Kleene Star parser

c++ - 用元素的混合物解析化学式

c++ - C++-如何为类模板声明函数模板好友

c++ - 如何将 type 和 mpl::vector 连接到一个新的 vector 中

c++ - 设置 Boost 正则表达式语言环境?