gpt4 book ai didi

c++ - boost spirit 还原解析

转载 作者:行者123 更新时间:2023-11-30 02:46:00 25 4
gpt4 key购买 nike

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

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],但它似乎不起作用。

编辑:我本地化了 seek 不起作用的原因:如果我使用 follows operator(>>>),那么它就起作用了。如果使用期望解析器 (>),则它会抛出异常。这是一个示例代码:

#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";
}

所以在真实的例子中,我的整个语法都是用期望运算符构造的。我是否必须更改所有内容才能使“搜索”工作,或者是否有任何其他方法(比方说,搜索一个简单的“{”,然后将一个 section_name_rule 还原回来)??

最佳答案

这是一个演示,以哈姆雷特为灵感: 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
}

引用 list

// #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";
}

关于c++ - boost spirit 还原解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23953804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com