了解Boost.Spirit中的列表运算符(%)

时间:2015-11-20 00:01:40

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

您能否帮我理解Boost.Spirit中a % b解析器与其展开的a >> *(b >> a)表单之间的区别?即使the reference manual声明它们是等价的,

  

列表运算符a % b是一个二元运算符,它匹配由a出现的b的一个或多个重复列表。这相当于a >> *(b >> a)

以下程序根据使用的结果产生不同的结果:

#include <iostream>
#include <string>
#include <vector>

#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>

struct Record {
  int id;
  std::vector<int> values;
};

BOOST_FUSION_ADAPT_STRUCT(Record,
  (int, id)
  (std::vector<int>, values)
)

int main() {
  namespace qi = boost::spirit::qi;

  const auto str = std::string{"1: 2, 3, 4"};

  const auto rule1 = qi::int_ >> ':' >> (qi::int_ % ',')                 >> qi::eoi;
  const auto rule2 = qi::int_ >> ':' >> (qi::int_ >> *(',' >> qi::int_)) >> qi::eoi;

  Record record1;
  if (qi::phrase_parse(str.begin(), str.end(), rule1, qi::space, record1)) {
    std::cout << record1.id << ": ";
    for (const auto& value : record1.values) { std::cout << value << ", "; }
    std::cout << '\n';
  } else {
    std::cerr << "syntax error\n";
  }

  Record record2;
  if (qi::phrase_parse(str.begin(), str.end(), rule2, qi::space, record2)) {
    std::cout << record2.id << ": ";
    for (const auto& value : record2.values) { std::cout << value << ", "; }
    std::cout << '\n';
  } else {
    std::cerr << "syntax error\n";
  }
}

Live on Coliru

1: 2, 3, 4, 
1: 2, 

rule1rule2的不同之处仅在于rule1使用了列表运算符((qi::int_ % ','))而rule2使用了其展开形式((qi::int_ >> *(',' >> qi::int_)) })。但是,rule1生成1: 2, 3, 4,(按预期方式)和rule2生成1: 2,。我无法理解rule2的结果:1)为什么它与rule1的结果不同?2)为什么34未包含在record2.values中即使phrase_parse以某种方式返回了真的吗?

2 个答案:

答案 0 :(得分:10)

  

更新 X3版本added

首先,你陷入了陷阱:

Qi规则不适用于auto。使用qi::copy或仅使用qi::rule<>。你的程序有不确定的行为,实际上它已经崩溃了(valgrind指出悬挂引用的起源)。

所以,先关闭:

const auto rule = qi::copy(qi::int_ >> ':' >> (qi::int_ % ',')                 >> qi::eoi); 

现在,当您删除程序中的冗余时,您会得到:

重现问题

<强> Live On Coliru

int main() {
    test(qi::copy(qi::int_ >> ':' >> (qi::int_ % ',')));
    test(qi::copy(qi::int_ >> ':' >> (qi::int_ >> *(',' >> qi::int_))));
}

打印

1: 2, 3, 4, 
1: 2, 

原因和解决方法

成功解析后的3, 4发生了什么?

嗯,属性传播规则表明qi::int_ >> *(',' >> qi::int_)公开tuple<int, vector<int> >。为了神奇地DoTheRightThing(TM)精神意外地失误并将int“分配”到属性引用中,忽略剩余的vector<int>

如果要将容器属性解析为“原子组”,请使用qi::as<>

test(qi::copy(qi::int_ >> ':' >> qi::as<Record::values_t>() [ qi::int_ >> *(',' >> qi::int_)]));

这里as<>充当属性兼容性启发式的障碍,语法知道你的意思:

<强> Live On Coliru

#include <iostream>
#include <string>
#include <vector>

#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>

struct Record {
  int id;
  using values_t = std::vector<int>;
  values_t values;
};

BOOST_FUSION_ADAPT_STRUCT(Record, id, values)

namespace qi = boost::spirit::qi;

template <typename T>
void test(T const& rule) {
    const std::string str = "1: 2, 3, 4";

    Record record;

    if (qi::phrase_parse(str.begin(), str.end(), rule >> qi::eoi, qi::space, record)) {
        std::cout << record.id << ": ";
        for (const auto& value : record.values) { std::cout << value << ", "; }
        std::cout << '\n';
    } else {
        std::cerr << "syntax error\n";
    }
}

int main() {
    test(qi::copy(qi::int_ >> ':' >> (qi::int_ % ',')));
    test(qi::copy(qi::int_ >> ':' >> (qi::int_ >> *(',' >> qi::int_))));
    test(qi::copy(qi::int_ >> ':' >> qi::as<Record::values_t>() [ qi::int_ >> *(',' >> qi::int_)]));
}

打印

1: 2, 3, 4, 
1: 2, 
1: 2, 3, 4, 

答案 1 :(得分:10)

因为是时候让人们开始使用X3(Spirit的新版本),并且因为我喜欢挑战msyelf在Spirit X3中执行相应的任务,所以这里是Spirit X3版本。

X3中的auto没有问题。

“破坏”的情况也表现得更好,触发了这个静态断言:

    // If you got an error here, then you are trying to pass
    // a fusion sequence with the wrong number of elements
    // as that expected by the (sequence) parser.
    static_assert(
        fusion::result_of::size<Attribute>::value == (l_size + r_size)
      , "Attribute does not have the expected size."
    );

那是很好,对吧?

解决方法似乎不太可读:

test(int_ >> ':' >> (rule<struct _, Record::values_t>{} = (int_ >> *(',' >> int_))));

但如果你想要编写自己的as<>“指令”(或只是一个函数),那将是微不足道的:

namespace {
    template <typename T>
    struct as_type {
        template <typename Expr>
            auto operator[](Expr&& expr) const {
                return x3::rule<struct _, T>{"as"} = x3::as_parser(std::forward<Expr>(expr));
            }
    };

    template <typename T> static const as_type<T> as = {};
}

样本

<强> Live On Coliru

#include <iostream>
#include <string>
#include <vector>

#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/spirit/home/x3.hpp>

struct Record {
    int id;
    using values_t = std::vector<int>;
    values_t values;
};

namespace x3 = boost::spirit::x3;

template <typename T>
void test(T const& rule) {
    const std::string str = "1: 2, 3, 4";

    Record record;

    auto attr = std::tie(record.id, record.values);

    if (x3::phrase_parse(str.begin(), str.end(), rule >> x3::eoi, x3::space, attr)) {
        std::cout << record.id << ": ";
        for (const auto& value : record.values) { std::cout << value << ", "; }
        std::cout << '\n';
    } else {
        std::cerr << "syntax error\n";
    }
}

namespace {
    template <typename T>
    struct as_type {
        template <typename Expr>
            auto operator[](Expr&& expr) const {
                return x3::rule<struct _, T>{"as"} = x3::as_parser(std::forward<Expr>(expr));
            }
    };

    template <typename T> static const as_type<T> as = {};
}

int main() {
    using namespace x3;
    test(int_ >> ':' >> (int_ % ','));
    //test(int_ >> ':' >> (int_ >> *(',' >> int_))); // COMPILER asserts "Attribute does not have the expected size."

    // "clumsy" x3 style workaround
    test(int_ >> ':' >> (rule<struct _, Record::values_t>{} = (int_ >> *(',' >> int_))));

    // using an ad-hoc `as<>` implementation:
    test(int_ >> ':' >> as<Record::values_t>[int_ >> *(',' >> int_)]);
}

打印

1: 2, 3, 4, 
1: 2, 3, 4, 
1: 2, 3, 4,