3

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 < 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

ildjarn
  • 62,044
  • 9
  • 127
  • 211
sk7
  • 43
  • 5
  • see also http://stackoverflow.com/questions/7436481/how-to-make-my-split-work-only-on-one-real-line-and-be-capable-to-skeep-quoted-pa/7462539#7462539 – sehe Sep 27 '11 at 12:33

1 Answers1

7

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.

hkaiser
  • 11,403
  • 1
  • 30
  • 35
  • +1, I hadn't noticed the `~` parser operator before (I still used `char_ - char_(", ;")` instead – sehe Sep 27 '11 at 12:32