ast_calc.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*=============================================================================
  2. Copyright (c) 2001-2003 Daniel Nuffer
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. ///////////////////////////////////////////////////////////////////////////////
  9. //
  10. // Demonstrates the ASTs. This is discussed in the
  11. // "Trees" chapter in the Spirit User's Guide.
  12. //
  13. ///////////////////////////////////////////////////////////////////////////////
  14. #define BOOST_SPIRIT_DUMP_PARSETREE_AS_XML
  15. #include <boost/spirit/include/classic_core.hpp>
  16. #include <boost/spirit/include/classic_ast.hpp>
  17. #include <boost/spirit/include/classic_tree_to_xml.hpp>
  18. #include <boost/assert.hpp>
  19. #include "tree_calc_grammar.hpp"
  20. #include <iostream>
  21. #include <stack>
  22. #include <functional>
  23. #include <string>
  24. #if defined(BOOST_SPIRIT_DUMP_PARSETREE_AS_XML)
  25. #include <map>
  26. #endif
  27. // This example shows how to use an AST.
  28. ////////////////////////////////////////////////////////////////////////////
  29. using namespace std;
  30. using namespace BOOST_SPIRIT_CLASSIC_NS;
  31. typedef char const* iterator_t;
  32. typedef tree_match<iterator_t> parse_tree_match_t;
  33. typedef parse_tree_match_t::tree_iterator iter_t;
  34. ////////////////////////////////////////////////////////////////////////////
  35. long evaluate(parse_tree_match_t hit);
  36. long eval_expression(iter_t const& i);
  37. long evaluate(tree_parse_info<> info)
  38. {
  39. return eval_expression(info.trees.begin());
  40. }
  41. long eval_expression(iter_t const& i)
  42. {
  43. cout << "In eval_expression. i->value = " <<
  44. string(i->value.begin(), i->value.end()) <<
  45. " i->children.size() = " << i->children.size() << endl;
  46. if (i->value.id() == calculator::integerID)
  47. {
  48. BOOST_ASSERT(i->children.size() == 0);
  49. // extract integer (not always delimited by '\0')
  50. string integer(i->value.begin(), i->value.end());
  51. return strtol(integer.c_str(), 0, 10);
  52. }
  53. else if (i->value.id() == calculator::factorID)
  54. {
  55. // factor can only be unary minus
  56. BOOST_ASSERT(*i->value.begin() == '-');
  57. return - eval_expression(i->children.begin());
  58. }
  59. else if (i->value.id() == calculator::termID)
  60. {
  61. if (*i->value.begin() == '*')
  62. {
  63. BOOST_ASSERT(i->children.size() == 2);
  64. return eval_expression(i->children.begin()) *
  65. eval_expression(i->children.begin()+1);
  66. }
  67. else if (*i->value.begin() == '/')
  68. {
  69. BOOST_ASSERT(i->children.size() == 2);
  70. return eval_expression(i->children.begin()) /
  71. eval_expression(i->children.begin()+1);
  72. }
  73. else
  74. BOOST_ASSERT(0);
  75. }
  76. else if (i->value.id() == calculator::expressionID)
  77. {
  78. if (*i->value.begin() == '+')
  79. {
  80. BOOST_ASSERT(i->children.size() == 2);
  81. return eval_expression(i->children.begin()) +
  82. eval_expression(i->children.begin()+1);
  83. }
  84. else if (*i->value.begin() == '-')
  85. {
  86. BOOST_ASSERT(i->children.size() == 2);
  87. return eval_expression(i->children.begin()) -
  88. eval_expression(i->children.begin()+1);
  89. }
  90. else
  91. BOOST_ASSERT(0);
  92. }
  93. else
  94. {
  95. BOOST_ASSERT(0); // error
  96. }
  97. return 0;
  98. }
  99. ////////////////////////////////////////////////////////////////////////////
  100. int
  101. main()
  102. {
  103. // look in tree_calc_grammar for the definition of calculator
  104. calculator calc;
  105. cout << "/////////////////////////////////////////////////////////\n\n";
  106. cout << "\t\tThe simplest working calculator...\n\n";
  107. cout << "/////////////////////////////////////////////////////////\n\n";
  108. cout << "Type an expression...or [q or Q] to quit\n\n";
  109. string str;
  110. while (getline(cin, str))
  111. {
  112. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  113. break;
  114. tree_parse_info<> info = ast_parse(str.c_str(), calc);
  115. if (info.full)
  116. {
  117. #if defined(BOOST_SPIRIT_DUMP_PARSETREE_AS_XML)
  118. // dump parse tree as XML
  119. std::map<parser_id, std::string> rule_names;
  120. rule_names[calculator::integerID] = "integer";
  121. rule_names[calculator::factorID] = "factor";
  122. rule_names[calculator::termID] = "term";
  123. rule_names[calculator::expressionID] = "expression";
  124. tree_to_xml(cout, info.trees, str.c_str(), rule_names);
  125. #endif
  126. // print the result
  127. cout << "parsing succeeded\n";
  128. cout << "result = " << evaluate(info) << "\n\n";
  129. }
  130. else
  131. {
  132. cout << "parsing failed\n";
  133. }
  134. }
  135. cout << "Bye... :-) \n\n";
  136. return 0;
  137. }