calc2.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 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 lambda functions. The parser prints code suitable for a stack
  10. // based virtual machine.
  11. //
  12. // [ JDG May 10, 2002 ] spirit 1
  13. // [ JDG March 4, 2007 ] spirit 2
  14. // [ JDG February 21, 2011 ] spirit 2.5
  15. // [ JDG June 6, 2014 ] spirit x3 (from qi calc2, but using lambda functions)
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #include <boost/config/warning_disable.hpp>
  19. #include <boost/spirit/home/x3.hpp>
  20. #include <boost/spirit/home/x3/support/ast/variant.hpp>
  21. #include <boost/fusion/include/adapt_struct.hpp>
  22. #include <iostream>
  23. #include <string>
  24. #include <list>
  25. #include <numeric>
  26. namespace x3 = boost::spirit::x3;
  27. namespace client
  28. {
  29. ///////////////////////////////////////////////////////////////////////////////
  30. // Semantic actions
  31. ////////////////////////////////////////////////////////1///////////////////////
  32. namespace
  33. {
  34. using x3::_attr;
  35. auto do_int = [](auto& ctx) { std::cout << "push " << _attr(ctx) << std::endl; };
  36. auto do_add = []{ std::cout << "add\n"; };
  37. auto do_subt = []{ std::cout << "subtract\n"; };
  38. auto do_mult = []{ std::cout << "mult\n"; };
  39. auto do_div = []{ std::cout << "divide\n"; };
  40. auto do_neg = []{ std::cout << "negate\n"; };
  41. }
  42. ///////////////////////////////////////////////////////////////////////////////
  43. // The calculator grammar
  44. ///////////////////////////////////////////////////////////////////////////////
  45. namespace calculator_grammar
  46. {
  47. using x3::uint_;
  48. using x3::char_;
  49. x3::rule<class expression> const expression("expression");
  50. x3::rule<class term> const term("term");
  51. x3::rule<class factor> const factor("factor");
  52. auto const expression_def =
  53. term
  54. >> *( ('+' >> term [do_add])
  55. | ('-' >> term [do_subt])
  56. )
  57. ;
  58. auto const term_def =
  59. factor
  60. >> *( ('*' >> factor [do_mult])
  61. | ('/' >> factor [do_div])
  62. )
  63. ;
  64. auto const factor_def =
  65. uint_ [do_int]
  66. | '(' >> expression >> ')'
  67. | ('-' >> factor [do_neg])
  68. | ('+' >> factor)
  69. ;
  70. BOOST_SPIRIT_DEFINE(
  71. expression
  72. , term
  73. , factor
  74. );
  75. auto calculator = expression;
  76. }
  77. using calculator_grammar::calculator;
  78. }
  79. ///////////////////////////////////////////////////////////////////////////////
  80. // Main program
  81. ///////////////////////////////////////////////////////////////////////////////
  82. int
  83. main()
  84. {
  85. std::cout << "/////////////////////////////////////////////////////////\n\n";
  86. std::cout << "Expression parser...\n\n";
  87. std::cout << "/////////////////////////////////////////////////////////\n\n";
  88. std::cout << "Type an expression...or [q or Q] to quit\n\n";
  89. typedef std::string::const_iterator iterator_type;
  90. std::string str;
  91. while (std::getline(std::cin, str))
  92. {
  93. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  94. break;
  95. auto& calc = client::calculator; // Our grammar
  96. iterator_type iter = str.begin();
  97. iterator_type end = str.end();
  98. boost::spirit::x3::ascii::space_type space;
  99. bool r = phrase_parse(iter, end, calc, space);
  100. if (r && iter == end)
  101. {
  102. std::cout << "-------------------------\n";
  103. std::cout << "Parsing succeeded\n";
  104. std::cout << "-------------------------\n";
  105. }
  106. else
  107. {
  108. std::string rest(iter, end);
  109. std::cout << "-------------------------\n";
  110. std::cout << "Parsing failed\n";
  111. std::cout << "stopped at: \"" << rest << "\"\n";
  112. std::cout << "-------------------------\n";
  113. }
  114. }
  115. std::cout << "Bye... :-) \n\n";
  116. return 0;
  117. }