1 | /*============================================================================= |
---|
2 | Copyright (c) 2002-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 | // Boiler plate [ A template for writing your parser ] |
---|
12 | // |
---|
13 | // [ JDG 9/17/2002 ] |
---|
14 | // |
---|
15 | /////////////////////////////////////////////////////////////////////////////// |
---|
16 | #include <boost/spirit/core.hpp> |
---|
17 | #include <iostream> |
---|
18 | #include <string> |
---|
19 | |
---|
20 | /////////////////////////////////////////////////////////////////////////////// |
---|
21 | using namespace std; |
---|
22 | using namespace boost::spirit; |
---|
23 | |
---|
24 | /////////////////////////////////////////////////////////////////////////////// |
---|
25 | // |
---|
26 | // Semantic actions |
---|
27 | // |
---|
28 | /////////////////////////////////////////////////////////////////////////////// |
---|
29 | struct my_action |
---|
30 | { |
---|
31 | template <typename IteratorT> |
---|
32 | void operator()(IteratorT first, IteratorT last) const |
---|
33 | { |
---|
34 | string s(first, last); |
---|
35 | cout << "\tMy Action got: " << s << endl; |
---|
36 | } |
---|
37 | }; |
---|
38 | |
---|
39 | /////////////////////////////////////////////////////////////////////////////// |
---|
40 | // |
---|
41 | // My grammar |
---|
42 | // |
---|
43 | /////////////////////////////////////////////////////////////////////////////// |
---|
44 | struct my_grammar : public grammar<my_grammar> |
---|
45 | { |
---|
46 | template <typename ScannerT> |
---|
47 | struct definition |
---|
48 | { |
---|
49 | definition(my_grammar const& self) |
---|
50 | { |
---|
51 | my_rule = |
---|
52 | *lexeme_d[(+graph_p)[my_action()]] |
---|
53 | ; |
---|
54 | } |
---|
55 | |
---|
56 | rule<ScannerT> my_rule; |
---|
57 | rule<ScannerT> const& |
---|
58 | start() const { return my_rule; } |
---|
59 | }; |
---|
60 | }; |
---|
61 | |
---|
62 | /////////////////////////////////////////////////////////////////////////////// |
---|
63 | // |
---|
64 | // Main program |
---|
65 | // |
---|
66 | /////////////////////////////////////////////////////////////////////////////// |
---|
67 | int |
---|
68 | main() |
---|
69 | { |
---|
70 | cout << "/////////////////////////////////////////////////////////\n\n"; |
---|
71 | cout << "\t\t A boiler-plate parser...\n\n"; |
---|
72 | cout << "/////////////////////////////////////////////////////////\n\n"; |
---|
73 | cout << "Type anything or [q or Q] to quit\n\n"; |
---|
74 | |
---|
75 | my_grammar g; |
---|
76 | |
---|
77 | string str; |
---|
78 | while (getline(cin, str)) |
---|
79 | { |
---|
80 | if (str.empty() || str[0] == 'q' || str[0] == 'Q') |
---|
81 | break; |
---|
82 | |
---|
83 | if (parse(str.c_str(), g, space_p).full) |
---|
84 | { |
---|
85 | cout << "parsing succeeded\n"; |
---|
86 | } |
---|
87 | else |
---|
88 | { |
---|
89 | cout << "parsing failed\n"; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | cout << "Bye... :-) \n\n"; |
---|
94 | return 0; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|