【问题标题】:X3 rule for parsing `int_` then repeated doubles equal to that `int_` [duplicate]解析`int_`然后重复等于`int_`的X3规则[重复]
【发布时间】:2018-08-15 16:53:14
【问题描述】:

有点难用的标题,但我想解析如下内容:

int_ >> repeat(N)[double_]

但是我希望 N 等于 int_ 解析出的任何值

如何使用 Boost.Spirit.X3 做到这一点?

【问题讨论】:

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


【解决方案1】:

解析后更容易验证。也就是不限制计数只解析,事后验证:

auto const p = x3::int_ >> *x3::double_;

// ...
std::pair<int, std::vector<double>> result;

if (x3::phrase_parse(begin, end, p, x3::space, result)) {
    if (result.first != result.second.size()) {
        // invalid
    }
}

如果您真的想在解析时验证这一点,可以使用语义操作:

int size;
auto const store_size = [&size](auto& ctx) { size = _attr(ctx); };
auto const validate_size = [&size](auto& ctx) {
    if (size >= 0 && static_cast<std::size_t>(size) != _attr(ctx).size()) {
        _pass(ctx) = false;
    }
};
auto const p = x3::int_[store_size] >> (*x3::double_)[validate_size];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    相关资源
    最近更新 更多