Understanding Boost.spirit’s string parser

It’s not the string matcher per se. It’s [attribute propagation] + [backtracking] in action.

A string attribute is a container attribute and many elements could be assigned into it by different parser subexpressions. Now for efficiency reasons, Spirit doesn’t rollback the values of emitted attributes on backtracking.

Often this is no problem at all, but as you can see, the ‘a’ from the failed first branch of the alternative sticks around.

Either reword or employ the ‘big gun’ qi::hold[] directive:

(qi::hold [ string("a")  >> string("a") ] | string("a")),

Rewording could look like:

qi::string("a") >> -qi::string("a"),

Also, if you’re really just trying to match certain textual strings, consider:

(qi::raw [ qi::lit("aa") | "a" ]), 
// or even just
qi::string("aa") | qi::string("a"),

Now which one of these applies most, depends on your grammar.

Leave a Comment