Search code examples
c++boostboost-spirit-qi

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 %=?


Solution

  • Ok, I've found it in boost documentation under Auto Rules:

     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 enable automatic attribute propagation, which would otherwise be inhibited by the presence of the semantic action.