]> git.saurik.com Git - bison.git/blob - data/glr.cc
* data/glr.cc: New.
[bison.git] / data / glr.cc
1 m4_divert(-1) -*- C -*-
2
3 # C++ GLR skeleton for Bison
4 # Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301 USA
20
21 # We require a pure interface using locations.
22 m4_define([b4_location_flag], [1])
23 m4_define([b4_pure], [1])
24
25 m4_include(b4_pkgdatadir/[c++.m4])
26 m4_include(b4_pkgdatadir/[location.cc])
27
28
29 # b4_yysymprint_generate(FUNCTION-DECLARATOR)
30 # -------------------------------------------
31 # Generate the "yysymprint" function, which declaration is issued using
32 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
33 # or "b4_c_function_def" for K&R.
34 m4_define([b4_yysymprint_generate],
35 [b4_c_ansi_function_decl([yyerror],
36 [static void],
37 [[yy::b4_parser_class_name::location_type *yylocationp], [yylocationp]],
38 b4_parse_param,
39 [[const char* msg], [msg]])[
40
41 /*--------------------------------.
42 | Print this symbol on YYOUTPUT. |
43 `--------------------------------*/
44
45 ]$1([yysymprint],
46 [static void],
47 [[FILE *yyoutput], [yyoutput]],
48 [[int yytype], [yytype]],
49 [[const yy::b4_parser_class_name::semantic_type *yyvaluep],
50 [yyvaluep]][]dnl
51 b4_location_if([,
52 [[const yy::b4_parser_class_name::location_type *yylocationp],
53 [yylocationp]]])[]dnl
54 m4_ifset([b4_parse_param], [, b4_parse_param]))[
55 {
56 (void) yyoutput;
57 yyparser.yysymprint_ (yytype, yyvaluep]b4_location_if([, yylocationp])[);
58 }
59 ]])
60
61 m4_prepend([b4_epilogue],
62 [[
63 /*------------------.
64 | Report an error. |
65 `------------------*/
66
67 ]b4_c_ansi_function_def([yyerror],
68 [static void],
69 [[yy::b4_parser_class_name::location_type *yylocationp], [yylocationp]],
70 b4_parse_param,
71 [[const char* msg], [msg]])[
72 {
73 yyparser.error (*yylocationp, msg);
74 }
75
76
77 namespace yy
78 {
79 ]dnl Restore the actual parser params.
80 m4_popdef([b4_parse_param])dnl
81 [
82 /// Build a parser object.
83 ]b4_parser_class_name::b4_parser_class_name[ (]b4_parse_param_decl[)
84 : yycdebug_ (&std::cerr)]b4_parse_param_cons[
85 {
86 }
87
88 ]b4_parser_class_name::~b4_parser_class_name[ ()
89 {
90 }
91
92 int
93 ]b4_parser_class_name[::parse ()
94 {
95 return ::yyparse (*this]b4_user_args[);
96 }
97
98 #if YYDEBUG
99
100 /*--------------------------------.
101 | Print this symbol on YYOUTPUT. |
102 `--------------------------------*/
103
104 void
105 ]b4_parser_class_name[::yysymprint_ (int yytype,
106 const semantic_type* yyvaluep, const location_type* yylocationp)
107 {
108 /* Pacify ``unused variable'' warnings. */
109 (void) yyvaluep;
110 (void) yylocationp;
111 /* Backward compatibility, but should be removed eventually. */
112 std::ostream& cdebug_ = *yycdebug_;
113 (void) cdebug_;
114
115 *yycdebug_ << (yytype < YYNTOKENS ? "token" : "nterm")
116 << ' ' << yytname[yytype] << " ("
117 << *yylocationp << ": ";
118 switch (yytype)
119 {
120 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
121 [ default:
122 break;
123 }
124 *yycdebug_ << ')';
125 }
126
127 std::ostream&
128 ]b4_parser_class_name[::debug_stream () const
129 {
130 return *yycdebug_;
131 }
132
133 void
134 ]b4_parser_class_name[::set_debug_stream (std::ostream& o)
135 {
136 yycdebug_ = &o;
137 }
138
139
140 ]b4_parser_class_name[::debug_level_type
141 ]b4_parser_class_name[::debug_level () const
142 {
143 return ::yydebug;
144 }
145
146 void
147 ]b4_parser_class_name[::set_debug_level (debug_level_type l)
148 {
149 ::yydebug = l;
150 }
151
152 #endif /* ! YYDEBUG */
153
154 } // namespace yy
155
156 ]])
157
158 # Let glr.c believe that the user arguments are only the parser itself.
159 m4_pushdef([b4_parse_param],
160 [[yy::b4_parser_class_name& yyparser, yyparser],]
161 m4_defn([b4_parse_param]))
162 m4_include(b4_pkgdatadir/[glr.c])
163 @output @output_header_name@
164 b4_copyright([C++ Skeleton parser for GLALR(1) parsing with Bison],
165 [2002, 2003, 2004, 2005])[
166 #ifndef PARSER_HEADER_H
167 # define PARSER_HEADER_H
168
169 #include <string>
170 #include <iostream>
171
172 /* Using locations. */
173 #define YYLSP_NEEDED ]b4_locations_flag[
174
175 namespace yy
176 {
177 class position;
178 class location;
179 }
180
181 /* Copy the first part of user declarations. */
182 ]b4_pre_prologue[
183
184 ]/* Line __line__ of glr.cc. */
185 b4_syncline([@oline@], [@ofile@])[
186
187 #include "stack.hh"
188 #include "location.hh"
189
190 /* Enabling traces. */
191 #ifndef YYDEBUG
192 # define YYDEBUG ]b4_debug[
193 #endif
194
195 /* Enabling verbose error messages. */
196 #ifdef YYERROR_VERBOSE
197 # undef YYERROR_VERBOSE
198 # define YYERROR_VERBOSE 1
199 #else
200 # define YYERROR_VERBOSE ]b4_error_verbose[
201 #endif
202
203 /* Enabling the token table. */
204 #ifndef YYTOKEN_TABLE
205 # define YYTOKEN_TABLE ]b4_token_table[
206 #endif
207
208 #if 0
209 /* Copy the second part of user declarations. */
210 ]b4_post_prologue[
211
212 ]/* Line __line__ of glr.cc. */
213
214 b4_syncline([@oline@], [@ofile@])[
215 #endif
216
217 /* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
218 If N is 0, then set CURRENT to the empty location which ends
219 the previous symbol: RHS[0] (always defined). */
220
221 #ifndef YYLLOC_DEFAULT
222 # define YYLLOC_DEFAULT(Current, Rhs, N) \
223 do \
224 if (N) \
225 { \
226 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
227 (Current).end = YYRHSLOC (Rhs, N).end; \
228 } \
229 else \
230 { \
231 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
232 } \
233 while (/*CONSTCOND*/ 0)
234 #endif
235
236 namespace yy
237 {
238 /// A Bison parser.
239 class ]b4_parser_class_name[
240 {
241 public:
242 /// Symbol semantic values.
243 #if ! defined (YYSTYPE)
244 ]m4_ifdef([b4_stype],
245 [b4_syncline([b4_stype_line], [b4_file_name])
246 union semantic_type b4_stype;
247 /* Line __line__ of lalr1.cc. */
248 b4_syncline([@oline@], [@ofile@])],
249 [ typedef int semantic_type;])[
250 #else
251 typedef YYSTYPE semantic_type;
252 #endif
253 /// Symbol locations.
254 typedef ]b4_location_type[ location_type;
255 /// Tokens.
256 struct token
257 {
258 ]b4_token_enums(b4_tokens)[
259 };
260
261 /// Build a parser object.
262 ]b4_parser_class_name[ (]b4_parse_param_decl[);
263 virtual ~]b4_parser_class_name[ ();
264
265 /// Parse.
266 /// \returns 0 iff parsing succeeded.
267 virtual int parse ();
268
269 /// The current debugging stream.
270 std::ostream& debug_stream () const;
271 /// Set the current debugging stream.
272 void set_debug_stream (std::ostream &);
273
274 /// Type for debugging levels.
275 typedef int debug_level_type;
276 /// The current debugging level.
277 debug_level_type debug_level () const;
278 /// Set the current debugging level.
279 void set_debug_level (debug_level_type l);
280
281 private:
282
283 public:
284 /// Report a syntax error.
285 /// \param loc where the syntax error is found.
286 /// \param msg a description of the syntax error.
287 virtual void error (const location_type& loc, const std::string& msg);
288 private:
289
290 #if YYDEBUG
291 public:
292 /// \brief Report a symbol on the debug stream.
293 /// \param yytype The token type.
294 /// \param yyvaluep Its semantic value.
295 /// \param yylocationp Its location.
296 virtual void yysymprint_ (int yytype,
297 const semantic_type* yyvaluep,
298 const location_type* yylocationp);
299 private:
300 #endif /* ! YYDEBUG */
301
302
303 /// \brief Reclaim the memory associated to a symbol.
304 /// \param yymsg Why this token is reclaimed.
305 /// \param yytype The symbol type.
306 /// \param yyvaluep Its semantic value.
307 /// \param yylocationp Its location.
308 inline void yydestruct_ (const char* yymsg,
309 int yytype,
310 semantic_type* yyvaluep,
311 location_type* yylocationp);
312
313 /* Debugging. */
314 std::ostream* yycdebug_;
315 ]b4_parse_param_vars[
316 };
317
318 ]dnl Redirections for glr.c.
319 [#define YYSTYPE yy::]b4_parser_class_name[::semantic_type
320 #define YYLTYPE yy::]b4_parser_class_name[::location_type
321
322 }
323
324 #endif /* ! defined PARSER_HEADER_H */]