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. using phx::bind;
  34.  
  35. auto parser =
  36. double_ [_val = _1]
  37. >> *( (lit('+') >> double_[bind(add_operand(), _val, _1)])
  38. | (lit('-') >> double_[bind(sub_operand(), _val, _1)])
  39. );
  40.  
  41. ok = phrase_parse(begin, end, parser, ascii::space, result);
  42. }
  43.  
  44. if (ok && begin == end)
  45. std::cout << "parsed, result = " << result << std::endl;
  46. else
  47. std::cout << "not parsed" << std::endl;
  48. }
  49.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty