I am using Boost 1.44, The Spirit parser works well for numeric parsing but really is tricky for string parsing. I am trying to parse a string to be split using multiple delimiters: ',' ,';' or ' '. It works well for numbers when I do this (where vect = vector &l开发者_开发知识库t; double >):
qi::parse(first,last,double_ >> *(',' >> double_ | ' ' >> double_ | ';' >> double_),
vect,space);
However when I modify the grammer for strings using vect = vector< string >,
+char_ >> *(',' >> +char_ | ' ' >> +char_ | ';' >> +char_)
I get the following error:
/usr/include/boost/spirit/home/qi/detail/assign_to.hpp:109:13: error: invalid conversion from ‘char’ to ‘const char*’/usr/include/boost/spirit/home/qi/detail/assign_to.hpp:109:13: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits,_Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]’
I narrowed down the error to being the first +char_ in the grammar syntax which is being taken as a series of chars rather than a string. Is there any way to fix this issue?
Thanks
String handling (the way you do it) got a lot easier with more recent versions of Spirit. I'd suggest to use Spirit from Boost V1.47, which got a major rewrite of the attribute handling code.
But even if it compiled the way you want, it wouldn't parse the way you expect. Spirit is inherently greedy, that means that +char_
will consume whatever is left in your input unconditionally. It seems to be better to have
+~char_(", ;") % char_(", ;")
i.e. one or more (+
) characters which are not (~
) in the set ", ;"
interpersed with exactly one of those characters. The list parser (%
) exposes a vector<A>
, where A
is the attribute of the left hand expression, a vector<string>
in the case above.
精彩评论