Using boost.spirit I try to parse simple command line of the form command:param1 param2...
to do so I created this parser:
(+(char_ - ':'))[ref(cmd) = _1]
>> ':'
>> (*char_)[ref(params) = _1]
The attribute types of the two compounds parser is vector, so if cmd and params are of type vector this work. However if they are of type std::string it doesn’t. While searching for this solution on the web I found hint that it should also work with string. Is there anyway I can make this work with string?
Sure, when you use semantic actions no automatic attribute propagation will happen. Both your parsers (
+(char_ - ':')and*char_) expose astd::vector<char>as their attribute. Therefore,_1refers to astd::vector<char>as well. Ifcmdandparamsare instances ofstd::stringit will not compile as no assignment from astd::vector<char>to astd::stringis defined.However, if you get rid of the semantic actions it will work:
This is not only simpler, but faster as well. The parser will place the matched characters directly into the supplied strings.