sum.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*=============================================================================
  2. Copyright (c) 2002-2015 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. ///////////////////////////////////////////////////////////////////////////////
  7. //
  8. // A parser for summing a comma-separated list of numbers using phoenix.
  9. //
  10. // [ JDG June 28, 2002 ] spirit1
  11. // [ JDG March 24, 2007 ] spirit2
  12. // [ JDG May 12, 2015 ] spirit X3
  13. //
  14. ///////////////////////////////////////////////////////////////////////////////
  15. #include <boost/config/warning_disable.hpp>
  16. #include <boost/spirit/home/x3.hpp>
  17. #include <iostream>
  18. #include <string>
  19. namespace client
  20. {
  21. namespace x3 = boost::spirit::x3;
  22. namespace ascii = boost::spirit::x3::ascii;
  23. using x3::double_;
  24. using ascii::space;
  25. using x3::_attr;
  26. ///////////////////////////////////////////////////////////////////////////
  27. // Our adder
  28. ///////////////////////////////////////////////////////////////////////////
  29. template <typename Iterator>
  30. bool adder(Iterator first, Iterator last, double& n)
  31. {
  32. auto assign = [&](auto& ctx){ n = _attr(ctx); };
  33. auto add = [&](auto& ctx){ n += _attr(ctx); };
  34. bool r = x3::phrase_parse(first, last,
  35. // Begin grammar
  36. (
  37. double_[assign] >> *(',' >> double_[add])
  38. )
  39. ,
  40. // End grammar
  41. space);
  42. if (first != last) // fail if we did not get a full match
  43. return false;
  44. return r;
  45. }
  46. }
  47. ////////////////////////////////////////////////////////////////////////////
  48. // Main program
  49. ////////////////////////////////////////////////////////////////////////////
  50. int
  51. main()
  52. {
  53. std::cout << "/////////////////////////////////////////////////////////\n\n";
  54. std::cout << "\t\tA parser for summing a list of numbers...\n\n";
  55. std::cout << "/////////////////////////////////////////////////////////\n\n";
  56. std::cout << "Give me a comma separated list of numbers.\n";
  57. std::cout << "The numbers are added using Phoenix.\n";
  58. std::cout << "Type [q or Q] to quit\n\n";
  59. std::string str;
  60. while (getline(std::cin, str))
  61. {
  62. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  63. break;
  64. double n;
  65. if (client::adder(str.begin(), str.end(), n))
  66. {
  67. std::cout << "-------------------------\n";
  68. std::cout << "Parsing succeeded\n";
  69. std::cout << str << " Parses OK: " << std::endl;
  70. std::cout << "sum = " << n;
  71. std::cout << "\n-------------------------\n";
  72. }
  73. else
  74. {
  75. std::cout << "-------------------------\n";
  76. std::cout << "Parsing failed\n";
  77. std::cout << "-------------------------\n";
  78. }
  79. }
  80. std::cout << "Bye... :-) \n\n";
  81. return 0;
  82. }