]> git.saurik.com Git - bison.git/blob - data/glr.cc
* NEWS (2.3a+): Mention yesterday's state-removal change.
[bison.git] / data / glr.cc
1 -*- C -*-
2
3 # C++ GLR skeleton for Bison
4 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation,
5 # Inc.
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 # 02110-1301 USA
21
22
23 # This skeleton produces a C++ class that encapsulates a C glr parser.
24 # This is in order to reduce the maintenance burden. The glr.c
25 # skeleton is clean and pure enough so that there are no real
26 # problems. The C++ interface is the same as that of lalr1.cc. In
27 # fact, glr.c can replace yacc.c without the user noticing any
28 # difference, and similarly for glr.cc replacing lalr1.cc.
29 #
30 # The passing of parse-params
31 #
32 # The additional arguments are stored as members of the parser
33 # object, yyparser. The C routines need to carry yyparser
34 # throughout the C parser; that easy: just let yyparser become an
35 # additional parse-param. But because the C++ skeleton needs to
36 # know the "real" original parse-param, we save them
37 # (b4_parse_param_orig). Note that b4_parse_param is overquoted
38 # (and c.m4 strips one level of quotes). This is a PITA, and
39 # explains why there are so many levels of quotes.
40 #
41 # The locations
42 #
43 # We use location.cc just like lalr1.cc, but because glr.c stores
44 # the locations in a (C++) union, the position and location classes
45 # must not have a constructor. Therefore, contrary to lalr1.cc, we
46 # must not define "b4_location_constructors". As a consequence the
47 # user must initialize the first positions (in particular the
48 # filename member).
49
50 # We require a pure interface using locations.
51 m4_define([b4_locations_flag], [1])
52 m4_define([b4_pure_flag], [1])
53
54 # The header is mandatory.
55 b4_defines_if([],
56 [b4_fatal([b4_skeleton[: using %%defines is mandatory]])])
57
58 m4_include(b4_pkgdatadir/[c++.m4])
59 m4_include(b4_pkgdatadir/[location.cc])
60
61 m4_define([b4_parser_class_name],
62 [b4_percent_define_get([[parser_class_name]])])
63 m4_define([b4_namespace],
64 [b4_percent_define_get([[namespace]])])
65
66 # Save the parse parameters.
67 m4_define([b4_parse_param_orig], m4_defn([b4_parse_param]))
68
69
70 # b4_yy_symbol_print_generate
71 # ---------------------------
72 # Bypass the default implementation to generate the "yy_symbol_print"
73 # and "yy_symbol_value_print" functions.
74 m4_define([b4_yy_symbol_print_generate],
75 [[
76 /*--------------------.
77 | Print this symbol. |
78 `--------------------*/
79
80 ]b4_c_ansi_function_def([yy_symbol_print],
81 [static void],
82 [[FILE *], []],
83 [[int yytype], [yytype]],
84 [[const b4_namespace::b4_parser_class_name::semantic_type *yyvaluep],
85 [yyvaluep]],
86 [[const b4_namespace::b4_parser_class_name::location_type *yylocationp],
87 [yylocationp]],
88 b4_parse_param)[
89 {
90 ]b4_parse_param_use[]dnl
91 [ yyparser.yy_symbol_print_ (yytype, yyvaluep]b4_locations_if([, yylocationp])[);
92 }
93 ]])
94
95
96 # Declare yyerror.
97 m4_append([b4_post_prologue],
98 [b4_syncline([@oline@], [@ofile@])
99
100 b4_c_ansi_function_decl([yyerror],
101 [static void],
102 [[b4_namespace::b4_parser_class_name::location_type *yylocationp], [yylocationp]],
103 b4_parse_param,
104 [[const char* msg], [msg]])])
105
106
107 # Define yyerror.
108 m4_append([b4_epilogue],
109 [b4_syncline([@oline@], [@ofile@])[
110 /*------------------.
111 | Report an error. |
112 `------------------*/
113
114 ]b4_c_ansi_function_def([yyerror],
115 [static void],
116 [[b4_namespace::b4_parser_class_name::location_type *yylocationp], [yylocationp]],
117 b4_parse_param,
118 [[const char* msg], [msg]])[
119 {
120 ]b4_parse_param_use[]dnl
121 [ yyparser.error (*yylocationp, msg);
122 }
123
124
125 namespace ]b4_namespace[
126 {
127 ]dnl In this section, the parse param are the original parse_params.
128 m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_orig]))dnl
129 [ /// Build a parser object.
130 ]b4_parser_class_name::b4_parser_class_name[ (]b4_parse_param_decl[)]m4_ifset([b4_parse_param], [
131 :])[
132 #if YYDEBUG
133 ]m4_ifset([b4_parse_param], [ ], [ :])[yydebug_ (false),
134 yycdebug_ (&std::cerr)]m4_ifset([b4_parse_param], [,])[
135 #endif]b4_parse_param_cons[
136 {
137 }
138
139 ]b4_parser_class_name::~b4_parser_class_name[ ()
140 {
141 }
142
143 int
144 ]b4_parser_class_name[::parse ()
145 {
146 return ::yyparse (*this]b4_user_args[);
147 }
148
149 #if YYDEBUG
150 /*--------------------.
151 | Print this symbol. |
152 `--------------------*/
153
154 inline void
155 ]b4_parser_class_name[::yy_symbol_value_print_ (int yytype,
156 const semantic_type* yyvaluep, const location_type* yylocationp)
157 {
158 /* Pacify ``unused variable'' warnings. */
159 YYUSE (yyvaluep);
160 YYUSE (yylocationp);
161 switch (yytype)
162 {
163 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
164 [ default:
165 break;
166 }
167 }
168
169
170 void
171 ]b4_parser_class_name[::yy_symbol_print_ (int yytype,
172 const semantic_type* yyvaluep, const location_type* yylocationp)
173 {
174 *yycdebug_ << (yytype < YYNTOKENS ? "token" : "nterm")
175 << ' ' << yytname[yytype] << " ("
176 << *yylocationp << ": ";
177 yy_symbol_value_print_ (yytype, yyvaluep, yylocationp);
178 *yycdebug_ << ')';
179 }
180
181 std::ostream&
182 ]b4_parser_class_name[::debug_stream () const
183 {
184 return *yycdebug_;
185 }
186
187 void
188 ]b4_parser_class_name[::set_debug_stream (std::ostream& o)
189 {
190 yycdebug_ = &o;
191 }
192
193
194 ]b4_parser_class_name[::debug_level_type
195 ]b4_parser_class_name[::debug_level () const
196 {
197 return yydebug_;
198 }
199
200 void
201 ]b4_parser_class_name[::set_debug_level (debug_level_type l)
202 {
203 yydebug_ = l;
204 }
205
206 #endif
207 ]m4_popdef([b4_parse_param])dnl
208 [} // namespace ]b4_namespace[
209
210 ]])
211
212
213 # Let glr.c believe that the user arguments include the parser itself.
214 m4_ifset([b4_parse_param],
215 [m4_pushdef([b4_parse_param],
216 m4_dquote([[[b4_namespace::b4_parser_class_name& yyparser], [[yyparser]]],]
217 m4_defn([b4_parse_param])))],
218 [m4_pushdef([b4_parse_param],
219 [[[[b4_namespace::b4_parser_class_name& yyparser], [[yyparser]]]]])
220 ])
221 m4_include(b4_pkgdatadir/[glr.c])
222 m4_popdef([b4_parse_param])
223
224 m4_divert_push(0)
225 @output(b4_spec_defines_file@)
226 b4_copyright([Skeleton interface for Bison GLR parsers in C++],
227 [2002, 2003, 2004, 2005, 2006])[
228
229 /* C++ GLR parser skeleton written by Akim Demaille. */
230
231 #ifndef PARSER_HEADER_H
232 # define PARSER_HEADER_H
233
234 ]b4_percent_code_get([[requires]])[
235
236 #include <string>
237 #include <iostream>
238
239 /* Using locations. */
240 #define YYLSP_NEEDED ]b4_locations_flag[
241
242 namespace ]b4_namespace[
243 {
244 class position;
245 class location;
246 }
247
248 #include "location.hh"
249
250 /* Enabling traces. */
251 #ifndef YYDEBUG
252 # define YYDEBUG ]b4_debug_flag[
253 #endif
254
255 /* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
256 If N is 0, then set CURRENT to the empty location which ends
257 the previous symbol: RHS[0] (always defined). */
258
259 #ifndef YYLLOC_DEFAULT
260 # define YYLLOC_DEFAULT(Current, Rhs, N) \
261 do \
262 if (N) \
263 { \
264 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
265 (Current).end = YYRHSLOC (Rhs, N).end; \
266 } \
267 else \
268 { \
269 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
270 } \
271 while (/*CONSTCOND*/ 0)
272 #endif
273
274 namespace ]b4_namespace[
275 {
276 /// A Bison parser.
277 class ]b4_parser_class_name[
278 {
279 public:
280 /// Symbol semantic values.
281 #ifndef YYSTYPE
282 ]m4_ifdef([b4_stype],
283 [ union semantic_type
284 {
285 b4_user_stype
286 };],
287 [m4_if(b4_tag_seen_flag, 0,
288 [[ typedef int semantic_type;]],
289 [[ typedef YYSTYPE semantic_type;]])])[
290 #else
291 typedef YYSTYPE semantic_type;
292 #endif
293 /// Symbol locations.
294 typedef ]b4_percent_define_get([[location_type]])[ location_type;
295 /// Tokens.
296 struct token
297 {
298 ]b4_token_enums(b4_tokens)[
299 };
300 /// Token type.
301 typedef token::yytokentype token_type;
302
303 /// Build a parser object.
304 ]b4_parser_class_name[ (]b4_parse_param_decl[);
305 virtual ~]b4_parser_class_name[ ();
306
307 /// Parse.
308 /// \returns 0 iff parsing succeeded.
309 virtual int parse ();
310
311 /// The current debugging stream.
312 std::ostream& debug_stream () const;
313 /// Set the current debugging stream.
314 void set_debug_stream (std::ostream &);
315
316 /// Type for debugging levels.
317 typedef int debug_level_type;
318 /// The current debugging level.
319 debug_level_type debug_level () const;
320 /// Set the current debugging level.
321 void set_debug_level (debug_level_type l);
322
323 private:
324
325 public:
326 /// Report a syntax error.
327 /// \param loc where the syntax error is found.
328 /// \param msg a description of the syntax error.
329 virtual void error (const location_type& loc, const std::string& msg);
330 private:
331
332 #if YYDEBUG
333 public:
334 /// \brief Report a symbol value on the debug stream.
335 /// \param yytype The token type.
336 /// \param yyvaluep Its semantic value.
337 /// \param yylocationp Its location.
338 virtual void yy_symbol_value_print_ (int yytype,
339 const semantic_type* yyvaluep,
340 const location_type* yylocationp);
341 /// \brief Report a symbol on the debug stream.
342 /// \param yytype The token type.
343 /// \param yyvaluep Its semantic value.
344 /// \param yylocationp Its location.
345 virtual void yy_symbol_print_ (int yytype,
346 const semantic_type* yyvaluep,
347 const location_type* yylocationp);
348 private:
349 /* Debugging. */
350 int yydebug_;
351 std::ostream* yycdebug_;
352 #endif
353
354
355 /// \brief Reclaim the memory associated to a symbol.
356 /// \param yymsg Why this token is reclaimed.
357 /// \param yytype The symbol type.
358 /// \param yyvaluep Its semantic value.
359 /// \param yylocationp Its location.
360 inline void yydestruct_ (const char* yymsg,
361 int yytype,
362 semantic_type* yyvaluep,
363 location_type* yylocationp);
364
365 ]b4_parse_param_vars[
366 };
367
368 ]dnl Redirections for glr.c.
369 b4_percent_define_flag_if([[global_tokens_and_yystype]],
370 [b4_token_defines(b4_tokens)])
371 [
372 #ifndef YYSTYPE
373 # define YYSTYPE ]b4_namespace[::]b4_parser_class_name[::semantic_type
374 #endif
375 #ifndef YYLTYPE
376 # define YYLTYPE ]b4_namespace[::]b4_parser_class_name[::location_type
377 #endif
378
379 }
380
381 ]b4_percent_code_get([[provides]])[]dnl
382
383 [#endif /* ! defined PARSER_HEADER_H */]
384 m4_divert_pop(0)