7

我在提升精神船长方面遇到了麻烦。

我需要解析这样的文件:

ROW int
int [int, int]
int [int, int]
...

只有当我在第一个 int 之后添加一个“_”时,我才能毫无问题地解析它(感谢 stackoverflow;)。

事实上,我认为船长在第一个 int 之后吃掉了行尾,所以第一个和第二个(在第二行)看起来只有一个 int。我不明白如何保持 eol 但吃空间。我找到了使用自定义解析器的示例,例如这里这里

我尝试了 qi::blank,使用一条规则的自定义解析器 lit(' ') 无论我使用什么船长,空间和 eol 总是被吃掉。

我的语法是:

一行:

struct rowType
{
    unsigned int number;
    std::list<unsigned int> list;
};

存储在结构中的完整问题:

struct problemType
{
    unsigned int ROW;
    std::vector<rowType> rows;
};

行解析器:

template<typename Iterator>
struct row_parser : qi::grammar<Iterator, rowType(), qi::space_type>
{
    row_parser() : row_parser::base_type(start)
    {

        list  = '[' >> -(qi::int_ % ',') >> ']';
        start = qi::int_ >> list;
    }

    qi::rule<Iterator, rowType(), qi::space_type> start;
    qi::rule<Iterator, std::list<unsigned int>(), qi::space_type> list;
};

和问题解析器:

template<typename Iterator>
struct problem_parser : qi::grammar<Iterator,problemType(),qi::space_type>
{

    problem_parser() : problem_parser::base_type(start)
    {
        using boost::phoenix::bind;
        using qi::lit;

        start = qi::int_ >> lit('_') >> +(row);

        //BOOST_SPIRIT_DEBUG_NODE(start);
    }

    qi::rule<Iterator, problemType(),qi::space_type> start;
    row_parser<Iterator> row;
};

我像这样使用它:

main() {
static const problem_parser<spirit::multi_pass<base_iterator_type> > p;
...
spirit::qi::phrase_parse(first, last ,
            p,
            qi::space,
            pb);
}

当然,qi::space 是我的问题,解决我的问题的一种方法是不使用船长,但是phrase_parse 需要一个,然后我的解析器需要一个。

我已经被困了几个小时了......我认为这很明显我误解了。

谢谢你的帮助。

4

1 回答 1

11

一般来说,以下指令有助于在语法中禁止/切换跳过者:

  • qi::lexeme [ p ]
    这会禁止跳过,例如,如果您想确保解析没有内部跳过的标识符) - 另请参见no_skip比较

  • qi::raw [ p ]
    它像往常一样解析,包括跳过,但返回匹配源序列的原始迭代器范围(包括跳过的位置)

  • qi::no_skip [ p ]
    在没有预跳过的情况下抑制跳过(我创建了一个最小的示例来展示这里的区别:Boost Spirit lexeme vs no_skip

  • qi::skip(s) [ p ]
    它将船长完全替换为另一个船长s(请注意,您需要在此类子句中使用适当声明qi::rule<>的实例)skip[]

p任何解析器表达式在哪里。

具体解决方案

正如您已经知道的那样,您的问题可能是qi::space吃掉所有空白。我不可能知道您的语法有什么问题(因为您没有显示完整的语法或相关输入)。

因此,这就是我要写的内容。笔记

  • 用于qi::eol明确要求在特定位置换
  • 用作qi::blank船长(不包括eol
  • 为简洁起见,我结合了语法

代码:

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

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

struct rowType {
    unsigned int number;
    std::list<unsigned int> list;
};

struct problemType {
    unsigned int ROW;
    std::vector<rowType> rows;
};

BOOST_FUSION_ADAPT_STRUCT(rowType, (unsigned int, number)(std::list<unsigned int>, list))
BOOST_FUSION_ADAPT_STRUCT(problemType, (unsigned int, ROW)(std::vector<rowType>, rows))

template<typename Iterator>
struct problem_parser : qi::grammar<Iterator,problemType(),qi::blank_type>
{
    problem_parser() : problem_parser::base_type(problem)
    {
        using namespace qi;
        list    = '[' >> -(int_ % ',') >> ']';
        row     = int_ >> list >> eol;
        problem = "ROW" >> int_ >> eol >> +row;

        BOOST_SPIRIT_DEBUG_NODES((problem)(row)(list));
    }

    qi::rule<Iterator, problemType()            , qi::blank_type> problem;
    qi::rule<Iterator, rowType()                , qi::blank_type> row;
    qi::rule<Iterator, std::list<unsigned int>(), qi::blank_type> list;
};

int main()
{
    const std::string input = 
        "ROW 1\n"
        "2 [3, 4]\n"
        "5 [6, 7]\n";

    auto f = begin(input), l = end(input);

    problem_parser<std::string::const_iterator> p;
    problemType data;

    bool ok = qi::phrase_parse(f, l, p, qi::blank, data);

    if (ok) std::cout << "success\n";
    else    std::cout << "failed\n";

    if (f!=l)
        std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}

如果您真的不想要求换行符:

template<typename Iterator>
struct problem_parser : qi::grammar<Iterator,problemType(),qi::space_type>
{
    problem_parser() : problem_parser::base_type(problem)
    {
        using namespace qi;
        list    = '[' >> -(int_ % ',') >> ']';
        row     = int_ >> list;
        problem = "ROW" >> int_ >> +row;

        BOOST_SPIRIT_DEBUG_NODES((problem)(row)(list));
    }

    qi::rule<Iterator, problemType()            , qi::space_type> problem;
    qi::rule<Iterator, rowType()                , qi::space_type> row;
    qi::rule<Iterator, std::list<unsigned int>(), qi::space_type> list;
};

int main()
{
    const std::string input = 
        "ROW 1 " // NOTE whitespace, obviously required!
        "2 [3, 4]"
        "5 [6, 7]";

    auto f = begin(input), l = end(input);

    problem_parser<std::string::const_iterator> p;
    problemType data;

    bool ok = qi::phrase_parse(f, l, p, qi::space, data);

    if (ok) std::cout << "success\n";
    else    std::cout << "failed\n";

    if (f!=l)
        std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}

更新

回应评论:这是一个片段,显示如何从文件中读取输入。这已经过测试并且对我来说很好:

std::ifstream ifs("input.txt"/*, std::ios::binary*/);
ifs.unsetf(std::ios::skipws);

boost::spirit::istream_iterator f(ifs), l;

problem_parser<boost::spirit::istream_iterator> p;
于 2013-06-12T19:44:36.607 回答