1 | /*============================================================================= |
---|
2 | Copyright (c) 2003 Jonathan de Halleux (dehalleux@pelikhan.com) |
---|
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 | // This example shows how the assign operator can be used to modify |
---|
11 | // rules with semantic actions |
---|
12 | // |
---|
13 | // First we show the basic spirit without (without any dynamic feature), |
---|
14 | // then we show how to use assign_a to make it dynamic, |
---|
15 | // |
---|
16 | // the grammar has to parse abcabc... sequences |
---|
17 | /////////////////////////////////////////////////////////////////////////////// |
---|
18 | #include <iostream> |
---|
19 | |
---|
20 | #define BOOST_SPIRIT_DEBUG |
---|
21 | #include <boost/spirit.hpp> |
---|
22 | |
---|
23 | #include <boost/spirit/actor/assign_actor.hpp> |
---|
24 | |
---|
25 | int main(int argc, char* argv[]) |
---|
26 | { |
---|
27 | using namespace boost::spirit; |
---|
28 | using namespace std; |
---|
29 | |
---|
30 | rule<> a,b,c,next; |
---|
31 | const char* str="abcabc"; |
---|
32 | parse_info<> hit; |
---|
33 | BOOST_SPIRIT_DEBUG_NODE(next); |
---|
34 | BOOST_SPIRIT_DEBUG_NODE(a); |
---|
35 | BOOST_SPIRIT_DEBUG_NODE(b); |
---|
36 | BOOST_SPIRIT_DEBUG_NODE(c); |
---|
37 | |
---|
38 | // basic spirit gram |
---|
39 | a = ch_p('a') >> !b; |
---|
40 | b = ch_p('b') >> !c; |
---|
41 | c = ch_p('c') >> !a; |
---|
42 | |
---|
43 | hit = parse(str, a); |
---|
44 | cout<<"hit :"<<( hit.hit ? "yes" : "no")<<", " |
---|
45 | <<(hit.full ? "full": "not full") |
---|
46 | <<endl; |
---|
47 | |
---|
48 | // using assign_a |
---|
49 | a = ch_p('a')[ assign_a( next, b)] >> !next; |
---|
50 | b = ch_p('b')[ assign_a( next, c)] >> !next; |
---|
51 | c = ch_p('c')[ assign_a( next, a)] >> !next; |
---|
52 | hit = parse(str, a); |
---|
53 | cout<<"hit :"<<( hit.hit ? "yes" : "no")<<", " |
---|
54 | <<(hit.full ? "full": "not full") |
---|
55 | <<endl; |
---|
56 | |
---|
57 | return 0; |
---|
58 | } |
---|