如何使用qi解析和验证有序的整数列表

dpj*_*dpj 5 c++ boost boost-spirit boost-spirit-qi

我正在解析一个文本文件,可能有几GB大小,由以下行组成:

11 0.1
14 0.78
532 -3.5
Run Code Online (Sandbox Code Playgroud)

基本上,每行一个int和一个float.整数应该是有序的而且是非负的.我想验证数据是否如描述,并返回给我范围内的最小和最大int.这就是我想出来的:

#include <iostream>
#include <string>

#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>

namespace px = boost::phoenix;
namespace qi = boost::spirit::qi;

namespace my_parsers
{
using namespace qi;
using px::at_c;
using px::val;
template <typename Iterator>
struct verify_data : grammar<Iterator, locals<int>, std::pair<int, int>()>
{
    verify_data() : verify_data::base_type(section)
    {
        section
            =  line(val(0))    [ at_c<0>(_val) = _1]
            >> +line(_a)       [ _a = _1]
            >> eps             [ at_c<1>(_val) = _a]
            ;

        line
            %= (int_ >> other) [
                                   if_(_r1 >= _1)
                                   [
                                       std::cout << _r1 << " and "
                                       << _1 << val(" out of order\n")
                                   ]
                               ]
            ;

        other
            = omit[(lit(' ') | '\t') >> float_ >> eol];
    }
    rule<Iterator, locals<int>, std::pair<int, int>() > section;
    rule<Iterator, int(int)> line;
    rule<Iterator> other;
};
}

using namespace std;
int main(int argc, char** argv)
{
    string input("11 0.1\n"
                 "14 0.78\n"
                 "532 -3.6\n");

    my_parsers::verify_data<string::iterator> verifier;
    pair<int, int> p;
    std::string::iterator begin(input.begin()), end(input.end());
    cout << "parse result: " << boolalpha
         << qi::parse(begin, end, verifier, p) << endl; 
    cout << "p.first: " << p.first << "\np.second: " << p.second << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想知道的是以下内容:

  • 有没有更好的方法来解决这个问题?我使用了继承和合成的属性,局部变量和一些凤凰伏都教.这很棒; 学习工具很好,但我不禁想到可能有一种更简单的方法来实现同样的事情:/(在PEG解析器中......)
  • 例如,如果没有局部变量,怎么办呢?

更多信息:我有其他数据格式同时被解析,所以我想保留返回值作为解析器属性.目前这是一个std :: pair,其他数据格式在解析时会暴露自己的std ::对,这就是我想要在std :: vector中填充的东西.

seh*_*ehe 2

这至少已经短了很多:

  • 减少至 28 LOC
  • 没有更多的当地人
  • 不再有融合矢量at<>魔法
  • 不再有继承属性
  • 不再有语法课
  • 不再需要手动迭代
  • 使用期望点(参见other)来增强解析错误报告
  • vector<int>如果您选择将其分配给该解析器表达式,则该解析器表达式会整齐地合成为 a %=(但除了可能分配较大的数组之外,还会降低性能)

#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>

namespace px = boost::phoenix;
namespace qi = boost::spirit::qi;

typedef std::string::iterator It;

int main(int argc, char** argv)
{
    std::string input("11 0.1\n"
            "14 0.78\n"
            "532 -3.6\n");

    int min=-1, max=0;
    {
        using namespace qi;
        using px::val;
        using px::ref;

        It begin(input.begin()), end(input.end());
        rule<It> index = int_ 
            [
                if_(ref(max) < _1)  [ ref(max) = _1 ] .else_ [ std::cout << _1 << val(" out of order\n") ],
                if_(ref(min) <  0)  [ ref(min) = _1 ]
            ] ;

        rule<It> other = char_(" \t") > float_ > eol;

        std::cout << "parse result: " << std::boolalpha 
                  << qi::parse(begin, end, index % other) << std::endl; 
    }
    std::cout << "min: " << min << "\nmax: " << max << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

奖金

我可能建议从表达式中去掉验证,并使其成为一个独立的函数;当然,这使得事情变得更加冗长(并且......清晰),并且我的脑残样本使用全局变量...... -但我相信你知道如何使用boost::bindpx::bind使其更加真实

除了上述之外

  • 即使使用免费功能,也可减少至 27 LOC
  • 不再有 phoenix,不再包含 phoenix(是的,编译时间)
  • 调试版本中不再有 Phoenix 表达式类型会导致二进制文件膨胀并减慢速度
  • 不再有varrefif_.else_和那些可怜的东西(由于 phoenix.hpp 中未包含重载,因此(在某些时候operator,)存在重大错误风险)
  • 轻松移植到 c++0x lambda - 立即消除对全局变量的需要

#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>
namespace px = boost::phoenix;
namespace qi = boost::spirit::qi;
typedef std::string::iterator It;

int min=-1, max=0, linenumber=0;
void validate_index(int index)
{
    linenumber++;
    if (min < 0)     min = index;
    if (max < index) max = index;
    else             std::cout << index << " out of order at line " << linenumber << std::endl;
}

int main(int argc, char** argv)
{
    std::string input("11 0.1\n"
            "14 0.78\n"
            "532 -3.6\n");
    It begin(input.begin()), end(input.end());

    {
        using namespace qi;

        rule<It> index = int_ [ validate_index ] ;
        rule<It> other = char_(" \t") > float_ > eol;
        std::cout << "parse result: " << std::boolalpha 
                  << qi::parse(begin, end, index % other) << std::endl; 
    }
    std::cout << "min: " << min << "\nmax: " << max << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)