primitive_calc.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 primitive calculator that knows how to add and subtract.
  11. // [ demonstrating phoenix ]
  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 primitive calculator
  28. //
  29. ///////////////////////////////////////////////////////////////////////////////
  30. template <typename IteratorT>
  31. bool primitive_calc(IteratorT first, IteratorT last, double& n)
  32. {
  33. return parse(first, last,
  34. // Begin grammar
  35. (
  36. real_p[var(n) = arg1]
  37. >> *( ('+' >> real_p[var(n) += arg1])
  38. | ('-' >> real_p[var(n) -= arg1])
  39. )
  40. )
  41. ,
  42. // End grammar
  43. space_p).full;
  44. }
  45. ////////////////////////////////////////////////////////////////////////////
  46. //
  47. // Main program
  48. //
  49. ////////////////////////////////////////////////////////////////////////////
  50. int
  51. main()
  52. {
  53. cout << "/////////////////////////////////////////////////////////\n\n";
  54. cout << "\t\tA primitive calculator...\n\n";
  55. cout << "/////////////////////////////////////////////////////////\n\n";
  56. cout << "Give me a list of numbers to be added or subtracted.\n";
  57. cout << "Example: 1 + 10 + 3 - 4 + 9\n";
  58. cout << "The result is computed using Phoenix.\n";
  59. cout << "Type [q or Q] to quit\n\n";
  60. string str;
  61. while (getline(cin, str))
  62. {
  63. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  64. break;
  65. double n;
  66. if (primitive_calc(str.begin(), str.end(), n))
  67. {
  68. cout << "-------------------------\n";
  69. cout << "Parsing succeeded\n";
  70. cout << str << " Parses OK: " << endl;
  71. cout << "result = " << n;
  72. cout << "\n-------------------------\n";
  73. }
  74. else
  75. {
  76. cout << "-------------------------\n";
  77. cout << "Parsing failed\n";
  78. cout << "-------------------------\n";
  79. }
  80. }
  81. cout << "Bye... :-) \n\n";
  82. return 0;
  83. }