sum.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*=============================================================================
  2. Copyright (c) 2002-2010 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. //
  13. ///////////////////////////////////////////////////////////////////////////////
  14. #include <boost/config/warning_disable.hpp>
  15. //[tutorial_adder_includes
  16. #include <boost/spirit/include/qi.hpp>
  17. #include <boost/spirit/include/phoenix_core.hpp>
  18. #include <boost/spirit/include/phoenix_operator.hpp>
  19. #include <iostream>
  20. #include <string>
  21. //]
  22. namespace client
  23. {
  24. //[tutorial_adder_using
  25. namespace qi = boost::spirit::qi;
  26. namespace ascii = boost::spirit::ascii;
  27. namespace phoenix = boost::phoenix;
  28. using qi::double_;
  29. using qi::_1;
  30. using ascii::space;
  31. using phoenix::ref;
  32. //]
  33. ///////////////////////////////////////////////////////////////////////////
  34. // Our adder
  35. ///////////////////////////////////////////////////////////////////////////
  36. //[tutorial_adder
  37. template <typename Iterator>
  38. bool adder(Iterator first, Iterator last, double& n)
  39. {
  40. bool r = qi::phrase_parse(first, last,
  41. // Begin grammar
  42. (
  43. double_[ref(n) = _1] >> *(',' >> double_[ref(n) += _1])
  44. )
  45. ,
  46. // End grammar
  47. space);
  48. if (first != last) // fail if we did not get a full match
  49. return false;
  50. return r;
  51. }
  52. //]
  53. }
  54. ////////////////////////////////////////////////////////////////////////////
  55. // Main program
  56. ////////////////////////////////////////////////////////////////////////////
  57. int
  58. main()
  59. {
  60. std::cout << "/////////////////////////////////////////////////////////\n\n";
  61. std::cout << "\t\tA parser for summing a list of numbers...\n\n";
  62. std::cout << "/////////////////////////////////////////////////////////\n\n";
  63. std::cout << "Give me a comma separated list of numbers.\n";
  64. std::cout << "The numbers are added using Phoenix.\n";
  65. std::cout << "Type [q or Q] to quit\n\n";
  66. std::string str;
  67. while (getline(std::cin, str))
  68. {
  69. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  70. break;
  71. double n;
  72. if (client::adder(str.begin(), str.end(), n))
  73. {
  74. std::cout << "-------------------------\n";
  75. std::cout << "Parsing succeeded\n";
  76. std::cout << str << " Parses OK: " << std::endl;
  77. std::cout << "sum = " << n;
  78. std::cout << "\n-------------------------\n";
  79. }
  80. else
  81. {
  82. std::cout << "-------------------------\n";
  83. std::cout << "Parsing failed\n";
  84. std::cout << "-------------------------\n";
  85. }
  86. }
  87. std::cout << "Bye... :-) \n\n";
  88. return 0;
  89. }