0

I'm writing a simple calculator using boost spirit.

I want the division operator to throw an exception if a zero denominator is encountered.

I'm thinking along the lines of

 term =
     factor                      [qi::_val = qi::_1]
     >> *(('*' >> factor         [qi::_val *= qi::_1])
     |
         ('/' >> factor  
        [qi::_val = boost::phoenix::if_else(
            qi::_1, 
            qi::_val / qi::_1, 
            /*ToDo some exception actor here*/)
        ])...

However, for this to make sense, the exception actor not only needs to lazy-throw "division by zero" but it also has to have an implicit return type compatible with qi::_val. That's where I'm stuck. Is there something in phoenix that I can use here or do I need to bind to a hand-coded function?

P45 Imminent
  • 8,319
  • 4
  • 35
  • 78

1 Answers1

3

Boost Phoenix allows you to group statements. Parentheses are used to do that. This, along with boost::phoenix::throw_ allows you to write

(boost::phoenix::throw_("division by zero"), qi::_1)

in your ToDo block. qi::_1 will not be evaluated but (i) you know it would evaluate to 0 as it will have "failed" the if_else and (ii) it has the correct type.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483