提升精神还原解析

时间:2014-05-30 11:43:55

标签: c++ boost boost-spirit boost-spirit-qi boost-phoenix

我想解析包含以下结构的文件:

some
garbage *&%
section1 {
    section_content
}
section2 {
    section_content
}

已定义解析section_name1 { ... } section_name2 { ... }的规则:

section_name_rule = lexeme[+char_("A-Za-z0-9_")];
section = section_name_rule > lit("{") > /*some complicated things*/... > lit("}");
sections %= +section;

所以我需要跳过任何垃圾,直到符合sections规则。 有没有办法实现这个目标?我试过了seek[sections],但似乎没有用。

修改: 我本地化了搜索不起作用的原因:如果我使用跟随运算符(>>),那么它可以工作。如果使用期望解析器(>),则会抛出异常。以下是示例代码:

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

namespace qi = boost::spirit::qi;
using boost::phoenix::push_back;

struct section_t {
    std::string name, contents;
    friend std::ostream& operator<<(std::ostream& os, section_t const& s) { return os << "section_t[" << s.name << "] {" << s.contents << "}"; }
};

BOOST_FUSION_ADAPT_STRUCT(section_t, (std::string, name)(std::string, contents))

    typedef std::vector<section_t> sections_t;

    template <typename It, typename Skipper = qi::space_type>
    struct grammar : qi::grammar<It, sections_t(), Skipper>
{
    grammar() : grammar::base_type(start) {
        using namespace qi;
        using boost::spirit::repository::qi::seek;
        section_name_rule = lexeme[+char_("A-Za-z0-9_")];
        //Replacing '>>'s with '>'s throws an exception, while this works as expected!!
        section = section_name_rule
            >>
            lit("{") >> lexeme[*~char_('}')] >> lit("}");
        start = seek [ hold[section[push_back(qi::_val, qi::_1)]] ]
            >> *(section[push_back(qi::_val, qi::_1)]);
    }
    private:
    qi::rule<It, sections_t(),  Skipper> start;
    qi::rule<It, section_t(),   Skipper> section;
    qi::rule<It, std::string(), Skipper> section_name_rule;
};

int main() {
    typedef std::string::const_iterator iter;
    std::string storage("sdfsdf\n sd:fgdfg section1 {dummy } section2 {dummy  } section3 {dummy  }");
    iter f(storage.begin()), l(storage.end());
    sections_t sections;
    if (qi::phrase_parse(f, l, grammar<iter>(), qi::space, sections))
    {
        for(auto& s : sections)
            std::cout << "Parsed: " << s << "\n";
    }
    if (f != l)
        std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}

所以在真实的例子中,我的整个语法是用期望运算符构造的。我是否必须改变所有内容以使&#34;寻求&#34;工作,还是有其他方式(让我们说,寻求一个简单的&#34; {&#34;,并将一个section_name_rule还原回来)??

1 个答案:

答案 0 :(得分:3)

这是一场演示,使用哈姆雷特作为灵感: Live On Coliru

start = *seek [ no_skip[eol] >> hold [section] ];

注意:

  • 降低期望值
  • 通过要求在部分名称之前开始行来优化

示例输入:

some
garbage *&%
section1 {
   Claudius: ...But now, my cousin Hamlet, and my son —
   Hamlet: A little more than kin, and less than kind.
}
WE CAN DO MOAR GARBAGE
section2 {
   Claudius: How is it that the clouds still hang on you?
   Hamlet: Not so my lord; I am too much i' the sun 
}

输出:

Parsed: section_t[section1] {Claudius: ...But now, my cousin Hamlet, and my son —
   Hamlet: A little more than kin, and less than kind.
}
Parsed: section_t[section2] {Claudius: How is it that the clouds still hang on you?
   Hamlet: Not so my lord; I am too much i' the sun 
}

参考列表

// #define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/repository/include/qi_seek.hpp>

namespace qi = boost::spirit::qi;

struct section_t { 
    std::string name, contents;
    friend std::ostream& operator<<(std::ostream& os, section_t const& s) { return os << "section_t[" << s.name << "] {" << s.contents << "}"; }
};

BOOST_FUSION_ADAPT_STRUCT(section_t, (std::string, name)(std::string, contents))

typedef std::vector<section_t> sections_t;

template <typename It, typename Skipper = qi::space_type>
struct grammar : qi::grammar<It, sections_t(), Skipper>
{
    grammar() : grammar::base_type(start) {
        using namespace qi;
        using boost::spirit::repository::qi::seek;

        section_name_rule = lexeme[+char_("A-Za-z0-9_")];
        section           = section_name_rule >> '{' >> lexeme[*~char_('}')] >> '}';
        start             = *seek [ no_skip[eol] >> hold [section] ];

        BOOST_SPIRIT_DEBUG_NODES((start)(section)(section_name_rule))
    }
  private:
    qi::rule<It, sections_t(),  Skipper> start;
    qi::rule<It, section_t(),   Skipper> section;
    qi::rule<It, std::string(), Skipper> section_name_rule;
};

int main() {
    using It = boost::spirit::istream_iterator;
    It f(std::cin >> std::noskipws), l;

    sections_t sections;
    if (qi::phrase_parse(f, l, grammar<It>(), qi::space, sections))
    {
        for(auto& s : sections)
            std::cout << "Parsed: " << s << "\n";
    }
    if (f != l)
        std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}