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