]> git.saurik.com Git - bison.git/blame - data/glr.cc
* src/Makefile.am (yacc): Quote target action commands properly so
[bison.git] / data / glr.cc
CommitLineData
08af01c2 1 -*- C -*-
42249483
AD
2
3# C++ GLR skeleton for Bison
279cabb6
JD
4# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation,
5# Inc.
42249483
AD
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
8f7e3cf9
AD
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
42249483 50# We require a pure interface using locations.
34376418
AD
51m4_define([b4_locations_flag], [1])
52m4_define([b4_pure_flag], [1])
53
54# The header is mandatory.
55b4_defines_if([],
5f340b48 56 [b4_fatal([b4_skeleton[: using %%defines is mandatory]])])
42249483
AD
57
58m4_include(b4_pkgdatadir/[c++.m4])
59m4_include(b4_pkgdatadir/[location.cc])
60
6afc30cc 61m4_define([b4_parser_class_name],
a4e25e1d 62 [b4_percent_define_get([[parser_class_name]])])
6afc30cc 63m4_define([b4_namespace],
a4e25e1d 64 [b4_percent_define_get([[namespace]])])
42249483 65
8f7e3cf9
AD
66# Save the parse parameters.
67m4_define([b4_parse_param_orig], m4_defn([b4_parse_param]))
68
69
c4d497a0
AD
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.
74m4_define([b4_yy_symbol_print_generate],
8f7e3cf9 75[[
c4d497a0
AD
76/*--------------------.
77| Print this symbol. |
78`--------------------*/
42249483 79
c4d497a0 80]b4_c_ansi_function_def([yy_symbol_print],
42249483 81 [static void],
c4d497a0 82 [[FILE *], []],
42249483 83 [[int yytype], [yytype]],
aa08666d 84 [[const b4_namespace::b4_parser_class_name::semantic_type *yyvaluep],
02650b7f 85 [yyvaluep]],
aa08666d 86 [[const b4_namespace::b4_parser_class_name::location_type *yylocationp],
02650b7f 87 [yylocationp]],
8f7e3cf9 88 b4_parse_param)[
42249483 89{
613d8952 90]b4_parse_param_use[]dnl
327afc7c 91[ yyparser.yy_symbol_print_ (yytype, yyvaluep]b4_locations_if([, yylocationp])[);
42249483
AD
92}
93]])
94
8f7e3cf9
AD
95
96# Declare yyerror.
97m4_append([b4_post_prologue],
8ec0a172 98[b4_syncline([@oline@], [@ofile@])
8f7e3cf9
AD
99
100b4_c_ansi_function_decl([yyerror],
101 [static void],
aa08666d 102 [[b4_namespace::b4_parser_class_name::location_type *yylocationp], [yylocationp]],
8f7e3cf9
AD
103 b4_parse_param,
104 [[const char* msg], [msg]])])
105
106
107# Define yyerror.
108m4_append([b4_epilogue],
8ec0a172 109[b4_syncline([@oline@], [@ofile@])[
42249483
AD
110/*------------------.
111| Report an error. |
112`------------------*/
113
114]b4_c_ansi_function_def([yyerror],
115 [static void],
aa08666d 116 [[b4_namespace::b4_parser_class_name::location_type *yylocationp], [yylocationp]],
42249483
AD
117 b4_parse_param,
118 [[const char* msg], [msg]])[
119{
613d8952 120]b4_parse_param_use[]dnl
c4d497a0 121[ yyparser.error (*yylocationp, msg);
42249483
AD
122}
123
124
aa08666d 125namespace ]b4_namespace[
42249483 126{
8f7e3cf9
AD
127]dnl In this section, the parse param are the original parse_params.
128m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_orig]))dnl
129[ /// Build a parser object.
fa7b79c0
PE
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[
42249483
AD
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
c4d497a0
AD
150 /*--------------------.
151 | Print this symbol. |
152 `--------------------*/
42249483 153
c4d497a0
AD
154 inline void
155 ]b4_parser_class_name[::yy_symbol_value_print_ (int yytype,
02650b7f 156 const semantic_type* yyvaluep, const location_type* yylocationp)
42249483
AD
157 {
158 /* Pacify ``unused variable'' warnings. */
613d8952
AD
159 YYUSE (yyvaluep);
160 YYUSE (yylocationp);
42249483
AD
161 switch (yytype)
162 {
163 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
164[ default:
02650b7f 165 break;
42249483 166 }
c4d497a0
AD
167 }
168
169
170 void
171 ]b4_parser_class_name[::yy_symbol_print_ (int yytype,
02650b7f 172 const semantic_type* yyvaluep, const location_type* yylocationp)
c4d497a0
AD
173 {
174 *yycdebug_ << (yytype < YYNTOKENS ? "token" : "nterm")
02650b7f
PE
175 << ' ' << yytname[yytype] << " ("
176 << *yylocationp << ": ";
c4d497a0 177 yy_symbol_value_print_ (yytype, yyvaluep, yylocationp);
42249483
AD
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 {
fa7b79c0 197 return yydebug_;
42249483
AD
198 }
199
200 void
201 ]b4_parser_class_name[::set_debug_level (debug_level_type l)
202 {
fa7b79c0 203 yydebug_ = l;
42249483
AD
204 }
205
fa7b79c0 206#endif
8f7e3cf9 207]m4_popdef([b4_parse_param])dnl
aa08666d 208[} // namespace ]b4_namespace[
42249483
AD
209
210]])
211
8f7e3cf9
AD
212
213# Let glr.c believe that the user arguments include the parser itself.
214m4_ifset([b4_parse_param],
215[m4_pushdef([b4_parse_param],
aa08666d 216 m4_dquote([[[b4_namespace::b4_parser_class_name& yyparser], [[yyparser]]],]
8f7e3cf9
AD
217m4_defn([b4_parse_param])))],
218[m4_pushdef([b4_parse_param],
aa08666d 219 [[[[b4_namespace::b4_parser_class_name& yyparser], [[yyparser]]]]])
8f7e3cf9 220])
42249483 221m4_include(b4_pkgdatadir/[glr.c])
8f7e3cf9
AD
222m4_popdef([b4_parse_param])
223
08af01c2
JD
224m4_divert_push(0)
225@output(b4_spec_defines_file@)
193d7c70 226b4_copyright([Skeleton interface for Bison GLR parsers in C++],
6e93d810 227 [2002, 2003, 2004, 2005, 2006])[
af3412cd 228
af3412cd
PE
229/* C++ GLR parser skeleton written by Akim Demaille. */
230
42249483
AD
231#ifndef PARSER_HEADER_H
232# define PARSER_HEADER_H
233
a4e25e1d 234]b4_percent_code_get([[requires]])[
aef3da86 235
42249483
AD
236#include <string>
237#include <iostream>
238
239/* Using locations. */
240#define YYLSP_NEEDED ]b4_locations_flag[
241
aa08666d 242namespace ]b4_namespace[
42249483
AD
243{
244 class position;
245 class location;
246}
247
42249483
AD
248#include "location.hh"
249
250/* Enabling traces. */
251#ifndef YYDEBUG
327afc7c 252# define YYDEBUG ]b4_debug_flag[
42249483
AD
253#endif
254
42249483
AD
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 } \
c4d497a0 271 while (/*CONSTCOND*/ 0)
42249483
AD
272#endif
273
aa08666d 274namespace ]b4_namespace[
42249483
AD
275{
276 /// A Bison parser.
277 class ]b4_parser_class_name[
278 {
279 public:
280 /// Symbol semantic values.
02650b7f 281#ifndef YYSTYPE
42249483 282]m4_ifdef([b4_stype],
1221b78a 283[ union semantic_type
7ecec4dd 284 {
8ec0a172 285b4_user_stype
7ecec4dd 286 };],
ddc8ede1
PE
287[m4_if(b4_tag_seen_flag, 0,
288[[ typedef int semantic_type;]],
289[[ typedef YYSTYPE semantic_type;]])])[
42249483
AD
290#else
291 typedef YYSTYPE semantic_type;
292#endif
293 /// Symbol locations.
a4e25e1d 294 typedef ]b4_percent_define_get([[location_type]])[ location_type;
42249483
AD
295 /// Tokens.
296 struct token
297 {
298 ]b4_token_enums(b4_tokens)[
299 };
c095d689
AD
300 /// Token type.
301 typedef token::yytokentype token_type;
42249483
AD
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:
c4d497a0
AD
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,
02650b7f
PE
339 const semantic_type* yyvaluep,
340 const location_type* yylocationp);
42249483
AD
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.
c4d497a0 345 virtual void yy_symbol_print_ (int yytype,
02650b7f
PE
346 const semantic_type* yyvaluep,
347 const location_type* yylocationp);
42249483 348 private:
fa7b79c0
PE
349 /* Debugging. */
350 int yydebug_;
351 std::ostream* yycdebug_;
352#endif
42249483
AD
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,
02650b7f
PE
361 int yytype,
362 semantic_type* yyvaluep,
363 location_type* yylocationp);
42249483 364
42249483
AD
365]b4_parse_param_vars[
366 };
367
368]dnl Redirections for glr.c.
592d0b1e 369b4_percent_define_flag_if([[global_tokens_and_yystype]],
8f7e3cf9
AD
370[b4_token_defines(b4_tokens)])
371[
372#ifndef YYSTYPE
aa08666d 373# define YYSTYPE ]b4_namespace[::]b4_parser_class_name[::semantic_type
8f7e3cf9
AD
374#endif
375#ifndef YYLTYPE
aa08666d 376# define YYLTYPE ]b4_namespace[::]b4_parser_class_name[::location_type
8f7e3cf9 377#endif
42249483
AD
378
379}
380
a4e25e1d 381]b4_percent_code_get([[provides]])[]dnl
9bc0dd67
JD
382
383[#endif /* ! defined PARSER_HEADER_H */]
08af01c2 384m4_divert_pop(0)