c++ - 用于解析 `int_` 然后重复等于 `int_` 的 double 的 X3 规则

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

<分区>

有点难以理解的标题,但我想解析这样的内容:

int_ >> repeat(N)[double_]

但是我希望 N 等于 int_ 解析为

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

最佳答案

解析后更容易验证这一点。即只解析不限制计数,然后验证:

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];

关于c++ - 用于解析 `int_` 然后重复等于 `int_` 的 double 的 X3 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51863074/

相关文章:

c++ - 清理cpp中的密码字符串

c++ - 创建 boost::tuple<std::string, std::string, int> 和 std::vector<int> 的映射

c++ - 将点数组转换为多边形

c++ - 振奋 spirit : how to use custom logic when parsing a list of doubles with text specifiers

c++ - Boost Spirit Parser 可选表达式求值

c++ - 用精神将日期时间字符串解析为 time_t 值

c++ - 静态变量和全局变量在linux上动态库和静态库显示地址不同?

c++ - 累加 vector 的绝对值

c++ - 如何为 std::unique_ptr 创建一个有效的 C++ 别名模板

c++ - cin 和 cout 对象的生命周期是什么? C++