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. struct add_operand {
  7. template<typename...> struct result { typedef void type; };
  8. template<typename L, typename R>
  9. void operator()(L& lhs, R rhs) const { lhs += rhs; }
  10. };
  11.  
  12. struct sub_operand {
  13. template<typename...> struct result { typedef void type; };
  14. template<typename L, typename R>
  15. void operator()(L& lhs, R rhs) const { lhs -= rhs; }
  16. };
  17.  
  18. int main()
  19. {
  20. const std::string INPUT_DATA = "12e-1 + 3.4 - .67";
  21. typedef std::string::const_iterator iterator_type;
  22. iterator_type begin = std::begin(INPUT_DATA);
  23. iterator_type end = std::end(INPUT_DATA);
  24.  
  25. namespace qi = boost::spirit::qi;
  26. namespace ascii = boost::spirit::qi::ascii;
  27. namespace phx = boost::phoenix;
  28.  
  29. bool ok;
  30. double result;
  31. {
  32. using namespace qi;
  33. phx::function<add_operand> add_;
  34. phx::function<sub_operand> sub_;
  35.  
  36. auto parser =
  37. double_ [_val = _1]
  38. >> *( (lit('+') >> double_[add_(_val, _1)])
  39. | (lit('-') >> double_[sub_(_val, _1)])
  40. );
  41.  
  42. ok = phrase_parse(begin, end, parser, ascii::space, result);
  43. }
  44.  
  45. if (ok && begin == end)
  46. std::cout << "parsed, result = " << result << std::endl;
  47. else
  48. std::cout << "not parsed" << std::endl;
  49. }
  50.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty