Parsing imperial values using boost spirit (qi)

て烟熏妆下的殇ゞ 提交于 2019-12-08 17:51:25

As alternatives to sehe's answer(that you should accept):

Your solution would work if you just changed your rule to:

qi::rule<std::string::const_iterator, imp_constant()> 
        impconst = 
                 (
                     (qi::uint_ >> L'\'')[phx::at_c<0>(qi::_val)=qi::_1]
                     ||
                     (qi::uint_ >> L'"')[phx::at_c<1>(qi::_val)=qi::_1]
                 )
                 >> 
                     -(qi::uint_ >> L'/' >> qi::uint_)
                     [phx::at_c<2>(qi::_val)=qi::_1,phx::at_c<3>(qi::_val)=qi::_2]
        ;

How I would do this:

Changing slightly your imp_constant:

struct fraction
{
   unsigned int n_,d_;
};

struct imp_constant
{
    unsigned int feet_,inches_;
    fraction frac_;
};

BOOST_FUSION_ADAPT_STRUCT(fraction,
                          (unsigned int, n_)
                          (unsigned int, d_)
)

BOOST_FUSION_ADAPT_STRUCT(imp_constant, 
                          (unsigned int, feet_)
                          (unsigned int, inches_)
                          (fraction    , frac_)

And then the rule would be:

qi::rule<std::string::const_iterator,unsigned int()> feet = (qi::uint_ >> L'\'') | qi::attr(0);
qi::rule<std::string::const_iterator,unsigned int()> inches = (qi::uint_ >> L'"') | qi::attr(0);
qi::rule<std::string::const_iterator,fraction()> fract = (qi::uint_ >> L'/' >> qi::uint_) | (qi::attr(0)>> qi::attr(1));
qi::rule<std::string::const_iterator, imp_constant()> impconst=feet>>inches>>fract;

Example on LWS.

You're looking at backtracking.

The '1' in 5'1/2 is first parsed as the qi::uint_ in a potential inches branch, causing the attribute to be assigned.

Only then is the '/' encountered, which results in backtracking from the 'inch branch'. But the attribute has already been set. In this case, explicit defaults or qi::hold can fix it.

Here's something like the way I'd probably phrase things myself:

qi::rule<std::string::const_iterator, unsigned(char)> 
    component = qi::hold [ qi::uint_ >> qi::lit(qi::_r1) ];

qi::rule<std::string::const_iterator, imp_constant()> 
    impconst = 
        component(L'\'') ||
        component(L'"')  ||
        component(L'/')  ||
        qi::uint_
;

BOOST_SPIRIT_DEBUG_NODE(component);
BOOST_SPIRIT_DEBUG_NODE(impconst);

I think this could be a good starting point?

Further notes:

  1. test f!=l to detect unparsed remaining input
  2. the semantic actions in the full code sample were redundant
  3. indeed, lexeme was redundant there

See it live here: http://liveworkspace.org/code/Rpydy$0.

Test output:

----------------------
> parsing: 5'1/2
<impconst>
  <try>5'1/2</try>
  <component>
    <try>5'1/2</try>
    <success>1/2</success>
    <attributes>[5, ']</attributes>
  </component>
  <component>
    <try>1/2</try>
    <fail/>
  </component>
  <component>
    <try>1/2</try>
    <success>2</success>
    <attributes>[1, /]</attributes>
  </component>
  <success></success>
  <attributes>[[5, 0, 1, 2]]</attributes>
</impconst>

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