3 # C++ GLR skeleton for Bison
4 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation,
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.
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.
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
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.
30 # The passing of parse-params
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.
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
50 # We require a pure interface using locations.
51 m4_define([b4_locations_flag
], [1])
52 m4_define([b4_pure_flag
], [1])
54 # The header is mandatory.
56 [b4_fatal([b4_skeleton
[: using %%defines is mandatory
]])])
58 m4_include(b4_pkgdatadir
/[c
++.m4
])
59 m4_include(b4_pkgdatadir
/[location
.cc
])
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]])])
66 # Save the parse parameters.
67 m4_define([b4_parse_param_orig
], m4_defn([b4_parse_param
]))
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
],
76 /*--------------------.
77 | Print this symbol. |
78 `--------------------*/
80 ]b4_c_ansi_function_def([yy_symbol_print
],
83 [[int yytype
], [yytype
]],
84 [[const b4_namespace::b4_parser_class_name::semantic_type
*yyvaluep
],
86 [[const b4_namespace::b4_parser_class_name::location_type
*yylocationp
],
90 ]b4_parse_param_use
[]dnl
91 [ yyparser
.yy_symbol_print_ (yytype
, yyvaluep
]b4_locations_if([, yylocationp
])[);
97 m4_append([b4_post_prologue
],
98 [b4_syncline([@oline@
], [@ofile@
])
100 b4_c_ansi_function_decl([yyerror
],
102 [[b4_namespace::b4_parser_class_name::location_type
*yylocationp
], [yylocationp
]],
104 [[const char* msg
], [msg
]])])
108 m4_append([b4_epilogue
],
109 [b4_syncline([@oline@
], [@ofile@
])[
110 /*------------------.
112 `------------------*/
114 ]b4_c_ansi_function_def([yyerror
],
116 [[b4_namespace::b4_parser_class_name::location_type
*yylocationp
], [yylocationp
]],
118 [[const char* msg
], [msg
]])[
120 ]b4_parse_param_use
[]dnl
121 [ yyparser
.error (*yylocationp
, msg
);
125 namespace ]b4_namespace
[
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
], [
133 ]m4_ifset([b4_parse_param
], [ ], [ :])[yydebug_ (false),
134 yycdebug_ (&std::cerr
)]m4_ifset([b4_parse_param
], [,])[
135 #endif]b4_parse_param_cons[
139 ]b4_parser_class_name::~b4_parser_class_name
[ ()
144 ]b4_parser_class_name
[::parse ()
146 return ::yyparse (*this]b4_user_args
[);
150 /*--------------------.
151 | Print this symbol. |
152 `--------------------*/
155 ]b4_parser_class_name
[::yy_symbol_value_print_ (int yytype
,
156 const semantic_type
* yyvaluep
, const location_type
* yylocationp
)
158 /* Pacify ``unused variable'' warnings. */
163 ]m4_map([b4_symbol_actions
], m4_defn([b4_symbol_printers
]))dnl
171 ]b4_parser_class_name
[::yy_symbol_print_ (int yytype
,
172 const semantic_type
* yyvaluep
, const location_type
* yylocationp
)
174 *yycdebug_
<< (yytype
< YYNTOKENS
? "token" : "nterm")
175 << ' ' << yytname
[yytype
] << " ("
176 << *yylocationp
<< ": ";
177 yy_symbol_value_print_ (yytype
, yyvaluep
, yylocationp
);
182 ]b4_parser_class_name
[::debug_stream () const
188 ]b4_parser_class_name
[::set_debug_stream (std::ostream
& o
)
194 ]b4_parser_class_name
[::debug_level_type
195 ]b4_parser_class_name
[::debug_level () const
201 ]b4_parser_class_name
[::set_debug_level (debug_level_type l
)
207 ]m4_popdef([b4_parse_param
])dnl
208 [} // namespace ]b4_namespace[
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]]]]])
221 m4_include(b4_pkgdatadir
/[glr
.c
])
222 m4_popdef([b4_parse_param
])
225 @
output(b4_spec_defines_file@
)
226 b4_copyright([Skeleton interface
for Bison GLR parsers in C
++],
227 [2002, 2003, 2004, 2005, 2006])[
229 /* C++ GLR parser skeleton written by Akim Demaille. */
231 #ifndef PARSER_HEADER_H
232 # define PARSER_HEADER_H
234 ]b4_percent_code_get([[requires]])[
239 /* Using locations. */
240 #define YYLSP_NEEDED ]b4_locations_flag[
242 namespace ]b4_namespace
[
248 #include "location.hh"
250 /* Enabling traces. */
252 # define YYDEBUG ]b4_debug_flag[
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). */
259 #ifndef YYLLOC_DEFAULT
260 # define YYLLOC_DEFAULT(Current, Rhs, N) \
264 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
265 (Current).end = YYRHSLOC (Rhs, N).end; \
269 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
271 while (/*CONSTCOND*/ 0)
274 namespace ]b4_namespace
[
277 class ]b4_parser_class_name
[
280 /// Symbol semantic values.
282 ]m4_ifdef([b4_stype
],
283 [ union semantic_type
287 [m4_if(b4_tag_seen_flag
, 0,
288 [[ typedef int semantic_type
;]],
289 [[ typedef YYSTYPE semantic_type
;]])])[
291 typedef YYSTYPE semantic_type
;
293 /// Symbol locations.
294 typedef ]b4_percent_define_get([[location_type]])[ location_type
;
298 ]b4_token_enums(b4_tokens
)[
301 typedef token::yytokentype token_type
;
303 /// Build a parser object.
304 ]b4_parser_class_name
[ (]b4_parse_param_decl
[);
305 virtual ~]b4_parser_class_name
[ ();
308 /// \returns 0 iff parsing succeeded.
309 virtual int parse ();
311 /// The current debugging stream.
312 std::ostream
& debug_stream () const;
313 /// Set the current debugging stream.
314 void set_debug_stream (std::ostream
&);
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
);
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
);
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
);
351 std::ostream
* yycdebug_
;
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
,
362 semantic_type
* yyvaluep
,
363 location_type
* yylocationp
);
365 ]b4_parse_param_vars
[
368 ]dnl Redirections
for glr
.c
.
369 b4_percent_define_flag_if([[global_tokens_and_yystype]],
370 [b4_token_defines(b4_tokens
)])
373 # define YYSTYPE ]b4_namespace[::]b4_parser_class_name[::semantic_type
376 # define YYLTYPE ]b4_namespace[::]b4_parser_class_name[::location_type
381 ]b4_percent_code_get([[provides]])[]dnl
383 [#endif /* ! defined PARSER_HEADER_H */]