我想使用boost::spiritstd::string解析结构。在我的示例中,字符串看起来像从文件中读取的内容

您知道为什么根本不解析吗?我复制了经典的employee示例,但未解析该字符串。

美国空军

std::string temp_1 =
"MyStruct   {               \n"
"   a ""A"",            \n"
"   b ""B"",            \n"
"   c ""C"",            \n"
"   d ""D""             \n"
"  }                    \n"
;




namespace client{
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;
    struct MyStruct{
        std::string a;
        std::string b;
        std::string c;
        std::string d;
    };
};



BOOST_FUSION_ADAPT_STRUCT(
    client::MyStruct,
    (std::string, a)
    (std::string, b)
    (std::string, c)
    (std::string, d)
)

namespace client
{
    template <typename Iterator>
    struct MyStruct_parser : qi::grammar<Iterator, MyStruct(), ascii::space_type>
    {
        MyStruct_parser() : MyStruct_parser::base_type(start)
        {
                using qi::lexeme;
                using ascii::char_;
            using qi::lit;

                quoted_string %= lexeme['"' >> +(char_ - '"') >> '"'];

                start %=
                lit("MyStruct")
                >> '{'
                >> "a" >> quoted_string >> ','
                >> "b" >> quoted_string >> ','
                >> "c" >> quoted_string >> ','
                >> "d" >> quoted_string
                >>  '}'
                ;
        }

        qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
        qi::rule<Iterator, MyStruct(),  ascii::space_type> start;
    };
}


int main( int argc, char** argv ){



    using boost::spirit::ascii::space;
    typedef std::string::const_iterator iterator_type;
    typedef client::MyStruct_parser<iterator_type> MyStruct_parser;

    std::stringstream inp(temp_1);

    std::string line;
    while( std::getline( line, inp ) ){

        std::string::const_iterator a = line.begin();
            std::string::const_iterator b = line.end();

        MyStruct_parser g; // Our grammar
        client::MyStruct obj;
        bool r = phrase_parse( a, b, g, space, obj);
        if( r ){
            std::cout << "parsed" << std::endl;
        }else{
            std::cout << "not parsed" << std::endl;
        }
    }
    return 1;
}

最佳答案

似乎您的temp_1中的字符串未加引号,但语法要求加引号的字符串。要给字符串文字加上引号,请使用转义\“

07-27 13:32