1 /*=============================================================================
2 Copyright (c) 2005 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 #include <iostream>
10
11 #define BOOST_SPIRIT_DEBUG
12 #include <boost/spirit/include/classic_core.hpp>
13 #include <boost/assert.hpp>
14
15 using namespace BOOST_SPIRIT_CLASSIC_NS;
16
17 struct non_greedy_kleene : public grammar<non_greedy_kleene>
18 {
19 template <typename ScannerT>
20 struct definition
21 {
22 typedef rule<ScannerT> rule_t;
23 rule_t r;
24
definitionnon_greedy_kleene::definition25 definition(non_greedy_kleene const& self)
26 {
27 r = (alnum_p >> r) | digit_p;
28 BOOST_SPIRIT_DEBUG_RULE(r);
29 }
30
31 rule_t const&
startnon_greedy_kleene::definition32 start() const
33 {
34 return r;
35 }
36 };
37 };
38
39 struct non_greedy_plus : public grammar<non_greedy_plus>
40 {
41 template <typename ScannerT>
42 struct definition
43 {
44 typedef rule<ScannerT> rule_t;
45 rule_t r;
46
definitionnon_greedy_plus::definition47 definition(non_greedy_plus const& self)
48 {
49 r = alnum_p >> (r | digit_p);
50 BOOST_SPIRIT_DEBUG_RULE(r);
51 }
52
53 rule_t const&
startnon_greedy_plus::definition54 start() const
55 {
56 return r;
57 }
58 };
59 };
60 int
main()61 main()
62 {
63 bool success;
64 {
65 non_greedy_kleene k;
66 success = parse("3", k).full;
67 BOOST_ASSERT(success);
68 success = parse("abcdef3", k).full;
69 BOOST_ASSERT(success);
70 success = parse("abc2def3", k).full;
71 BOOST_ASSERT(success);
72 success = parse("abc", k).full;
73 BOOST_ASSERT(!success);
74 }
75
76 {
77 non_greedy_plus p;
78 success = parse("3", p).full;
79 BOOST_ASSERT(!success);
80 success = parse("abcdef3", p).full;
81 BOOST_ASSERT(success);
82 success = parse("abc2def3", p).full;
83 BOOST_ASSERT(success);
84 success = parse("abc", p).full;
85 BOOST_ASSERT(!success);
86 }
87
88 std::cout << "SUCCESS!!!\n";
89 return 0;
90 }
91