rpn_calc.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*=============================================================================
  2. Copyright (c) 2001-2003 Hartmut Kaiser
  3. Copyright (c) 2002-2003 Joel de Guzman
  4. http://spirit.sourceforge.net/
  5. Use, modification and distribution is subject to the Boost Software
  6. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. =============================================================================*/
  9. ///////////////////////////////////////////////////////////////////////////////
  10. //
  11. // This sample shows, how to use Phoenix for implementing a
  12. // simple (RPN style) calculator [ demonstrating phoenix ]
  13. //
  14. // [ HKaiser 2001 ]
  15. // [ JDG 6/29/2002 ]
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #include <boost/spirit/include/classic_core.hpp>
  19. #include <boost/spirit/include/classic_attribute.hpp>
  20. #include <boost/spirit/include/phoenix1_functions.hpp>
  21. #include <iostream>
  22. #include <string>
  23. ///////////////////////////////////////////////////////////////////////////////
  24. using namespace std;
  25. using namespace BOOST_SPIRIT_CLASSIC_NS;
  26. using namespace phoenix;
  27. ///////////////////////////////////////////////////////////////////////////////
  28. //
  29. // Our RPN calculator grammar using phoenix to do the semantics
  30. // The class 'RPNCalculator' implements a polish reverse notation
  31. // calculator which is equivalent to the following YACC description.
  32. //
  33. // exp:
  34. // NUM { $$ = $1; }
  35. // | exp exp '+' { $$ = $1 + $2; }
  36. // | exp exp '-' { $$ = $1 - $2; }
  37. // | exp exp '*' { $$ = $1 * $2; }
  38. // | exp exp '/' { $$ = $1 / $2; }
  39. // | exp exp '^' { $$ = pow ($1, $2); } /* Exponentiation */
  40. // | exp 'n' { $$ = -$1; } /* Unary minus */
  41. // ;
  42. //
  43. // The different notation results from the requirement of LL parsers not to
  44. // allow left recursion in their grammar (would lead to endless recursion).
  45. // Therefore the left recursion in the YACC script before is transformated
  46. // into iteration. To some, this is less intuitive, but once you get used
  47. // to it, it's very easy to follow.
  48. //
  49. // Note: The top rule propagates the expression result (value) upwards
  50. // to the calculator grammar self.val closure member which is
  51. // then visible outside the grammar (i.e. since self.val is the
  52. // member1 of the closure, it becomes the attribute passed by
  53. // the calculator to an attached semantic action. See the
  54. // driver code that uses the calculator below).
  55. //
  56. ///////////////////////////////////////////////////////////////////////////////
  57. struct pow_
  58. {
  59. template <typename X, typename Y>
  60. struct result { typedef X type; };
  61. template <typename X, typename Y>
  62. X operator()(X x, Y y) const
  63. {
  64. using namespace std;
  65. return pow(x, y);
  66. }
  67. };
  68. // Notice how power(x, y) is lazily implemented using Phoenix function.
  69. function<pow_> power;
  70. struct calc_closure : BOOST_SPIRIT_CLASSIC_NS::closure<calc_closure, double, double>
  71. {
  72. member1 x;
  73. member2 y;
  74. };
  75. struct calculator : public grammar<calculator, calc_closure::context_t>
  76. {
  77. template <typename ScannerT>
  78. struct definition {
  79. definition(calculator const& self)
  80. {
  81. top = expr [self.x = arg1];
  82. expr =
  83. real_p [expr.x = arg1]
  84. >> *(
  85. expr [expr.y = arg1]
  86. >> (
  87. ch_p('+') [expr.x += expr.y]
  88. | ch_p('-') [expr.x -= expr.y]
  89. | ch_p('*') [expr.x *= expr.y]
  90. | ch_p('/') [expr.x /= expr.y]
  91. | ch_p('^') [expr.x = power(expr.x, expr.y)]
  92. )
  93. | ch_p('n') [expr.x = -expr.x]
  94. )
  95. ;
  96. }
  97. typedef rule<ScannerT, calc_closure::context_t> rule_t;
  98. rule_t expr;
  99. rule<ScannerT> top;
  100. rule<ScannerT> const&
  101. start() const { return top; }
  102. };
  103. };
  104. ///////////////////////////////////////////////////////////////////////////////
  105. //
  106. // Main program
  107. //
  108. ///////////////////////////////////////////////////////////////////////////////
  109. int
  110. main()
  111. {
  112. cout << "/////////////////////////////////////////////////////////\n\n";
  113. cout << "\t\tExpression parser using Phoenix...\n\n";
  114. cout << "/////////////////////////////////////////////////////////\n\n";
  115. cout << "Type an expression...or [q or Q] to quit\n\n";
  116. calculator calc; // Our parser
  117. string str;
  118. while (getline(cin, str))
  119. {
  120. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  121. break;
  122. double n = 0;
  123. parse_info<> info = parse(str.c_str(), calc[var(n) = arg1], space_p);
  124. // calc[var(n) = arg1] invokes the calculator and extracts
  125. // the result of the computation. See calculator grammar
  126. // note above.
  127. if (info.full)
  128. {
  129. cout << "-------------------------\n";
  130. cout << "Parsing succeeded\n";
  131. cout << "result = " << n << endl;
  132. cout << "-------------------------\n";
  133. }
  134. else
  135. {
  136. cout << "-------------------------\n";
  137. cout << "Parsing failed\n";
  138. cout << "stopped at: \": " << info.stop << "\"\n";
  139. cout << "-------------------------\n";
  140. }
  141. }
  142. cout << "Bye... :-) \n\n";
  143. return 0;
  144. }