fork download
  1. #define BOOST_SPIRIT_USE_PHOENIX_V3
  2. #include <boost/spirit/include/qi.hpp>
  3. #include <boost/spirit/include/phoenix.hpp>
  4. #include <boost/phoenix/function/adapt_function.hpp>
  5.  
  6. void add_operand(double& lhs, double rhs) { lhs += rhs; }
  7. void sub_operand(double& lhs, double rhs) { lhs -= rhs; }
  8.  
  9. BOOST_PHOENIX_ADAPT_FUNCTION(void, add_, add_operand, 2)
  10. BOOST_PHOENIX_ADAPT_FUNCTION(void, sub_, sub_operand, 2)
  11.  
  12. int main()
  13. {
  14. const std::string INPUT_DATA = "12e-1 + 3.4 - .67";
  15. typedef std::string::const_iterator iterator_type;
  16. iterator_type begin = std::begin(INPUT_DATA);
  17. iterator_type end = std::end(INPUT_DATA);
  18.  
  19. namespace qi = boost::spirit::qi;
  20. namespace ascii = boost::spirit::qi::ascii;
  21. namespace phx = boost::phoenix;
  22.  
  23. bool ok;
  24. double result;
  25. {
  26. using namespace qi;
  27.  
  28. auto parser =
  29. double_ [_val = _1]
  30. >> *( (lit('+') >> double_[add_(_val, _1)])
  31. | (lit('-') >> double_[sub_(_val, _1)])
  32. );
  33.  
  34. ok = phrase_parse(begin, end, parser, ascii::space, result);
  35. }
  36.  
  37. if (ok && begin == end)
  38. std::cout << "parsed, result = " << result << std::endl;
  39. else
  40. std::cout << "not parsed" << std::endl;
  41. }
  42.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty