fork(1) download
  1. #include <boost/spirit/include/qi.hpp>
  2. #include <boost/spirit/include/phoenix.hpp>
  3.  
  4. namespace qi = boost::spirit::qi;
  5. namespace phx = boost::phoenix;
  6.  
  7. typedef boost::function<double()> Value;
  8.  
  9. #define BINARY_FUNCTOR(name, op) \
  10.   struct name \
  11.   { \
  12.   name(Value x, Value y): x_(x), y_(y) {} \
  13.   double operator()() { return x_() op y_(); } \
  14.   Value x_, y_; \
  15.   };
  16.  
  17. BINARY_FUNCTOR(ADD, +)
  18. BINARY_FUNCTOR(SUB, -)
  19. BINARY_FUNCTOR(MUL, *)
  20. BINARY_FUNCTOR(DIV, /)
  21.  
  22. struct LIT
  23. {
  24. LIT(double x): x_(x) {}
  25. double operator()()
  26. {
  27. return x_;
  28. }
  29. double x_;
  30. };
  31.  
  32. struct NEG
  33. {
  34. NEG(Value x): x_(x) {}
  35. double operator()()
  36. {
  37. return -x_();
  38. }
  39. Value x_;
  40. };
  41.  
  42. struct SQRT
  43. {
  44. SQRT(Value x): x_(x) {}
  45. double operator()()
  46. {
  47. return sqrt(x_());
  48. }
  49. Value x_;
  50. };
  51.  
  52. // expression grammar definition
  53. template <typename It, typename Skipper=qi::space_type>
  54. struct parser : qi::grammar<It, Value(), Skipper>
  55. {
  56. parser() : parser::base_type(expression)
  57. {
  58. using namespace qi;
  59. expression =
  60. term [_val = _1]
  61. >> *(('+' >> term [_val = phx::construct<ADD>(_val, _1)])
  62. | ('-' >> term [_val = phx::construct<SUB>(_val, _1)])
  63. );
  64. term =
  65. factor [_val = _1]
  66. >> *(('*' >> factor [_val = phx::construct<MUL>(_val, _1)])
  67. | ('/' >> factor [_val = phx::construct<DIV>(_val, _1)])
  68. );
  69. factor =
  70. double_ [_val = phx::construct<LIT>(_1)]
  71. | '(' >> expression [_val = _1] >> ')'
  72. | ('-' >> factor [_val = phx::construct<NEG>(_1)])
  73. | ('+' >> factor [_val = _1])
  74. | (string("SQRT") >> '(' >> expression [_val = phx::construct<SQRT>(_1)] >> ')');
  75.  
  76. BOOST_SPIRIT_DEBUG_NODE(expression);
  77. BOOST_SPIRIT_DEBUG_NODE(term);
  78. BOOST_SPIRIT_DEBUG_NODE(factor);
  79. }
  80.  
  81. private:
  82. qi::rule<It, std::wstring(), Skipper> scientificNumber;
  83. qi::rule<It, Value(), Skipper> expression, term, factor;
  84.  
  85. };
  86.  
  87.  
  88.  
  89. int main()
  90. {
  91. const std::wstring testExp = L"3E-10*(12-3)";
  92.  
  93. typedef std::wstring::const_iterator It;
  94. It f(testExp.begin()), e(testExp.end());
  95. parser<It, qi::space_type> expressionParser;
  96.  
  97. Value logicExpression;
  98. bool ok = phrase_parse(f,e,expressionParser,qi::space,logicExpression);
  99.  
  100. std::cout << "Success: " << std::boolalpha << ok << "\tValue: " << logicExpression() << '\n';
  101. }
  102.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:39: fatal error: boost/spirit/include/qi.hpp: No such file or directory
compilation terminated.
stdout
Standard output is empty