calc2.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 plain functions. The parser prints code suitable for a stack
  10. // based virtual machine.
  11. //
  12. // [ JDG May 10, 2002 ] spirit1
  13. // [ JDG March 4, 2007 ] spirit2
  14. // [ JDG February 21, 2011 ] spirit2.5
  15. //
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // Spirit v2.5 allows you to suppress automatic generation
  18. // of predefined terminals to speed up complation. With
  19. // BOOST_SPIRIT_NO_PREDEFINED_TERMINALS defined, you are
  20. // responsible in creating instances of the terminals that
  21. // you need (e.g. see qi::uint_type uint_ below).
  22. #define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  23. #include <boost/config/warning_disable.hpp>
  24. #include <boost/spirit/include/qi.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. // Semantic actions
  33. ////////////////////////////////////////////////////////1///////////////////////
  34. namespace
  35. {
  36. void do_int(int n) { std::cout << "push " << n << std::endl; }
  37. void do_add() { std::cout << "add\n"; }
  38. void do_subt() { std::cout << "subtract\n"; }
  39. void do_mult() { std::cout << "mult\n"; }
  40. void do_div() { std::cout << "divide\n"; }
  41. void do_neg() { std::cout << "negate\n"; }
  42. }
  43. ///////////////////////////////////////////////////////////////////////////////
  44. // Our calculator grammar
  45. ///////////////////////////////////////////////////////////////////////////////
  46. template <typename Iterator>
  47. struct calculator : qi::grammar<Iterator, ascii::space_type>
  48. {
  49. calculator() : calculator::base_type(expression)
  50. {
  51. qi::uint_type uint_;
  52. expression =
  53. term
  54. >> *( ('+' >> term [&do_add])
  55. | ('-' >> term [&do_subt])
  56. )
  57. ;
  58. term =
  59. factor
  60. >> *( ('*' >> factor [&do_mult])
  61. | ('/' >> factor [&do_div])
  62. )
  63. ;
  64. factor =
  65. uint_ [&do_int]
  66. | '(' >> expression >> ')'
  67. | ('-' >> factor [&do_neg])
  68. | ('+' >> factor)
  69. ;
  70. }
  71. qi::rule<Iterator, ascii::space_type> expression, term, factor;
  72. };
  73. }
  74. ///////////////////////////////////////////////////////////////////////////////
  75. // Main program
  76. ///////////////////////////////////////////////////////////////////////////////
  77. int
  78. main()
  79. {
  80. std::cout << "/////////////////////////////////////////////////////////\n\n";
  81. std::cout << "Expression parser...\n\n";
  82. std::cout << "/////////////////////////////////////////////////////////\n\n";
  83. std::cout << "Type an expression...or [q or Q] to quit\n\n";
  84. typedef std::string::const_iterator iterator_type;
  85. typedef client::calculator<iterator_type> calculator;
  86. boost::spirit::ascii::space_type space; // Our skipper
  87. calculator calc; // Our grammar
  88. std::string str;
  89. while (std::getline(std::cin, str))
  90. {
  91. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  92. break;
  93. std::string::const_iterator iter = str.begin();
  94. std::string::const_iterator end = str.end();
  95. bool r = phrase_parse(iter, end, calc, space);
  96. if (r && iter == end)
  97. {
  98. std::cout << "-------------------------\n";
  99. std::cout << "Parsing succeeded\n";
  100. std::cout << "-------------------------\n";
  101. }
  102. else
  103. {
  104. std::string rest(iter, end);
  105. std::cout << "-------------------------\n";
  106. std::cout << "Parsing failed\n";
  107. std::cout << "stopped at: \" " << rest << "\"\n";
  108. std::cout << "-------------------------\n";
  109. }
  110. }
  111. std::cout << "Bye... :-) \n\n";
  112. return 0;
  113. }