Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()>
{
public:
  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 %=?

like image 790
nkdm Avatar asked Feb 08 '16 21:02

nkdm


1 Answers

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

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

like image 128
nkdm Avatar answered Sep 27 '22 21:09

nkdm