• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2003 Joel de Guzman
3     http://spirit.sourceforge.net/
4 
5     Use, modification and distribution is subject to the Boost Software
6     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7     http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 ///////////////////////////////////////////////////////////////////////////////
10 //
11 //  A complex number micro parser (using subrules)
12 //
13 //  [ JDG 5/10/2002 ]
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 #include <boost/spirit/include/classic_core.hpp>
17 #include <iostream>
18 #include <complex>
19 #include <string>
20 
21 ///////////////////////////////////////////////////////////////////////////////
22 using namespace std;
23 using namespace BOOST_SPIRIT_CLASSIC_NS;
24 
25 ///////////////////////////////////////////////////////////////////////////////
26 //
27 //  Our complex number micro parser
28 //
29 ///////////////////////////////////////////////////////////////////////////////
30 bool
parse_complex(char const * str,complex<double> & c)31 parse_complex(char const* str, complex<double>& c)
32 {
33     double rN = 0.0;
34     double iN = 0.0;
35 
36     subrule<0> first;
37     subrule<1> r;
38     subrule<2> i;
39 
40     if (parse(str,
41 
42         //  Begin grammar
43         (
44             first = '(' >> r >> !(',' >> i) >> ')' | r,
45             r = real_p[assign(rN)],
46             i = real_p[assign(iN)]
47         )
48         ,
49         //  End grammar
50 
51         space_p).full)
52     {
53         c = complex<double>(rN, iN);
54         return true;
55     }
56     else
57     {
58         return false;
59     }
60 }
61 
62 ////////////////////////////////////////////////////////////////////////////
63 //
64 //  Main program
65 //
66 ////////////////////////////////////////////////////////////////////////////
67 int
main()68 main()
69 {
70     cout << "/////////////////////////////////////////////////////////\n\n";
71     cout << "\t\tA complex number micro parser for Spirit...\n\n";
72     cout << "/////////////////////////////////////////////////////////\n\n";
73 
74     cout << "Give me a complex number of the form r or (r) or (r,i) \n";
75     cout << "Type [q or Q] to quit\n\n";
76 
77     string str;
78     while (getline(cin, str))
79     {
80         if (str.empty() || str[0] == 'q' || str[0] == 'Q')
81             break;
82 
83         complex<double> c;
84         if (parse_complex(str.c_str(), c))
85         {
86             cout << "-------------------------\n";
87             cout << "Parsing succeeded\n";
88             cout << str << " Parses OK: " << c << endl;
89             cout << "-------------------------\n";
90         }
91         else
92         {
93             cout << "-------------------------\n";
94             cout << "Parsing failed\n";
95             cout << "-------------------------\n";
96         }
97     }
98 
99     cout << "Bye... :-) \n\n";
100     return 0;
101 }
102 
103 
104