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