1 /*=============================================================================
2     Copyright (c) 2002-2010 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 complex number micro parser.
10 //
11 //  [ JDG May 10, 2002 ]    spirit1
12 //  [ JDG May 9, 2007 ]     spirit2
13 //
14 ///////////////////////////////////////////////////////////////////////////////
15 
16 #include <boost/config/warning_disable.hpp>
17 #include <boost/spirit/include/qi.hpp>
18 #include <boost/spirit/include/phoenix_core.hpp>
19 #include <boost/spirit/include/phoenix_operator.hpp>
20 
21 #include <iostream>
22 #include <string>
23 #include <complex>
24 
25 ///////////////////////////////////////////////////////////////////////////////
26 //  Our complex number parser/compiler
27 ///////////////////////////////////////////////////////////////////////////////
28 //[tutorial_complex_number
29 namespace client
30 {
31     template <typename Iterator>
parse_complex(Iterator first,Iterator last,std::complex<double> & c)32     bool parse_complex(Iterator first, Iterator last, std::complex<double>& c)
33     {
34         using boost::spirit::qi::double_;
35         using boost::spirit::qi::_1;
36         using boost::spirit::qi::phrase_parse;
37         using boost::spirit::ascii::space;
38         using boost::phoenix::ref;
39 
40         double rN = 0.0;
41         double iN = 0.0;
42         bool r = phrase_parse(first, last,
43 
44             //  Begin grammar
45             (
46                     '(' >> double_[ref(rN) = _1]
47                         >> -(',' >> double_[ref(iN) = _1]) >> ')'
48                 |   double_[ref(rN) = _1]
49             ),
50             //  End grammar
51 
52             space);
53 
54         if (!r || first != last) // fail if we did not get a full match
55             return false;
56         c = std::complex<double>(rN, iN);
57         return r;
58     }
59 }
60 //]
61 
62 ////////////////////////////////////////////////////////////////////////////
63 //  Main program
64 ////////////////////////////////////////////////////////////////////////////
65 int
main()66 main()
67 {
68     std::cout << "/////////////////////////////////////////////////////////\n\n";
69     std::cout << "\t\tA complex number micro parser for Spirit...\n\n";
70     std::cout << "/////////////////////////////////////////////////////////\n\n";
71 
72     std::cout << "Give me a complex number of the form r or (r) or (r,i) \n";
73     std::cout << "Type [q or Q] to quit\n\n";
74 
75     std::string str;
76     while (getline(std::cin, str))
77     {
78         if (str.empty() || str[0] == 'q' || str[0] == 'Q')
79             break;
80 
81         std::complex<double> c;
82         if (client::parse_complex(str.begin(), str.end(), c))
83         {
84             std::cout << "-------------------------\n";
85             std::cout << "Parsing succeeded\n";
86             std::cout << "got: " << c << std::endl;
87             std::cout << "\n-------------------------\n";
88         }
89         else
90         {
91             std::cout << "-------------------------\n";
92             std::cout << "Parsing failed\n";
93             std::cout << "-------------------------\n";
94         }
95     }
96 
97     std::cout << "Bye... :-) \n\n";
98     return 0;
99 }
100 
101 
102