sum.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*=============================================================================
  2. Copyright (c) 2002-2003 Joel de Guzman
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. ///////////////////////////////////////////////////////////////////////////////
  9. //
  10. // A parser for summing a list of numbers. Demonstrating phoenix
  11. // This is discussed in the "Phoenix" chapter in the Spirit User's Guide.
  12. //
  13. // [ JDG 6/28/2002 ]
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #include <boost/spirit/include/classic_core.hpp>
  17. #include <boost/spirit/include/phoenix1_primitives.hpp>
  18. #include <boost/spirit/include/phoenix1_operators.hpp>
  19. #include <iostream>
  20. #include <string>
  21. ///////////////////////////////////////////////////////////////////////////////
  22. using namespace std;
  23. using namespace BOOST_SPIRIT_CLASSIC_NS;
  24. using namespace phoenix;
  25. ///////////////////////////////////////////////////////////////////////////////
  26. //
  27. // Our adder
  28. //
  29. ///////////////////////////////////////////////////////////////////////////////
  30. template <typename IteratorT>
  31. bool adder(IteratorT first, IteratorT last, double& n)
  32. {
  33. return parse(first, last,
  34. // Begin grammar
  35. (
  36. real_p[var(n) = arg1] >> *(',' >> real_p[var(n) += arg1])
  37. )
  38. ,
  39. // End grammar
  40. space_p).full;
  41. }
  42. ////////////////////////////////////////////////////////////////////////////
  43. //
  44. // Main program
  45. //
  46. ////////////////////////////////////////////////////////////////////////////
  47. int
  48. main()
  49. {
  50. cout << "/////////////////////////////////////////////////////////\n\n";
  51. cout << "\t\tA parser for summing a list of numbers...\n\n";
  52. cout << "/////////////////////////////////////////////////////////\n\n";
  53. cout << "Give me a comma separated list of numbers.\n";
  54. cout << "The numbers are added using Phoenix.\n";
  55. cout << "Type [q or Q] to quit\n\n";
  56. string str;
  57. while (getline(cin, str))
  58. {
  59. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  60. break;
  61. double n;
  62. if (adder(str.begin(), str.end(), n))
  63. {
  64. cout << "-------------------------\n";
  65. cout << "Parsing succeeded\n";
  66. cout << str << " Parses OK: " << endl;
  67. cout << "sum = " << n;
  68. cout << "\n-------------------------\n";
  69. }
  70. else
  71. {
  72. cout << "-------------------------\n";
  73. cout << "Parsing failed\n";
  74. cout << "-------------------------\n";
  75. }
  76. }
  77. cout << "Bye... :-) \n\n";
  78. return 0;
  79. }