]> git.saurik.com Git - bison.git/blob - data/glr.cc
85aca842d8ef3d3d8e30cf433f8d5489638fb459
[bison.git] / data / glr.cc
1 # C++ GLR skeleton for Bison
2
3 # Copyright (C) 2002-2012 Free Software Foundation, Inc.
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18
19 # This skeleton produces a C++ class that encapsulates a C glr parser.
20 # This is in order to reduce the maintenance burden. The glr.c
21 # skeleton is clean and pure enough so that there are no real
22 # problems. The C++ interface is the same as that of lalr1.cc. In
23 # fact, glr.c can replace yacc.c without the user noticing any
24 # difference, and similarly for glr.cc replacing lalr1.cc.
25 #
26 # The passing of parse-params
27 #
28 # The additional arguments are stored as members of the parser
29 # object, yyparser. The C routines need to carry yyparser
30 # throughout the C parser; that's easy: make yyparser an
31 # additional parse-param. But because the C++ skeleton needs to
32 # know the "real" original parse-param, we save them
33 # (b4_parse_param_orig). Note that b4_parse_param is overquoted
34 # (and c.m4 strips one level of quotes). This is a PITA, and
35 # explains why there are so many levels of quotes.
36 #
37 # The locations
38 #
39 # We use location.cc just like lalr1.cc, but because glr.c stores
40 # the locations in a union, the position and location classes
41 # must not have a constructor. Therefore, contrary to lalr1.cc, we
42 # must not define "b4_location_constructors". As a consequence the
43 # user must initialize the first positions (in particular the
44 # filename member).
45
46 # We require a pure interface.
47 m4_define([b4_pure_flag], [1])
48
49 m4_include(b4_pkgdatadir/[c++.m4])
50 b4_bison_locations_if([m4_include(b4_pkgdatadir/[location.cc])])
51
52 m4_define([b4_parser_class_name],
53 [b4_percent_define_get([[parser_class_name]])])
54
55 # Save the parse parameters.
56 m4_define([b4_parse_param_orig], m4_defn([b4_parse_param]))
57
58 # b4_parse_param_wrap
59 # -------------------
60 # New ones.
61 m4_ifset([b4_parse_param],
62 [m4_define([b4_parse_param_wrap],
63 [[b4_namespace_ref::b4_parser_class_name[& yyparser], [[yyparser]]],]
64 m4_defn([b4_parse_param]))],
65 [m4_define([b4_parse_param_wrap],
66 [[b4_namespace_ref::b4_parser_class_name[& yyparser], [[yyparser]]]])
67 ])
68
69
70 # b4_yy_symbol_print_define
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_define],
75 [[
76 /*--------------------.
77 | Print this symbol. |
78 `--------------------*/
79
80 ]b4_function_define([yy_symbol_print],
81 [static void],
82 [[FILE *], []],
83 [[int yytype], [yytype]],
84 [[const ]b4_namespace_ref::b4_parser_class_name[::semantic_type *yyvaluep],
85 [yyvaluep]][]dnl
86 b4_locations_if([,
87 [[const ]b4_namespace_ref::b4_parser_class_name[::location_type *yylocationp],
88 [yylocationp]]]),
89 b4_parse_param)[
90 {
91 ]b4_parse_param_use[]dnl
92 [ yyparser.yy_symbol_print_ (yytype, yyvaluep]b4_locations_if([, yylocationp])[);
93 }
94 ]])[
95
96 # Hijack the initial action to initialize the locations.
97 ]b4_bison_locations_if([m4_define([b4_initial_action],
98 [yylloc.initialize ();]m4_ifdef([b4_initial_action], [
99 m4_defn([b4_initial_action])]))])[
100
101 # Hijack the post prologue to insert early definition of YYLLOC_DEFAULT
102 # and declaration of yyerror.
103 ]m4_append([b4_post_prologue],
104 [b4_syncline([@oline@], [@ofile@])[
105 ]b4_yylloc_default_define[
106 #define YYRHSLOC(Rhs, K) ((Rhs)[K].yystate.yyloc)
107 ]b4_function_declare([yyerror],
108 [static void],b4_locations_if([
109 [[const ]b4_namespace_ref::b4_parser_class_name[::location_type *yylocationp],
110 [yylocationp]],])
111 b4_parse_param,
112 [[const char* msg], [msg]])])
113
114
115 # Hijack the epilogue to define implementations (yyerror, parser member
116 # functions etc.).
117 m4_append([b4_epilogue],
118 [b4_syncline([@oline@], [@ofile@])[
119 /*------------------.
120 | Report an error. |
121 `------------------*/
122
123 ]b4_function_define([yyerror],
124 [static void],b4_locations_if([
125 [[const ]b4_namespace_ref::b4_parser_class_name[::location_type *yylocationp],
126 [yylocationp]],])
127 b4_parse_param,
128 [[const char* msg], [msg]])[
129 {
130 ]b4_parse_param_use[]dnl
131 [ yyparser.error (]b4_locations_if([[*yylocationp, ]])[msg);
132 }
133
134
135 ]b4_namespace_open[
136 ]dnl In this section, the parse params are the original parse_params.
137 m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_orig]))dnl
138 [ /// Build a parser object.
139 ]b4_parser_class_name::b4_parser_class_name[ (]b4_parse_param_decl[)]m4_ifset([b4_parse_param], [
140 :])[
141 #if ]b4_api_PREFIX[DEBUG
142 ]m4_ifset([b4_parse_param], [ ], [ :])[yycdebug_ (&std::cerr)]m4_ifset([b4_parse_param], [,])[
143 #endif]b4_parse_param_cons[
144 {
145 }
146
147 ]b4_parser_class_name::~b4_parser_class_name[ ()
148 {
149 }
150
151 int
152 ]b4_parser_class_name[::parse ()
153 {
154 return ::yyparse (*this]b4_user_args[);
155 }
156
157 #if ]b4_api_PREFIX[DEBUG
158 /*--------------------.
159 | Print this symbol. |
160 `--------------------*/
161
162 inline void
163 ]b4_parser_class_name[::yy_symbol_value_print_ (int yytype,
164 const semantic_type* yyvaluep]b4_locations_if([[,
165 const location_type* yylocationp]])[)
166 {]b4_locations_if([[
167 YYUSE (yylocationp);]])[
168 YYUSE (yyvaluep);
169 std::ostream& yyoutput = debug_stream ();
170 std::ostream& yyo = yyoutput;
171 YYUSE (yyo);
172 switch (yytype)
173 {
174 ]b4_symbol_foreach([b4_symbol_printer])dnl
175 [ default:
176 break;
177 }
178 }
179
180
181 void
182 ]b4_parser_class_name[::yy_symbol_print_ (int yytype,
183 const semantic_type* yyvaluep]b4_locations_if([[,
184 const location_type* yylocationp]])[)
185 {
186 *yycdebug_ << (yytype < YYNTOKENS ? "token" : "nterm")
187 << ' ' << yytname[yytype] << " ("]b4_locations_if([[
188 << *yylocationp << ": "]])[;
189 yy_symbol_value_print_ (yytype, yyvaluep]b4_locations_if([[, yylocationp]])[);
190 *yycdebug_ << ')';
191 }
192
193 std::ostream&
194 ]b4_parser_class_name[::debug_stream () const
195 {
196 return *yycdebug_;
197 }
198
199 void
200 ]b4_parser_class_name[::set_debug_stream (std::ostream& o)
201 {
202 yycdebug_ = &o;
203 }
204
205
206 ]b4_parser_class_name[::debug_level_type
207 ]b4_parser_class_name[::debug_level () const
208 {
209 return yydebug;
210 }
211
212 void
213 ]b4_parser_class_name[::set_debug_level (debug_level_type l)
214 {
215 // Actually, it is yydebug which is really used.
216 yydebug = l;
217 }
218
219 #endif
220 ]m4_popdef([b4_parse_param])dnl
221 b4_namespace_close
222 ])
223
224 # b4_shared_declarations
225 # ----------------------
226 # Declaration that might either go into the header (if --defines)
227 # or open coded in the parser body.
228 m4_define([b4_shared_declarations],
229 [m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_orig]))dnl
230 b4_percent_code_get([[requires]])[
231
232 #include <stdexcept>
233 #include <string>
234 #include <iostream>]b4_defines_if([
235 b4_bison_locations_if([[#include "location.hh"]])])[
236
237 ]b4_YYDEBUG_define[
238
239 ]b4_namespace_open[
240 ]b4_defines_if([],
241 [b4_bison_locations_if([b4_position_define
242 b4_location_define])])[
243
244 /// A Bison parser.
245 class ]b4_parser_class_name[
246 {
247 public:
248 ]b4_public_types_declare[
249
250 /// Build a parser object.
251 ]b4_parser_class_name[ (]b4_parse_param_decl[);
252 virtual ~]b4_parser_class_name[ ();
253
254 /// Parse.
255 /// \returns 0 iff parsing succeeded.
256 virtual int parse ();
257
258 /// The current debugging stream.
259 std::ostream& debug_stream () const;
260 /// Set the current debugging stream.
261 void set_debug_stream (std::ostream &);
262
263 /// Type for debugging levels.
264 typedef int debug_level_type;
265 /// The current debugging level.
266 debug_level_type debug_level () const;
267 /// Set the current debugging level.
268 void set_debug_level (debug_level_type l);
269
270 public:
271 /// Report a syntax error.]b4_locations_if([[
272 /// \param loc where the syntax error is found.]])[
273 /// \param msg a description of the syntax error.
274 virtual void error (]b4_locations_if([[const location_type& loc, ]])[const std::string& msg);
275
276 # if ]b4_api_PREFIX[DEBUG
277 public:
278 /// \brief Report a symbol value on the debug stream.
279 /// \param yytype The token type.
280 /// \param yyvaluep Its semantic value.]b4_locations_if([[
281 /// \param yylocationp Its location.]])[
282 virtual void yy_symbol_value_print_ (int yytype,
283 const semantic_type* yyvaluep]b4_locations_if([[,
284 const location_type* yylocationp]])[);
285 /// \brief Report a symbol on the debug stream.
286 /// \param yytype The token type.
287 /// \param yyvaluep Its semantic value.]b4_locations_if([[
288 /// \param yylocationp Its location.]])[
289 virtual void yy_symbol_print_ (int yytype,
290 const semantic_type* yyvaluep]b4_locations_if([[,
291 const location_type* yylocationp]])[);
292 private:
293 /* Debugging. */
294 std::ostream* yycdebug_;
295 #endif
296
297 ]b4_parse_param_vars[
298 };
299
300 ]dnl Redirections for glr.c.
301 b4_percent_define_flag_if([[global_tokens_and_yystype]],
302 [b4_token_defines])
303 [
304 #ifndef ]b4_api_PREFIX[STYPE
305 # define ]b4_api_PREFIX[STYPE ]b4_namespace_ref[::]b4_parser_class_name[::semantic_type
306 #endif
307 #ifndef ]b4_api_PREFIX[LTYPE
308 # define ]b4_api_PREFIX[LTYPE ]b4_namespace_ref[::]b4_parser_class_name[::location_type
309 #endif
310
311 ]b4_namespace_close[
312 ]b4_percent_code_get([[provides]])[
313 ]m4_popdef([b4_parse_param])dnl
314 ])
315
316 b4_defines_if(
317 [b4_output_begin([b4_spec_defines_file])
318 b4_copyright([Skeleton interface for Bison GLR parsers in C++],
319 [2002-2012])[
320
321 /* C++ GLR parser skeleton written by Akim Demaille. */
322
323 ]b4_cpp_guard_open([b4_spec_defines_file])[
324 ]b4_shared_declarations[
325 ]b4_cpp_guard_close([b4_spec_defines_file])[
326 ]b4_output_end()])
327
328 # Let glr.c (and b4_shared_declarations) believe that the user
329 # arguments include the parser itself.
330 m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_wrap]))
331 m4_include(b4_pkgdatadir/[glr.c])
332 m4_popdef([b4_parse_param])