1/*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
3
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// A Calculator example demonstrating generation of AST. The AST,
10// once created, is traversed, 1) To print its contents and
11// 2) To evaluate the result.
12//
13// [ JDG April 28, 2008 ] For BoostCon 2008
14// [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
15// [ JDG January 9, 2013 ] Spirit X3
16//
17///////////////////////////////////////////////////////////////////////////////
18
19#if defined(_MSC_VER)
20# pragma warning(disable: 4345)
21#endif
22
23#include <boost/spirit/home/x3.hpp>
24#include <boost/spirit/home/x3/support/ast/variant.hpp>
25#include <boost/fusion/include/adapt_struct.hpp>
26
27#include <iostream>
28#include <string>
29#include <list>
30#include <numeric>
31
32namespace x3 = boost::spirit::x3;
33
34namespace client { namespace ast
35{
36 ///////////////////////////////////////////////////////////////////////////
37 // The AST
38 ///////////////////////////////////////////////////////////////////////////
39 struct nil {};
40 struct signed_;
41 struct program;
42
43 struct operand : x3::variant<
44 nil
45 , unsigned int
46 , x3::forward_ast<signed_>
47 , x3::forward_ast<program>
48 >
49 {
50 using base_type::base_type;
51 using base_type::operator=;
52 };
53
54 struct signed_
55 {
56 char sign;
57 operand operand_;
58 };
59
60 struct operation
61 {
62 char operator_;
63 operand operand_;
64 };
65
66 struct program
67 {
68 operand first;
69 std::list<operation> rest;
70 };
71}}
72
73BOOST_FUSION_ADAPT_STRUCT(client::ast::signed_,
74 sign, operand_
75)
76
77BOOST_FUSION_ADAPT_STRUCT(client::ast::operation,
78 operator_, operand_
79)
80
81BOOST_FUSION_ADAPT_STRUCT(client::ast::program,
82 first, rest
83)
84
85namespace client { namespace ast
86{
87 ///////////////////////////////////////////////////////////////////////////
88 // The AST Printer
89 ///////////////////////////////////////////////////////////////////////////
90 struct printer
91 {
92 typedef void result_type;
93
94 void operator()(nil) const {}
95 void operator()(unsigned int n) const { std::cout << n; }
96
97 void operator()(operation const& x) const
98 {
99 boost::apply_visitor(visitor: *this, visitable: x.operand_);
100 switch (x.operator_)
101 {
102 case '+': std::cout << " add"; break;
103 case '-': std::cout << " subt"; break;
104 case '*': std::cout << " mult"; break;
105 case '/': std::cout << " div"; break;
106 }
107 }
108
109 void operator()(signed_ const& x) const
110 {
111 boost::apply_visitor(visitor: *this, visitable: x.operand_);
112 switch (x.sign)
113 {
114 case '-': std::cout << " neg"; break;
115 case '+': std::cout << " pos"; break;
116 }
117 }
118
119 void operator()(program const& x) const
120 {
121 boost::apply_visitor(visitor: *this, visitable: x.first);
122 for (operation const& oper: x.rest)
123 {
124 std::cout << ' ';
125 (*this)(oper);
126 }
127 }
128 };
129
130 ///////////////////////////////////////////////////////////////////////////
131 // The AST evaluator
132 ///////////////////////////////////////////////////////////////////////////
133 struct eval
134 {
135 typedef int result_type;
136
137 int operator()(nil) const { BOOST_ASSERT(0); return 0; }
138 int operator()(unsigned int n) const { return n; }
139
140 int operator()(int lhs, operation const& x) const
141 {
142 int rhs = boost::apply_visitor(visitor: *this, visitable: x.operand_);
143 switch (x.operator_)
144 {
145 case '+': return lhs + rhs;
146 case '-': return lhs - rhs;
147 case '*': return lhs * rhs;
148 case '/': return lhs / rhs;
149 }
150 BOOST_ASSERT(0);
151 return 0;
152 }
153
154 int operator()(signed_ const& x) const
155 {
156 int rhs = boost::apply_visitor(visitor: *this, visitable: x.operand_);
157 switch (x.sign)
158 {
159 case '-': return -rhs;
160 case '+': return +rhs;
161 }
162 BOOST_ASSERT(0);
163 return 0;
164 }
165
166 int operator()(program const& x) const
167 {
168 return std::accumulate(
169 first: x.rest.begin(), last: x.rest.end()
170 , init: boost::apply_visitor(visitor: *this, visitable: x.first)
171 , binary_op: *this);
172 }
173 };
174}}
175
176namespace client
177{
178 ///////////////////////////////////////////////////////////////////////////////
179 // The calculator grammar
180 ///////////////////////////////////////////////////////////////////////////////
181 namespace calculator_grammar
182 {
183 using x3::uint_;
184 using x3::char_;
185
186 x3::rule<class expression, ast::program> const expression("expression");
187 x3::rule<class term, ast::program> const term("term");
188 x3::rule<class factor, ast::operand> const factor("factor");
189
190 auto const expression_def =
191 term
192 >> *( (char_('+') >> term)
193 | (char_('-') >> term)
194 )
195 ;
196
197 auto const term_def =
198 factor
199 >> *( (char_('*') >> factor)
200 | (char_('/') >> factor)
201 )
202 ;
203
204 auto const factor_def =
205 uint_
206 | '(' >> expression >> ')'
207 | (char_('-') >> factor)
208 | (char_('+') >> factor)
209 ;
210
211 BOOST_SPIRIT_DEFINE(expression, term, factor);
212
213 auto calculator = expression;
214 }
215
216 using calculator_grammar::calculator;
217
218}
219
220///////////////////////////////////////////////////////////////////////////////
221// Main program
222///////////////////////////////////////////////////////////////////////////////
223int
224main()
225{
226 std::cout << "/////////////////////////////////////////////////////////\n\n";
227 std::cout << "Expression parser...\n\n";
228 std::cout << "/////////////////////////////////////////////////////////\n\n";
229 std::cout << "Type an expression...or [q or Q] to quit\n\n";
230
231 typedef std::string::const_iterator iterator_type;
232 typedef client::ast::program ast_program;
233 typedef client::ast::printer ast_print;
234 typedef client::ast::eval ast_eval;
235
236 std::string str;
237 while (std::getline(is&: std::cin, str&: str))
238 {
239 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
240 break;
241
242 auto& calc = client::calculator; // Our grammar
243 ast_program program; // Our program (AST)
244 ast_print print; // Prints the program
245 ast_eval eval; // Evaluates the program
246
247 iterator_type iter = str.begin();
248 iterator_type end = str.end();
249 boost::spirit::x3::ascii::space_type space;
250 bool r = phrase_parse(first&: iter, last: end, p: calc, s: space, attr&: program);
251
252 if (r && iter == end)
253 {
254 std::cout << "-------------------------\n";
255 std::cout << "Parsing succeeded\n";
256 print(program);
257 std::cout << "\nResult: " << eval(program) << std::endl;
258 std::cout << "-------------------------\n";
259 }
260 else
261 {
262 std::string rest(iter, end);
263 std::cout << "-------------------------\n";
264 std::cout << "Parsing failed\n";
265 std::cout << "stopped at: \"" << rest << "\"\n";
266 std::cout << "-------------------------\n";
267 }
268 }
269
270 std::cout << "Bye... :-) \n\n";
271 return 0;
272}
273

source code of boost/libs/spirit/example/x3/calc/calc4b.cpp