calc_utree_ast.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/support_utree.hpp>
  20. #include <boost/spirit/include/qi.hpp>
  21. #include <boost/spirit/include/phoenix_operator.hpp>
  22. #include <boost/spirit/include/phoenix_function.hpp>
  23. #include <iostream>
  24. #include <string>
  25. namespace client
  26. {
  27. namespace qi = boost::spirit::qi;
  28. namespace ascii = boost::spirit::ascii;
  29. namespace spirit = boost::spirit;
  30. struct expr
  31. {
  32. template <typename T1, typename T2 = void>
  33. struct result { typedef void type; };
  34. expr(char op) : op(op) {}
  35. void operator()(spirit::utree& expr, spirit::utree const& rhs) const
  36. {
  37. spirit::utree lhs;
  38. lhs.swap(expr);
  39. expr.push_back(spirit::utf8_symbol_range_type(&op, &op+1));
  40. expr.push_back(lhs);
  41. expr.push_back(rhs);
  42. }
  43. char const op;
  44. };
  45. boost::phoenix::function<expr> const plus = expr('+');
  46. boost::phoenix::function<expr> const minus = expr('-');
  47. boost::phoenix::function<expr> const times = expr('*');
  48. boost::phoenix::function<expr> const divide = expr('/');
  49. struct negate_expr
  50. {
  51. template <typename T1, typename T2 = void>
  52. struct result { typedef void type; };
  53. void operator()(spirit::utree& expr, spirit::utree const& rhs) const
  54. {
  55. char const op = '-';
  56. expr.clear();
  57. expr.push_back(spirit::utf8_symbol_range_type(&op, &op+1));
  58. expr.push_back(rhs);
  59. }
  60. };
  61. boost::phoenix::function<negate_expr> neg;
  62. ///////////////////////////////////////////////////////////////////////////////
  63. // Our calculator grammar
  64. ///////////////////////////////////////////////////////////////////////////////
  65. template <typename Iterator>
  66. struct calculator : qi::grammar<Iterator, ascii::space_type, spirit::utree()>
  67. {
  68. calculator() : calculator::base_type(expression)
  69. {
  70. using qi::uint_;
  71. using qi::_val;
  72. using qi::_1;
  73. expression =
  74. term [_val = _1]
  75. >> *( ('+' >> term [plus(_val, _1)])
  76. | ('-' >> term [minus(_val, _1)])
  77. )
  78. ;
  79. term =
  80. factor [_val = _1]
  81. >> *( ('*' >> factor [times(_val, _1)])
  82. | ('/' >> factor [divide(_val, _1)])
  83. )
  84. ;
  85. factor =
  86. uint_ [_val = _1]
  87. | '(' >> expression [_val = _1] >> ')'
  88. | ('-' >> factor [neg(_val, _1)])
  89. | ('+' >> factor [_val = _1])
  90. ;
  91. BOOST_SPIRIT_DEBUG_NODE(expression);
  92. BOOST_SPIRIT_DEBUG_NODE(term);
  93. BOOST_SPIRIT_DEBUG_NODE(factor);
  94. }
  95. qi::rule<Iterator, ascii::space_type, spirit::utree()> expression, term, factor;
  96. };
  97. }
  98. ///////////////////////////////////////////////////////////////////////////////
  99. // Main program
  100. ///////////////////////////////////////////////////////////////////////////////
  101. int main()
  102. {
  103. std::cout << "/////////////////////////////////////////////////////////\n\n";
  104. std::cout << "Expression parser...\n\n";
  105. std::cout << "/////////////////////////////////////////////////////////\n\n";
  106. std::cout << "Type an expression...or [q or Q] to quit\n\n";
  107. using boost::spirit::ascii::space;
  108. using boost::spirit::utree;
  109. typedef std::string::const_iterator iterator_type;
  110. typedef client::calculator<iterator_type> calculator;
  111. calculator calc; // Our grammar
  112. std::string str;
  113. while (std::getline(std::cin, str))
  114. {
  115. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  116. break;
  117. std::string::const_iterator iter = str.begin();
  118. std::string::const_iterator end = str.end();
  119. utree ut;
  120. bool r = phrase_parse(iter, end, calc, space, ut);
  121. if (r && iter == end)
  122. {
  123. std::cout << "-------------------------\n";
  124. std::cout << "Parsing succeeded: " << ut << "\n";
  125. std::cout << "-------------------------\n";
  126. }
  127. else
  128. {
  129. std::string rest(iter, end);
  130. std::cout << "-------------------------\n";
  131. std::cout << "Parsing failed\n";
  132. std::cout << "stopped at: \": " << rest << "\"\n";
  133. std::cout << "-------------------------\n";
  134. }
  135. }
  136. std::cout << "Bye... :-) \n\n";
  137. return 0;
  138. }