第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

促進精神船長問題

促進精神船長問題

C++
qq_遁去的一_1 2019-06-15 13:57:46
促進精神船長問題我對精神跳躍者有困難。我需要解析這樣的文件: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 ]
    它像往常一樣解析,包括跳過,但返回匹配的源序列的原始迭代器范圍(包括跳過的位置)。

  • qi::no_skip [ p ]

    抑制跳過而不預跳
  • qi::skip(s) [ p ]

    ,它將船長替換為另一個船長。

    s

    (請注意,您需要使用適當聲明的

    qi::rule<>

    實例中的實例。

    skip[]

    條款)

哪里p是任何解析器表達式。

特定溶液

正如你已經(jīng)知道的,你的問題可能是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";}

更新

作為對評論的回應:這里有一個片段,展示了如何從文件中讀取輸入。這是經(jīng)過測試的,對我來說很好:

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;


查看完整回答
反對 回復 2019-06-15
  • 1 回答
  • 0 關注
  • 290 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號