1/*=============================================================================
2 Copyright (c) 2001-2010 Joel de Guzman
3 Copyright (c) 2001-2010 Hartmut Kaiser
4
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7=============================================================================*/
8///////////////////////////////////////////////////////////////////////////////
9//
10// A Calculator example demonstrating generation of AST which gets dumped into
11// a reverse polish notation afterwards.
12//
13// [ JDG April 28, 2008 ]
14// [ HK April 28, 2008 ]
15//
16///////////////////////////////////////////////////////////////////////////////
17
18#include <iostream>
19#include <vector>
20#include <string>
21
22#include "calc2_ast.hpp"
23
24#include <boost/spirit/include/qi.hpp>
25#include <boost/spirit/include/karma.hpp>
26#include <boost/fusion/include/adapt_struct.hpp>
27
28using namespace boost::spirit;
29using namespace boost::spirit::ascii;
30
31///////////////////////////////////////////////////////////////////////////////
32// Our calculator parser grammar
33///////////////////////////////////////////////////////////////////////////////
34template <typename Iterator>
35struct calculator
36 : qi::grammar<Iterator, expression_ast(), space_type>
37{
38 calculator() : calculator::base_type(expression)
39 {
40 expression =
41 term [_val = _1]
42 >> *( ('+' >> term [_val += _1])
43 | ('-' >> term [_val -= _1])
44 )
45 ;
46
47 term =
48 factor [_val = _1]
49 >> *( ('*' >> factor [_val *= _1])
50 | ('/' >> factor [_val /= _1])
51 )
52 ;
53
54 factor =
55 uint_ [_val = _1]
56 | '(' >> expression [_val = _1] >> ')'
57 | ('-' >> factor [_val = neg(_1)])
58 | ('+' >> factor [_val = pos(_1)])
59 ;
60 }
61
62 qi::rule<Iterator, expression_ast(), space_type> expression, term, factor;
63};
64
65// We need to tell fusion about our binary_op and unary_op structs
66// to make them a first-class fusion citizen
67//
68// Note: we register the members exactly in the same sequence as we need them
69// in the grammar
70BOOST_FUSION_ADAPT_STRUCT(
71 binary_op,
72 (expression_ast, left)
73 (expression_ast, right)
74 (char, op)
75)
76
77BOOST_FUSION_ADAPT_STRUCT(
78 unary_op,
79 (expression_ast, right)
80 (char, op)
81)
82
83///////////////////////////////////////////////////////////////////////////////
84// Our AST grammar for the generator, this prints the AST in reverse polish
85// notation
86///////////////////////////////////////////////////////////////////////////////
87template <typename OuputIterator>
88struct ast_rpn
89 : karma::grammar<OuputIterator, expression_ast(), space_type>
90{
91 ast_rpn() : ast_rpn::base_type(ast_node)
92 {
93 ast_node %= int_ | binary_node | unary_node;
94 binary_node %= ast_node << ast_node << char_;
95 unary_node %= '(' << ast_node << char_ << ')';
96 }
97
98 karma::rule<OuputIterator, expression_ast(), space_type> ast_node;
99 karma::rule<OuputIterator, binary_op(), space_type> binary_node;
100 karma::rule<OuputIterator, unary_op(), space_type> unary_node;
101};
102
103///////////////////////////////////////////////////////////////////////////////
104// Main program
105///////////////////////////////////////////////////////////////////////////////
106int
107main()
108{
109 std::cout << "/////////////////////////////////////////////////////////\n\n";
110 std::cout << "RPN generator for simple expressions...\n\n";
111 std::cout << "/////////////////////////////////////////////////////////\n\n";
112 std::cout << "Type an expression...or [q or Q] to quit\n\n";
113
114 // Our parser grammar definitions
115 typedef std::string::const_iterator iterator_type;
116 typedef calculator<iterator_type> calculator;
117
118 calculator calc;
119
120 // Our generator grammar definitions
121 typedef std::back_insert_iterator<std::string> output_iterator_type;
122 typedef ast_rpn<output_iterator_type> ast_rpn;
123
124 ast_rpn ast_grammar;
125
126 std::string str;
127 while (std::getline(is&: std::cin, str&: str))
128 {
129 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
130 break;
131
132 expression_ast ast; // this will hold the generated AST
133
134 std::string::const_iterator iter = str.begin();
135 std::string::const_iterator end = str.end();
136 bool r = qi::phrase_parse(first&: iter, last: end, expr: calc, skipper: space, attr&: ast);
137
138 if (r && iter == end)
139 {
140 std::string generated;
141 output_iterator_type outit(generated);
142 r = karma::generate_delimited(sink&: outit, expr: ast_grammar, delimiter: space, attr: ast);
143
144 if (r)
145 {
146 std::cout << "RPN for '" << str << "': \n" << generated
147 << std::endl;
148 std::cout << "-------------------------\n";
149 }
150 else
151 {
152 std::cout << "-------------------------\n";
153 std::cout << "Generating failed\n";
154 std::cout << "-------------------------\n";
155 }
156 }
157 else
158 {
159 std::string rest(iter, end);
160 std::cout << "-------------------------\n";
161 std::cout << "Parsing failed\n";
162 std::cout << "stopped at: \": " << rest << "\"\n";
163 std::cout << "-------------------------\n";
164 }
165 }
166
167 std::cout << "Bye... :-) \n\n";
168 return 0;
169}
170
171
172

source code of boost/libs/spirit/example/karma/calc2_ast_rpn.cpp