ast_calc_tests.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. // JDG 4-16-03 Modified from ast_calc.cpp as a test
  9. #include <boost/spirit/include/classic_core.hpp>
  10. #include <boost/spirit/include/classic_ast.hpp>
  11. #include <boost/spirit/include/classic_tree_to_xml.hpp>
  12. #include <boost/detail/workaround.hpp>
  13. #include <iostream>
  14. #include <stack>
  15. #include <functional>
  16. #include <string>
  17. #include <boost/detail/lightweight_test.hpp>
  18. using namespace BOOST_SPIRIT_CLASSIC_NS;
  19. ////////////////////////////////////////////////////////////////////////////
  20. //
  21. // Our calculator grammar
  22. //
  23. ////////////////////////////////////////////////////////////////////////////
  24. struct calculator : public grammar<calculator>
  25. {
  26. static const int integerID = 1;
  27. static const int factorID = 2;
  28. static const int termID = 3;
  29. static const int expressionID = 4;
  30. template <typename ScannerT>
  31. struct definition
  32. {
  33. definition(calculator const& /*self*/)
  34. {
  35. // Start grammar definition
  36. integer = leaf_node_d[real_p]; // we're not really using a real
  37. // but just for compile checking
  38. // the AST tree match code...
  39. factor = integer
  40. | inner_node_d[ch_p('(') >> expression >> ch_p(')')]
  41. | (root_node_d[ch_p('-')] >> factor);
  42. term = factor >>
  43. *( (root_node_d[ch_p('*')] >> factor)
  44. | (root_node_d[ch_p('/')] >> factor)
  45. );
  46. expression = term >>
  47. *( (root_node_d[ch_p('+')] >> term)
  48. | (root_node_d[ch_p('-')] >> term)
  49. );
  50. // End grammar definition
  51. }
  52. rule<ScannerT, parser_context<>, parser_tag<expressionID> > expression;
  53. rule<ScannerT, parser_context<>, parser_tag<termID> > term;
  54. rule<ScannerT, parser_context<>, parser_tag<factorID> > factor;
  55. rule<ScannerT, parser_context<>, parser_tag<integerID> > integer;
  56. rule<ScannerT, parser_context<>, parser_tag<expressionID> > const&
  57. start() const { return expression; }
  58. };
  59. };
  60. ////////////////////////////////////////////////////////////////////////////
  61. //
  62. // Our calculator grammar, but with dynamically assigned rule ID's
  63. //
  64. ////////////////////////////////////////////////////////////////////////////
  65. struct dyn_calculator : public grammar<dyn_calculator>
  66. {
  67. static const int integerID = 1;
  68. static const int factorID = 2;
  69. static const int termID = 3;
  70. static const int expressionID = 4;
  71. template <typename ScannerT>
  72. struct definition
  73. {
  74. definition(dyn_calculator const& /*self*/)
  75. {
  76. expression.set_id(expressionID);
  77. term.set_id(termID);
  78. factor.set_id(factorID);
  79. integer.set_id(integerID);
  80. // Start grammar definition
  81. integer = leaf_node_d[real_p]; // we're not really using a real
  82. // but just for compile checking
  83. // the AST tree match code...
  84. factor = integer
  85. | inner_node_d[ch_p('(') >> expression >> ch_p(')')]
  86. | (root_node_d[ch_p('-')] >> factor);
  87. term = factor >>
  88. *( (root_node_d[ch_p('*')] >> factor)
  89. | (root_node_d[ch_p('/')] >> factor)
  90. );
  91. expression = term >>
  92. *( (root_node_d[ch_p('+')] >> term)
  93. | (root_node_d[ch_p('-')] >> term)
  94. );
  95. // End grammar definition
  96. }
  97. rule<ScannerT, parser_context<>, dynamic_parser_tag> expression;
  98. rule<ScannerT, parser_context<>, dynamic_parser_tag> term;
  99. rule<ScannerT, parser_context<>, dynamic_parser_tag> factor;
  100. rule<ScannerT, parser_context<>, dynamic_parser_tag> integer;
  101. rule<ScannerT, parser_context<>, dynamic_parser_tag> const&
  102. start() const { return expression; }
  103. };
  104. };
  105. ////////////////////////////////////////////////////////////////////////////
  106. using namespace std;
  107. using namespace BOOST_SPIRIT_CLASSIC_NS;
  108. typedef char const* parser_iterator_t;
  109. typedef tree_match<parser_iterator_t> parse_tree_match_t;
  110. typedef parse_tree_match_t::tree_iterator iter_t;
  111. ////////////////////////////////////////////////////////////////////////////
  112. long evaluate(parse_tree_match_t hit);
  113. long eval_expression(iter_t const& i);
  114. long evaluate(tree_parse_info<> info)
  115. {
  116. return eval_expression(info.trees.begin());
  117. }
  118. long eval_expression(iter_t const& i)
  119. {
  120. switch (i->value.id().to_long())
  121. {
  122. case calculator::integerID:
  123. {
  124. BOOST_TEST(i->children.size() == 0);
  125. // extract integer (not always delimited by '\0')
  126. #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
  127. // std::string(iter,iter) constructor has a bug in MWCW 8.3:
  128. // in some situations, the null terminator won't be added
  129. // and c_str() will return bogus data. Conservatively, I
  130. // activate this workaround up to version 8.3.
  131. std::vector<char> value(i->value.begin(), i->value.end());
  132. value.push_back('\0');
  133. return strtol(&value[0], 0, 10);
  134. #else
  135. string integer(i->value.begin(), i->value.end());
  136. return strtol(integer.c_str(), 0, 10);
  137. #endif
  138. }
  139. case calculator::factorID:
  140. {
  141. // factor can only be unary minus
  142. BOOST_TEST(*i->value.begin() == '-');
  143. return - eval_expression(i->children.begin());
  144. }
  145. case calculator::termID:
  146. {
  147. if (*i->value.begin() == '*')
  148. {
  149. BOOST_TEST(i->children.size() == 2);
  150. return eval_expression(i->children.begin()) *
  151. eval_expression(i->children.begin()+1);
  152. }
  153. else if (*i->value.begin() == '/')
  154. {
  155. BOOST_TEST(i->children.size() == 2);
  156. return eval_expression(i->children.begin()) /
  157. eval_expression(i->children.begin()+1);
  158. }
  159. else
  160. std::abort();
  161. }
  162. case calculator::expressionID:
  163. {
  164. if (*i->value.begin() == '+')
  165. {
  166. BOOST_TEST(i->children.size() == 2);
  167. return eval_expression(i->children.begin()) +
  168. eval_expression(i->children.begin()+1);
  169. }
  170. else if (*i->value.begin() == '-')
  171. {
  172. BOOST_TEST(i->children.size() == 2);
  173. return eval_expression(i->children.begin()) -
  174. eval_expression(i->children.begin()+1);
  175. }
  176. else
  177. std::abort();
  178. }
  179. default:
  180. std::abort(); // error
  181. }
  182. #if defined(_MSC_VER) && (_MSC_VER < 1700)
  183. return 0;
  184. #endif
  185. }
  186. ////////////////////////////////////////////////////////////////////////////
  187. int
  188. parse(char const* str)
  189. {
  190. calculator calc;
  191. tree_parse_info<> info = ast_parse(str, calc, space_p);
  192. if (info.full)
  193. return evaluate(info);
  194. else
  195. return -1;
  196. }
  197. int
  198. parse_dyn(char const* str)
  199. {
  200. dyn_calculator calc;
  201. tree_parse_info<> info = ast_parse(str, calc, space_p);
  202. if (info.full)
  203. return evaluate(info);
  204. else
  205. return -1;
  206. }
  207. int
  208. main()
  209. {
  210. // test the calculator with statically assigned rule ID's
  211. BOOST_TEST(parse("12345") == 12345);
  212. BOOST_TEST(parse("-12345") == -12345);
  213. BOOST_TEST(parse("1 + 2") == 1 + 2);
  214. BOOST_TEST(parse("1 * 2") == 1 * 2);
  215. BOOST_TEST(parse("1/2 + 3/4") == 1/2 + 3/4);
  216. BOOST_TEST(parse("1 + 2 + 3 + 4") == 1 + 2 + 3 + 4);
  217. BOOST_TEST(parse("1 * 2 * 3 * 4") == 1 * 2 * 3 * 4);
  218. BOOST_TEST(parse("(1 + 2) * (3 + 4)") == (1 + 2) * (3 + 4));
  219. BOOST_TEST(parse("(-1 + 2) * (3 + -4)") == (-1 + 2) * (3 + -4));
  220. BOOST_TEST(parse("1 + ((6 * 200) - 20) / 6") == 1 + ((6 * 200) - 20) / 6);
  221. BOOST_TEST(parse("(1 + (2 + (3 + (4 + 5))))") == (1 + (2 + (3 + (4 + 5)))));
  222. BOOST_TEST(parse("1 + 2 + 3 + 4 + 5") == 1 + 2 + 3 + 4 + 5);
  223. BOOST_TEST(parse("(12 * 22) + (36 + -4 + 5)") == (12 * 22) + (36 + -4 + 5));
  224. BOOST_TEST(parse("(12 * 22) / (5 - 10 + 15)") == (12 * 22) / (5 - 10 + 15));
  225. BOOST_TEST(parse("12 * 6 * 15 + 5 - 25") == 12 * 6 * 15 + 5 - 25);
  226. // test the calculator with dynamically assigned rule ID's
  227. BOOST_TEST(parse_dyn("12345") == 12345);
  228. BOOST_TEST(parse_dyn("-12345") == -12345);
  229. BOOST_TEST(parse_dyn("1 + 2") == 1 + 2);
  230. BOOST_TEST(parse_dyn("1 * 2") == 1 * 2);
  231. BOOST_TEST(parse_dyn("1/2 + 3/4") == 1/2 + 3/4);
  232. BOOST_TEST(parse_dyn("1 + 2 + 3 + 4") == 1 + 2 + 3 + 4);
  233. BOOST_TEST(parse_dyn("1 * 2 * 3 * 4") == 1 * 2 * 3 * 4);
  234. BOOST_TEST(parse_dyn("(1 + 2) * (3 + 4)") == (1 + 2) * (3 + 4));
  235. BOOST_TEST(parse_dyn("(-1 + 2) * (3 + -4)") == (-1 + 2) * (3 + -4));
  236. BOOST_TEST(parse_dyn("1 + ((6 * 200) - 20) / 6") == 1 + ((6 * 200) - 20) / 6);
  237. BOOST_TEST(parse_dyn("(1 + (2 + (3 + (4 + 5))))") == (1 + (2 + (3 + (4 + 5)))));
  238. BOOST_TEST(parse_dyn("1 + 2 + 3 + 4 + 5") == 1 + 2 + 3 + 4 + 5);
  239. BOOST_TEST(parse_dyn("(12 * 22) + (36 + -4 + 5)") == (12 * 22) + (36 + -4 + 5));
  240. BOOST_TEST(parse_dyn("(12 * 22) / (5 - 10 + 15)") == (12 * 22) / (5 - 10 + 15));
  241. BOOST_TEST(parse_dyn("12 * 6 * 15 + 5 - 25") == 12 * 6 * 15 + 5 - 25);
  242. return boost::report_errors();
  243. }