Home Ask Login Register

Developers Planet

Your answer is one click away!

nkdm February 2016

Boost.Spirit: Difference between operators "%=" and "="

I don't understand the difference between these two operators. Let's take an example parsing inputs like "AA,BB,CC,DD" into vector of strings.

namespace qi = boost::spirit::qi;
class my_grammar : public qi::grammar<string::const_iterator, string()>
  my_grammar() : base_type(start) {
    using qi::_1;
    using qi::char_;
    start = *(char_ - qi::lit(','));
  qi::rule<string::const_iterator, string()> start;

As far as I know, the a %= b is equivalent to a = b[_val = _1]. That's clear. But on the other hand, the parser *(char_ - qi::lit(',')) has a synthesised attribute of type std::string to which the matched sequence will be assigned. The result of using start = *(char_ - qi::lit(',')) is the same. So what's the case for using operator %=?


nkdm February 2016

Ok, I've found it in boost documentation http://www.boost.org/doc/libs/1_60_0/libs/spirit/doc/html/spirit/qi.html:

 r %= p and r = p are equivalent if there are no semantic actions associated with p. 

So if the start rule contained semantic action ex.

*(char_[boost::phoenix::ref(my_string) = _1] - qi::lit(','))`

then changing the operator to %= would make a difference.

Post Status

Asked in February 2016
Viewed 3,734 times
Voted 9
Answered 1 times


Leave an answer

Quote of the day: live life