2
votes

I have a question on semantic actions on boost spirit with the ">" operator

I got this rule which works perfectly ..

             ifelse = (iter_pos >> 
                         nocaselit(L"if") >> expression >> nocaselit(L"then") >> 
                           block_statements_eol >> -ifelse_ifelse >> nocaselit(L"end") >> nocaselit(L"if") >> 
                      iter_pos)   
               [_val = construct<common_node>(type_cmd_ifelse,LOCATION(_1,_5), key_cond, _2, key_seq, _3, key_else, phoenix::bind(&makeOptNode, _4))];

To add some error handling I also add the on_error stuff to my parser. As far I understood I also have to add “expectation points” for boost to output the error correct

So I change the grammar to this one (replace the >> with >) to give information about stop backtracking and report errors.

             ifelse = (iter_pos >> 
                         nocaselit(L"if") > expression > nocaselit(L"then") >> 
                           block_statements_eol > -ifelse_ifelse > nocaselit(L"end") > nocaselit(L"if") >> 
                      iter_pos)   
               [_val = construct<common_node>(type_cmd_ifelse,LOCATION(_1,_5), key_cond, _2, key_seq, _3, key_else, phoenix::bind(&makeOptNode, _4))];

At this point I get the C++ compiler error invalid index for _3 and _4 .. so it seems that the semantic action has to be changed in some way but I have no idea how.

1

1 Answers

1
votes

Operator precedence changes the synthesized attribute structure.

For example, both int_ >> int_ >> int_ and int_ > int_ > int_ synthesize a tuple<int, int, int> but if you mix the operators of differing precedence, you get e.g. tuple<int, tuple<int, int> > or tuple<tuple<int, int>, int>/

Now, for many automatic attribute propagation scenarios, this wouldn't hurt. But for semantic actions it changes the syntax to dissect the attribute (see fusion::at_c<> and phoenix::at_c<>).

There is a chance that the under-documented

#define BOOST_SPIRIT_ACTIONS_ALLOW_ATTR_COMPAT

might help out in specific circumstance, but otherwise, simply deal with it.

In general, avoiding semantic actions is a good guideline (Boost Spirit: "Semantic actions are evil"?).