calc3.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. ///////////////////////////////////////////////////////////////////////////////
  7. //
  8. // A calculator example demonstrating the grammar and semantic actions
  9. // using phoenix to do the actual expression evaluation. The parser is
  10. // essentially an "interpreter" that evaluates expressions on the fly.
  11. //
  12. // [ JDG June 29, 2002 ] spirit1
  13. // [ JDG March 5, 2007 ] spirit2
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. // Spirit v2.5 allows you to suppress automatic generation
  17. // of predefined terminals to speed up complation. With
  18. // BOOST_SPIRIT_NO_PREDEFINED_TERMINALS defined, you are
  19. // responsible in creating instances of the terminals that
  20. // you need (e.g. see qi::uint_type uint_ below).
  21. #define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  22. #include <boost/config/warning_disable.hpp>
  23. #include <boost/spirit/include/qi.hpp>
  24. #include <boost/spirit/include/phoenix_operator.hpp>
  25. #include <iostream>
  26. #include <string>
  27. namespace client
  28. {
  29. namespace qi = boost::spirit::qi;
  30. namespace ascii = boost::spirit::ascii;
  31. ///////////////////////////////////////////////////////////////////////////
  32. // Our calculator grammar
  33. ///////////////////////////////////////////////////////////////////////////
  34. template <typename Iterator>
  35. struct calculator : qi::grammar<Iterator, int(), ascii::space_type>
  36. {
  37. calculator() : calculator::base_type(expression)
  38. {
  39. qi::_val_type _val;
  40. qi::_1_type _1;
  41. qi::uint_type uint_;
  42. expression =
  43. term [_val = _1]
  44. >> *( ('+' >> term [_val += _1])
  45. | ('-' >> term [_val -= _1])
  46. )
  47. ;
  48. term =
  49. factor [_val = _1]
  50. >> *( ('*' >> factor [_val *= _1])
  51. | ('/' >> factor [_val /= _1])
  52. )
  53. ;
  54. factor =
  55. uint_ [_val = _1]
  56. | '(' >> expression [_val = _1] >> ')'
  57. | ('-' >> factor [_val = -_1])
  58. | ('+' >> factor [_val = _1])
  59. ;
  60. }
  61. qi::rule<Iterator, int(), ascii::space_type> expression, term, factor;
  62. };
  63. }
  64. ///////////////////////////////////////////////////////////////////////////////
  65. // Main program
  66. ///////////////////////////////////////////////////////////////////////////////
  67. int
  68. main()
  69. {
  70. std::cout << "/////////////////////////////////////////////////////////\n\n";
  71. std::cout << "Expression parser...\n\n";
  72. std::cout << "/////////////////////////////////////////////////////////\n\n";
  73. std::cout << "Type an expression...or [q or Q] to quit\n\n";
  74. typedef std::string::const_iterator iterator_type;
  75. typedef client::calculator<iterator_type> calculator;
  76. boost::spirit::ascii::space_type space; // Our skipper
  77. calculator calc; // Our grammar
  78. std::string str;
  79. int result;
  80. while (std::getline(std::cin, str))
  81. {
  82. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  83. break;
  84. std::string::const_iterator iter = str.begin();
  85. std::string::const_iterator end = str.end();
  86. bool r = phrase_parse(iter, end, calc, space, result);
  87. if (r && iter == end)
  88. {
  89. std::cout << "-------------------------\n";
  90. std::cout << "Parsing succeeded\n";
  91. std::cout << "result = " << result << std::endl;
  92. std::cout << "-------------------------\n";
  93. }
  94. else
  95. {
  96. std::string rest(iter, end);
  97. std::cout << "-------------------------\n";
  98. std::cout << "Parsing failed\n";
  99. std::cout << "stopped at: \" " << rest << "\"\n";
  100. std::cout << "-------------------------\n";
  101. }
  102. }
  103. std::cout << "Bye... :-) \n\n";
  104. return 0;
  105. }