calc_utree_naive.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // This rather naive example demonstrates that you can pass an instance of a
  18. // utree as the attribute for almost any grammar. As the result the utree will
  19. // be filled with the parse tree as generated during the parsing. This is most
  20. // of the time not what's desired, but is usually a good first step in order to
  21. // prepare your grammar to generate a customized AST. See the calc_utree_ast
  22. // example for a modified version of this grammar filling the attribute with a
  23. // AST (abstract syntax tree) representing the math expression as matched from
  24. // the input.
  25. // #define BOOST_SPIRIT_DEBUG
  26. #include <boost/config/warning_disable.hpp>
  27. #include <boost/spirit/include/qi.hpp>
  28. #include <boost/spirit/include/support_utree.hpp>
  29. #include <iostream>
  30. #include <string>
  31. namespace client
  32. {
  33. namespace qi = boost::spirit::qi;
  34. namespace ascii = boost::spirit::ascii;
  35. namespace spirit = boost::spirit;
  36. ///////////////////////////////////////////////////////////////////////////////
  37. // Our calculator grammar
  38. ///////////////////////////////////////////////////////////////////////////////
  39. template <typename Iterator>
  40. struct calculator : qi::grammar<Iterator, ascii::space_type, spirit::utree()>
  41. {
  42. calculator() : calculator::base_type(expression)
  43. {
  44. using qi::uint_;
  45. using qi::char_;
  46. expression =
  47. term
  48. >> *( (char_('+') >> term)
  49. | (char_('-') >> term)
  50. )
  51. ;
  52. term =
  53. factor
  54. >> *( (char_('*') >> factor)
  55. | (char_('/') >> factor)
  56. )
  57. ;
  58. factor =
  59. uint_
  60. | '(' >> expression >> ')'
  61. | (char_('-') >> factor)
  62. | (char_('+') >> factor)
  63. ;
  64. BOOST_SPIRIT_DEBUG_NODE(expression);
  65. BOOST_SPIRIT_DEBUG_NODE(term);
  66. BOOST_SPIRIT_DEBUG_NODE(factor);
  67. }
  68. qi::rule<Iterator, ascii::space_type, spirit::utree()> expression;
  69. qi::rule<Iterator, ascii::space_type, spirit::utree::list_type()> term;
  70. qi::rule<Iterator, ascii::space_type, spirit::utree::list_type()> factor;
  71. };
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////
  74. // Main program
  75. ///////////////////////////////////////////////////////////////////////////////
  76. int main()
  77. {
  78. std::cout << "/////////////////////////////////////////////////////////\n\n";
  79. std::cout << "Expression parser...\n\n";
  80. std::cout << "/////////////////////////////////////////////////////////\n\n";
  81. std::cout << "Type an expression...or [q or Q] to quit\n\n";
  82. using boost::spirit::ascii::space;
  83. using boost::spirit::utree;
  84. typedef std::string::const_iterator iterator_type;
  85. typedef client::calculator<iterator_type> calculator;
  86. calculator calc; // Our grammar
  87. std::string str;
  88. while (std::getline(std::cin, str))
  89. {
  90. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  91. break;
  92. std::string::const_iterator iter = str.begin();
  93. std::string::const_iterator end = str.end();
  94. utree ut;
  95. bool r = phrase_parse(iter, end, calc, space, ut);
  96. if (r && iter == end)
  97. {
  98. std::cout << "-------------------------\n";
  99. std::cout << "Parsing succeeded: " << ut << "\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. }