c – 使用boost :: spirit :: qi :: phrase_parse将值存储在std
我目前正在尝试使用boost :: spirit :: qi :: phrase_parse完成一些工作,但我不能自己解决这个问题.
值得一提的是:我是全新的提升,所以提升::精神. 我得到“{A [B C] – > F [D E],C – > E,B-> Z}形式的输入” 我想将这种类型的输入解析为std :: map< std :: string,std :: string>.密钥应该在“ – >”之前保存每个std :: string以及“ – >”之后的每个std :: string的值直到’,’发生. 此外,不应存储'[‘和’]’. 因此,解析成功后,std :: map的内容应该是这样的: { ("A","F"),("A","D E"),("B C",("C","E"),("B","Z") } 我的第一种方法是将所有键/值存储在std :: vector< std :: string>中. #include <boost/spirit/include/qi.hpp> #include <iostream> #include <string> #include <vector> int main() { using boost::spirit::qi::phrase_parse; using boost::spirit::qi::char_; using boost::spirit::qi::lexeme; std::string input = "{A [B C] -> F [D E],C ->E,B->Z}"; std::string::const_iterator beg = input.begin(),end = input.end(); std::vector<std::string> sdvec; bool r = phrase_parse( beg,end,'{' >> (+(+char_("a-zA-Z0-9") | lexeme[('[' >> +char_("a-zA-Z0-9 ") >> ']')]) >> '-' >> '>' >> +(+char_("a-zA-Z0-9") | lexeme[('[' >> +char_("a-zA-Z0-9 ") >> ']')])) % ',' >> '}',boost::spirit::ascii::space,sdvec ); if(beg != end) { std::cout << "Parsing failed!" << std::endl; } else { std::cout << "Parsing succeeded!" << std::endl; } for(int i=0; i<sdvec.size(); i++) { std::cout << i << ": " << sdvec[i] << std::endl; } return 0; } 执行这个我得到每个发现std :: string作为std :: vector的一个条目: Parsing 2 succeeded! 0: A 1: B C 2: F 3: D E 4: C 5: E 6: B 7: Z 但我不知道如何将这些值解析为std :: map< std :: string,std :: string>使用boost :: spirit :: qi :: phrase_parse作为简单替换会抛出一些编译错误. 编辑: 实际上我发现了一些我需要的东西:http://boost-spirit.com/home/articles/qi-example/parsing-a-list-of-key-value-pairs-using-spirit-qi/ 我根据我的问题采用了这篇文章的代码: #include <boost/spirit/include/qi.hpp> #include <boost/fusion/include/std_pair.hpp> #include <iostream> #include <string> #include <vector> #include <map> namespace qi = boost::spirit::qi; template <typename Iterator> struct keys_and_values : qi::grammar<Iterator,std::map<std::string,std::string>()> { keys_and_values() : keys_and_values::base_type(query) { query = '{' >> *qi::lit(' ') >> pair >> *(qi::lit(',') >> *qi::lit(' ') >> pair) >> *qi::lit(' ') >> '}'; pair = key >> -(*qi::lit(' ') >> "->" >> *qi::lit(' ') >> value); key = +qi::char_("a-zA-Z0-9") | qi::lexeme[('[' >> +qi::char_("a-zA-Z0-9 ") >> ']')]; value = +qi::char_("a-zA-Z0-9") | qi::lexeme[('[' >> +qi::char_("a-zA-Z0-9 ") >> ']')]; } qi::rule<Iterator,std::string>()> query; qi::rule<Iterator,std::pair<std::string,std::string>()> pair; qi::rule<Iterator,std::string()> key,value; }; int main() { std::string input = "{AB -> CD,E -> F,G -> HI,[J K L] -> [M N O] }"; std::string::iterator begin = input.begin(); std::string::iterator end = input.end(); keys_and_values<std::string::iterator> p; // create instance of parser std::map<std::string,std::string> m; // map to receive results bool result = qi::phrase_parse(begin,p,m); // returns true if successful if(begin != end) { std::cout << "Parsing failed!" << std::endl; } else { std::cout << "Parsing succeeded!" << std::endl; } std::cout << m["AB"] << "t" << m["E"] << "t" << m["G"] << "t" << m["J K L"] << std::endl; return 0; } 结果或多或少是我需要的: Parsing succeeded! CD F HI M N O 我要解决的最后一个问题是像A [B C] – >这样的情况. F [D E]. 任何方式将它们作为四个分开的键值对(“A”,“F”),(“A”,“DE”),(“BC”,“DE”) )进入我的std :: map< std :: string,std :: string> M& 或者也许更容易将其解析为std :: map< std :: vector< std :: string>,std :: vector< std :: string> >其中每个std :: vector< std :: string>持有所有键/值? 例如: in: "{A [B C] -> F [D E],C ->E,B->Z}" out: { ({"A","B C"},{"F","D E"}),({"C"},{"E"}),({"B"},{"Z"}) } 谢谢你的帮助! 解决方法
我认为你非常接近你的目标所以我将跳过组合部分:-)
解析器将执行它应该做的事情…检查语法并标记数据然后它将键,值和输出映射(多图)参数传递到phoenix函数插入器,您可以在地图中插入所需的任何内容(多图) #if __cplusplus >= 201103L #define BOOST_RESULT_OF_USE_DECLTYPE #endif #define BOOST_SPIRIT_USE_PHOENIX_V3 #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix.hpp> #include <iostream> #include <algorithm> #include <iterator> #include <iomanip> #include <vector> #include <map> namespace qi = boost::spirit::qi; namespace ascii=boost::spirit::ascii; typedef std::map< std::string,std::string > TMap; //typedef std::multimap< std::string,std::string > TMap; struct SMapInsert { template <typename Arg1,typename Arg2,typename Arg3> struct result { typedef void type; }; template <typename Arg1,typename Arg3> void operator()( Arg1&out,Arg2&keys,Arg3&vals ) const { std::cout << "Keys:" << std::endl; for( const auto &key : keys ) std::cout << std::left << "`" << key << "`" << std::endl; std::cout << "Vals:" << std::endl; for( const auto &val : vals ) std::cout << std::left << "`" << val << "`" << std::endl; // your map here... // out.insert } }; boost::phoenix::function< SMapInsert > inserter; int main() { std::string input = "{A [B C] -> F [D E],B->Z}"; TMap data; std::string::const_iterator iter = input.begin(); std::string::const_iterator last = input.end(); qi::rule< std::string::const_iterator,std::string() > token=+qi::alnum; qi::rule< std::string::const_iterator,ascii::space_type,std::vector< std::string >() > keyOrvalue = +( token | ( '[' >> qi::lexeme[ +qi::char_("a-zA-Z0-9 ") ] >> ']' ) ); qi::rule< std::string::const_iterator,TMap() > root = '{' >> ( ( keyOrvalue >> "->" >> keyOrvalue )[ inserter( qi::_val,qi::_1,qi::_2 ) ] ) % ',' >> '}'; std::cout << "input: `" << input << "`" << std::endl; if( qi::phrase_parse( iter,last,root,ascii::space,data ) && iter==last ) { for( const auto &keyValue : data ) std::cout << std::left << std::setw(10) << keyValue.first << std::setw(10) << keyValue.second << std::endl; } else std::cout << "parsing failed:" << std::string( iter,last ) << std::endl; return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |