calc_utree.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. Copyright (c) 2001-2011 Joel de Guzman
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. ///////////////////////////////////////////////////////////////////////////////
  8. //
  9. // Plain calculator example demonstrating the grammar. The parser is a
  10. // syntax checker only and does not do any semantic evaluation.
  11. //
  12. // [ JDG May 10, 2002 ] spirit1
  13. // [ JDG March 4, 2007 ] spirit2
  14. // [ HK November 30, 2010 ] spirit2/utree
  15. //
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // #define BOOST_SPIRIT_DEBUG
  18. #include <boost/config/warning_disable.hpp>
  19. #include <boost/spirit/include/qi.hpp>
  20. #include <boost/spirit/include/support_utree.hpp>
  21. #include <boost/spirit/include/phoenix_operator.hpp>
  22. #include <boost/spirit/include/phoenix_function.hpp>
  23. #include <iostream>
  24. #include <string>
  25. #if BOOST_PHOENIX_VERSION == 0x2000
  26. namespace boost { namespace phoenix
  27. {
  28. // There's a bug in the Phoenix V2 type deduction mechanism that prevents
  29. // correct return type deduction for the math operations below. Newer
  30. // versions of Phoenix will be switching to BOOST_TYPEOF. In the meantime,
  31. // we will use the specializations helping with return type deduction
  32. // below:
  33. template <>
  34. struct result_of_plus<spirit::utree&, spirit::utree&>
  35. {
  36. typedef spirit::utree type;
  37. };
  38. template <>
  39. struct result_of_minus<spirit::utree&, spirit::utree&>
  40. {
  41. typedef spirit::utree type;
  42. };
  43. template <>
  44. struct result_of_multiplies<spirit::utree&, spirit::utree&>
  45. {
  46. typedef spirit::utree type;
  47. };
  48. template <>
  49. struct result_of_divides<spirit::utree&, spirit::utree&>
  50. {
  51. typedef spirit::utree type;
  52. };
  53. template <>
  54. struct result_of_negate<spirit::utree&>
  55. {
  56. typedef spirit::utree type;
  57. };
  58. }}
  59. #endif
  60. namespace client
  61. {
  62. namespace qi = boost::spirit::qi;
  63. namespace ascii = boost::spirit::ascii;
  64. namespace spirit = boost::spirit;
  65. ///////////////////////////////////////////////////////////////////////////////
  66. // Our calculator grammar
  67. ///////////////////////////////////////////////////////////////////////////////
  68. template <typename Iterator>
  69. struct calculator : qi::grammar<Iterator, ascii::space_type, spirit::utree()>
  70. {
  71. calculator() : calculator::base_type(expression)
  72. {
  73. using qi::uint_;
  74. using qi::_val;
  75. using qi::_1;
  76. expression =
  77. term [_val = _1]
  78. >> *( ('+' >> term [_val = _val + _1])
  79. | ('-' >> term [_val = _val - _1])
  80. )
  81. ;
  82. term =
  83. factor [_val = _1]
  84. >> *( ('*' >> factor [_val = _val * _1])
  85. | ('/' >> factor [_val = _val / _1])
  86. )
  87. ;
  88. factor =
  89. uint_ [_val = _1]
  90. | '(' >> expression [_val = _1] >> ')'
  91. | ('-' >> factor [_val = -_1])
  92. | ('+' >> factor [_val = _1])
  93. ;
  94. BOOST_SPIRIT_DEBUG_NODE(expression);
  95. BOOST_SPIRIT_DEBUG_NODE(term);
  96. BOOST_SPIRIT_DEBUG_NODE(factor);
  97. }
  98. qi::rule<Iterator, ascii::space_type, spirit::utree()> expression, term, factor;
  99. };
  100. }
  101. ///////////////////////////////////////////////////////////////////////////////
  102. // Main program
  103. ///////////////////////////////////////////////////////////////////////////////
  104. int main()
  105. {
  106. std::cout << "/////////////////////////////////////////////////////////\n\n";
  107. std::cout << "Expression parser...\n\n";
  108. std::cout << "/////////////////////////////////////////////////////////\n\n";
  109. std::cout << "Type an expression...or [q or Q] to quit\n\n";
  110. using boost::spirit::ascii::space;
  111. using boost::spirit::utree;
  112. typedef std::string::const_iterator iterator_type;
  113. typedef client::calculator<iterator_type> calculator;
  114. calculator calc; // Our grammar
  115. std::string str;
  116. while (std::getline(std::cin, str))
  117. {
  118. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  119. break;
  120. std::string::const_iterator iter = str.begin();
  121. std::string::const_iterator end = str.end();
  122. utree ut;
  123. bool r = phrase_parse(iter, end, calc, space, ut);
  124. if (r && iter == end)
  125. {
  126. std::cout << "-------------------------\n";
  127. std::cout << "Parsing succeeded: " << ut << "\n";
  128. std::cout << "-------------------------\n";
  129. }
  130. else
  131. {
  132. std::string rest(iter, end);
  133. std::cout << "-------------------------\n";
  134. std::cout << "Parsing failed\n";
  135. std::cout << "stopped at: \": " << rest << "\"\n";
  136. std::cout << "-------------------------\n";
  137. }
  138. }
  139. std::cout << "Bye... :-) \n\n";
  140. return 0;
  141. }