问题
I've been reading up on semantic actions and I have a rule that looks like this:
property_rule %=
identifier_rule % ','
>> lit(L":")
>> type_specification_rule
>> -(lit(L":=") >> +(alnum - ';'))
>> lit(L";");
The property_rule
is defined as
qi::rule<Iterator, property(), space_type> property_rule;
Now, I also want to support operator ≡
so what I want is to change the rule to something like
...
>> -(( lit(L":=") || lit(L"≡")[SEMANTIC_ACTION_HERE]) >> +(alnum - ';'))
...
In the semantic action, I want to change the property
that is being parsed, specifically setting its field is_constant
to true
. The property is Fusion-adapted. How do I do it?
回答1:
I would - as ever - avoid the semantic action (Boost Spirit: "Semantic actions are evil"? ).
I'd simply synthesize the value for is_constant
on both branches of the alternative:
>> -(( lit(L":=") || lit(L"≡")[SEMANTIC_ACTION_HERE]) >> +(alnum - ';'))
would become instead:
>> -(
(L":=" >> attr(false) | L"≡" >> ::attr(true)) >> +(alnum - ';')
)
Notes:
- the
lit
is implied - you probably do not want
||
parser operator - this assumes that the
is_constant
field is adapted in the fusion sequence
来源:https://stackoverflow.com/questions/43958088/boost-spirit-semantic-actions-on-qirule