c++ - 提升精神 : parse a section of an input

标签 c++ parsing boost-spirit

我有数千行输入,每行由 3 个整数和一个逗号组成,看起来像这样:

5 6 10,
8 9 45,
.....

如何创建仅解析输入的特定部分的语法,例如前 100 行或从 1000 到 1200 行并忽略其余部分。

我的语法目前是这样的:

qi::int_ >> qi::int_ >> qi::int_ >> qi::lit(",");

但显然它会解析整个输入。

最佳答案

您可以只查找感兴趣的点并在那里解析 100 行。

关于如何从 spirit 跳过 100 行的草图:

Live On Coliru

#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted/std_tuple.hpp>
#include <tuple>

namespace qi = boost::spirit::qi;

int main() {
    using It  = boost::spirit::istream_iterator;
    using Tup = std::tuple<int, int, int>;

    It f(std::cin >> std::noskipws), l;
    std::vector<Tup> data;

    using namespace qi;

    if (phrase_parse(f, l,
            omit [ repeat(100) [ *(char_ - eol) >> eol ] ] >> // omit 100 lines
            repeat(10) [  int_ >> int_ >> int_ >> ',' >> eol ], // parse 10 3-tuples
            blank, data))
    {
        int line = 100;
        for(auto tup : data)
            std::cout << ++line << "\t" << boost::fusion::as_vector(tup) << "\n";
    }

}

当使用一些随机输入进行测试时

od -Anone -t d2 /dev/urandom -w6 | sed 's/$/,/g' | head -200 | tee log | ./test
echo ============== VERIFY WITH sed:
nl log | sed -n '101,110p'

它会打印一些预期的东西,比如:

101 (15400 5215 -20219)
102 (26426 -17361 -6618)
103 (-15311 -6387 -5902)
104 (22737 14339 16074)
105 (-28136 21003 -11594)
106 (-11020 -32377 -4866)
107 (-24024 10995 22766)
108 (3438 -19758 -10931)
109 (28839 22032 -7204)
110 (-25237 23224 26189)
============== VERIFY WITH sed:
   101    15400   5215 -20219,
   102    26426 -17361  -6618,
   103   -15311  -6387  -5902,
   104    22737  14339  16074,
   105   -28136  21003 -11594,
   106   -11020 -32377  -4866,
   107   -24024  10995  22766,
   108     3438 -19758 -10931,
   109    28839  22032  -7204,
   110   -25237  23224  26189,

关于c++ - 提升精神 : parse a section of an input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33070299/

相关文章:

c++ - 如何打破 for 循环并转换整数?

c++ - C++ 中的 'and' 和 '&' 有什么区别?

parsing - markItUp 编辑器的 Markdown 解析器或稳定的 WMD-showdown 版本

c++ - boost::spirit 解析为 fusion 适应结构可选但独占

c++ - 用字符c++初始化二维数组

java - Android XML 解析错误 - "Couldn' t 打开 .../directory/container.xml"

PHP EDI X12 解析

c++ - Boost Spirit X3 无法编译具有可变因子的重复指令

c++ - 需要一种方法为 boost::spirit::qi 解析器加上另一个前缀

C++ 14制作自定义迭代器,它将经过2并返回修改后的数据