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