• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2011 Hartmut Kaiser
3     Copyright (c) 2001-2011 Joel de Guzman
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 //  Plain calculator example demonstrating the grammar. The parser is a
11 //  syntax checker only and does not do any semantic evaluation.
12 //
13 //  [ JDG May 10, 2002 ]        spirit1
14 //  [ JDG March 4, 2007 ]       spirit2
15 //  [ HK November 30, 2010 ]    spirit2/utree
16 //
17 ///////////////////////////////////////////////////////////////////////////////
18 
19 // This rather naive example demonstrates that you can pass an instance of a
20 // utree as the attribute for almost any grammar. As the result the utree will
21 // be filled with the parse tree as generated during the parsing. This is most
22 // of the time not what's desired, but is usually a good first step in order to
23 // prepare your grammar to generate a customized AST. See the calc_utree_ast
24 // example for a modified version of this grammar filling the attribute with a
25 // AST (abstract syntax tree) representing the math expression as matched from
26 // the input.
27 
28 // #define BOOST_SPIRIT_DEBUG
29 
30 #include <boost/config/warning_disable.hpp>
31 #include <boost/spirit/include/qi.hpp>
32 #include <boost/spirit/include/support_utree.hpp>
33 
34 #include <iostream>
35 #include <string>
36 
37 namespace client
38 {
39     namespace qi = boost::spirit::qi;
40     namespace ascii = boost::spirit::ascii;
41     namespace spirit = boost::spirit;
42 
43     ///////////////////////////////////////////////////////////////////////////////
44     //  Our calculator grammar
45     ///////////////////////////////////////////////////////////////////////////////
46     template <typename Iterator>
47     struct calculator : qi::grammar<Iterator, ascii::space_type, spirit::utree()>
48     {
calculatorclient::calculator49         calculator() : calculator::base_type(expression)
50         {
51             using qi::uint_;
52             using qi::char_;
53 
54             expression =
55                 term
56                 >> *(   (char_('+') >> term)
57                     |   (char_('-') >> term)
58                     )
59                 ;
60 
61             term =
62                 factor
63                 >> *(   (char_('*') >> factor)
64                     |   (char_('/') >> factor)
65                     )
66                 ;
67 
68             factor =
69                 uint_
70                 |   '(' >> expression >> ')'
71                 |   (char_('-') >> factor)
72                 |   (char_('+') >> factor)
73                 ;
74 
75             BOOST_SPIRIT_DEBUG_NODE(expression);
76             BOOST_SPIRIT_DEBUG_NODE(term);
77             BOOST_SPIRIT_DEBUG_NODE(factor);
78         }
79 
80         qi::rule<Iterator, ascii::space_type, spirit::utree()> expression;
81         qi::rule<Iterator, ascii::space_type, spirit::utree::list_type()> term;
82         qi::rule<Iterator, ascii::space_type, spirit::utree::list_type()> factor;
83     };
84 }
85 
86 ///////////////////////////////////////////////////////////////////////////////
87 //  Main program
88 ///////////////////////////////////////////////////////////////////////////////
main()89 int main()
90 {
91     std::cout << "/////////////////////////////////////////////////////////\n\n";
92     std::cout << "Expression parser...\n\n";
93     std::cout << "/////////////////////////////////////////////////////////\n\n";
94     std::cout << "Type an expression...or [q or Q] to quit\n\n";
95 
96     using boost::spirit::ascii::space;
97     using boost::spirit::utree;
98     typedef std::string::const_iterator iterator_type;
99     typedef client::calculator<iterator_type> calculator;
100 
101     calculator calc; // Our grammar
102 
103     std::string str;
104     while (std::getline(std::cin, str))
105     {
106         if (str.empty() || str[0] == 'q' || str[0] == 'Q')
107             break;
108 
109         std::string::const_iterator iter = str.begin();
110         std::string::const_iterator end = str.end();
111         utree ut;
112         bool r = phrase_parse(iter, end, calc, space, ut);
113 
114         if (r && iter == end)
115         {
116             std::cout << "-------------------------\n";
117             std::cout << "Parsing succeeded: " << ut << "\n";
118             std::cout << "-------------------------\n";
119         }
120         else
121         {
122             std::string rest(iter, end);
123             std::cout << "-------------------------\n";
124             std::cout << "Parsing failed\n";
125             std::cout << "stopped at: \": " << rest << "\"\n";
126             std::cout << "-------------------------\n";
127         }
128     }
129 
130     std::cout << "Bye... :-) \n\n";
131     return 0;
132 }
133 
134 
135