]> git.saurik.com Git - bison.git/blob - examples/variant.yy
examples: minor improvements
[bison.git] / examples / variant.yy
1 /*
2 Copyright (C) 2008-2012 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 %debug
19 %skeleton "lalr1.cc"
20 %defines
21 %define api.token.constructor
22 %define api.value.type variant
23 %define parse.assert
24 %locations
25
26 %code requires // *.hh
27 {
28 #include <list>
29 #include <string>
30 typedef std::list<std::string> strings_type;
31 }
32
33 %code // *.cc
34 {
35 #include <algorithm>
36 #include <iostream>
37 #include <iterator>
38 #include <sstream>
39
40 // Prototype of the yylex function providing subsequent tokens.
41 namespace yy
42 {
43 static parser::symbol_type yylex ();
44 }
45
46 // Printing a list of strings.
47 // Koening look up will look into std, since that's an std::list.
48 namespace std
49 {
50 std::ostream&
51 operator<< (std::ostream& o, const strings_type& s)
52 {
53 std::copy (s.begin (), s.end (),
54 std::ostream_iterator<strings_type::value_type> (o, "\n"));
55 return o;
56 }
57 }
58
59 // Conversion to string.
60 template <typename T>
61 inline
62 std::string
63 string_cast (const T& t)
64 {
65 std::ostringstream o;
66 o << t;
67 return o.str ();
68 }
69 }
70
71 %token <::std::string> TEXT;
72 %token <int> NUMBER;
73 %printer { yyoutput << $$; } <*>;
74 %token END_OF_FILE 0;
75
76 %type <::std::string> item;
77 %type <::std::list<std::string>> list;
78
79 %%
80
81 result:
82 list { std::cout << $1 << std::endl; }
83 ;
84
85 list:
86 /* nothing */ { /* Generates an empty string list */ }
87 | list item { std::swap ($$, $1); $$.push_back ($2); }
88 ;
89
90 item:
91 TEXT { std::swap ($$, $1); }
92 | NUMBER { $$ = string_cast ($1); }
93 ;
94 %%
95
96 namespace yy
97 {
98 // The yylex function providing subsequent tokens:
99 // TEXT "I have three numbers for you."
100 // NUMBER 1
101 // NUMBER 2
102 // NUMBER 3
103 // TEXT "And that's all!"
104 // END_OF_FILE
105
106 static
107 parser::symbol_type
108 yylex ()
109 {
110 static int stage = -1;
111 ++stage;
112 parser::location_type loc(0, stage + 1, stage + 1);
113 switch (stage)
114 {
115 case 0:
116 return parser::make_TEXT ("I have three numbers for you.", loc);
117 case 1:
118 case 2:
119 case 3:
120 return parser::make_NUMBER (stage, loc);
121 case 4:
122 return parser::make_TEXT ("And that's all!", loc);
123 default:
124 return parser::make_END_OF_FILE (loc);
125 }
126 }
127
128 // Mandatory error function
129 void
130 parser::error (const parser::location_type& loc, const std::string& msg)
131 {
132 std::cerr << loc << ": " << msg << std::endl;
133 }
134 }
135
136 int
137 main ()
138 {
139 yy::parser p;
140 p.set_debug_level (!!getenv ("YYDEBUG"));
141 return p.parse ();
142 }
143
144 // Local Variables:
145 // mode: C++
146 // End: