Boost spirit semantic actions on qi::rule

旧巷老猫 提交于 2019-12-23 17:52:19

问题


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:

  1. the lit is implied
  2. you probably do not want || parser operator
  3. this assumes that the is_constant field is adapted in the fusion sequence


来源:https://stackoverflow.com/questions/43958088/boost-spirit-semantic-actions-on-qirule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!