促進精神船長問題我對精神跳躍者有困難。我需要解析這樣的文件:ROW intint [int, int]int [int, int]...只有在第一個int之后添加一個‘_’,我才能沒有問題地解析它(這要感謝堆棧溢出;)。事實上,我認為船長在第一個int之后吃行的末尾,所以第一個和第二個(在第二行)看起來只有一個int。我不知道如何保持生活質量,但要多吃點地方。我找到了使用自定義解析器的示例,如這里和這里.我試過氣:空白,自定義解析器與一個單一的規(guī)則點燃(‘)無論我使用什么船長,空間和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是我的問題,解決我的問題的一種方法是不使用SCOPER,但是短語_PARASE需要一個,然后我的解析器需要一個。我已經(jīng)被困了好幾個小時了.。我想這顯然是我誤解了。謝謝你的幫助。
1 回答

夢里花落0921
TA貢獻1772條經(jīng)驗 獲得超6個贊
qi::lexeme [ p ]
例如,如果您想要確保解析一個標識符而不需要內部跳過,則這會抑制跳過器) qi::raw [ 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;
- 1 回答
- 0 關注
- 290 瀏覽
添加回答
舉報
0/150
提交
取消