]> git.saurik.com Git - bison.git/blob - data/c.m4
* data/glr.c (yyuserMerge, yyreportAmbiguity, yyreportSyntaxError):
[bison.git] / data / c.m4
1 m4_divert(-1) -*- Autoconf -*-
2
3 # C M4 Macros for Bison.
4 # Copyright (C) 2002, 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
22 ## ---------------- ##
23 ## Identification. ##
24 ## ---------------- ##
25
26 # b4_copyright(TITLE, YEARS)
27 # --------------------------
28 m4_define([b4_copyright],
29 [/* A Bison parser, made by GNU Bison b4_version. */
30
31 /* $1,
32 Copyright (C) $2 Free Software Foundation, Inc.
33
34 This program is free software; you can redistribute it and/or modify
35 it under the terms of the GNU General Public License as published by
36 the Free Software Foundation; either version 2, or (at your option)
37 any later version.
38
39 This program is distributed in the hope that it will be useful,
40 but WITHOUT ANY WARRANTY; without even the implied warranty of
41 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 GNU General Public License for more details.
43
44 You should have received a copy of the GNU General Public License
45 along with this program; if not, write to the Free Software
46 Foundation, Inc., 51 Franklin Street, Fifth Floor,
47 Boston, MA 02110-1301, USA. */])
48
49
50 # b4_identification
51 # -----------------
52 m4_define([b4_identification],
53 [/* Identify Bison output. */
54 [#]define YYBISON 1
55
56 /* Bison version. */
57 [#]define YYBISON_VERSION "b4_version"
58
59 /* Skeleton name. */
60 [#]define YYSKELETON_NAME b4_skeleton
61
62 /* Pure parsers. */
63 [#]define YYPURE b4_pure
64
65 /* Using locations. */
66 [#]define YYLSP_NEEDED b4_locations_flag
67 ])
68
69
70
71 ## ---------------- ##
72 ## Default values. ##
73 ## ---------------- ##
74
75 m4_define_default([b4_epilogue], [])
76
77
78
79 ## ------------------------ ##
80 ## Pure/impure interfaces. ##
81 ## ------------------------ ##
82
83
84 # b4_user_args
85 # ------------
86 m4_define([b4_user_args],
87 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
88
89
90 # b4_parse_param
91 # --------------
92 # If defined, b4_parse_param arrives double quoted, but below we prefer
93 # it to be single quoted.
94 m4_define_default([b4_parse_param])
95 m4_define([b4_parse_param],
96 b4_parse_param))
97
98
99
100 ## ------------ ##
101 ## Data Types. ##
102 ## ------------ ##
103
104
105 # b4_ints_in(INT1, INT2, LOW, HIGH)
106 # ---------------------------------
107 # Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
108 m4_define([b4_ints_in],
109 [m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
110
111
112 # b4_int_type(MIN, MAX)
113 # ---------------------
114 # Return the smallest int type able to handle numbers ranging from
115 # MIN to MAX (included).
116 m4_define([b4_int_type],
117 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
118 b4_ints_in($@, [-128], [127]), [1], [signed char],
119
120 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
121 b4_ints_in($@, [-32768], [32767]), [1], [short int],
122
123 m4_eval([0 <= $1]), [1], [unsigned int],
124
125 [int])])
126
127
128 # b4_int_type_for(NAME)
129 # ---------------------
130 # Return the smallest int type able to handle numbers ranging from
131 # `NAME_min' to `NAME_max' (included).
132 m4_define([b4_int_type_for],
133 [b4_int_type($1_min, $1_max)])
134
135
136 ## ------------------ ##
137 ## Decoding options. ##
138 ## ------------------ ##
139
140
141 # b4_error_verbose_if(IF-TRUE, IF-FALSE)
142 # --------------------------------------
143 # Expand IF-TRUE, if errors are verbose, IF-FALSE otherwise.
144 m4_define([b4_error_verbose_if],
145 [m4_if(b4_error_verbose, [1],
146 [$1],
147 [$2])])
148
149
150 # b4_location_if(IF-TRUE, IF-FALSE)
151 # ---------------------------------
152 # Expand IF-TRUE, if locations are used, IF-FALSE otherwise.
153 m4_define([b4_location_if],
154 [m4_if(b4_locations_flag, [1],
155 [$1],
156 [$2])])
157
158
159 # b4_pure_if(IF-TRUE, IF-FALSE)
160 # -----------------------------
161 # Expand IF-TRUE, if %pure-parser, IF-FALSE otherwise.
162 m4_define([b4_pure_if],
163 [m4_if(b4_pure, [1],
164 [$1],
165 [$2])])
166
167
168
169 ## ------------------------- ##
170 ## Assigning token numbers. ##
171 ## ------------------------- ##
172
173 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
174 # -----------------------------------------
175 # Output the definition of this token as #define.
176 m4_define([b4_token_define],
177 [#define $1 $2
178 ])
179
180
181 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
182 # -------------------------------------------------------
183 # Output the definition of the tokens (if there are) as #defines.
184 m4_define([b4_token_defines],
185 [m4_if([$@], [[]], [],
186 [/* Tokens. */
187 m4_map([b4_token_define], [$@])])
188 ])
189
190
191 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
192 # ---------------------------------------
193 # Output the definition of this token as an enum.
194 m4_define([b4_token_enum],
195 [$1 = $2])
196
197
198 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
199 # -----------------------------------------------------
200 # Output the definition of the tokens (if there are) as enums.
201 m4_define([b4_token_enums],
202 [m4_if([$@], [[]], [],
203 [/* Tokens. */
204 #ifndef YYTOKENTYPE
205 # define YYTOKENTYPE
206 /* Put the tokens into the symbol table, so that GDB and other debuggers
207 know about them. */
208 enum yytokentype {
209 m4_map_sep([ b4_token_enum], [,
210 ],
211 [$@])
212 };
213 #endif
214 ])])
215
216
217 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
218 # -------------------------------------------------------------
219 # Output the definition of the tokens (if there are) as enums and #defines.
220 m4_define([b4_token_enums_defines],
221 [b4_token_enums($@)b4_token_defines($@)
222 ])
223
224
225
226 ## --------------------------------------------- ##
227 ## Defining C functions in both K&R and ANSI-C. ##
228 ## --------------------------------------------- ##
229
230
231 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
232 # ----------------------------------------------------------
233 # Declare the function NAME.
234 m4_define([b4_c_function_def],
235 [#if defined (__STDC__) || defined (__cplusplus)
236 b4_c_ansi_function_def($@)
237 #else
238 $2
239 $1 (b4_c_knr_formal_names(m4_shiftn(2, $@)))
240 b4_c_knr_formal_decls(m4_shiftn(2, $@))
241 #endif[]dnl
242 ])
243
244
245 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
246 # ---------------------------------------------------------------
247 # Declare the function NAME in ANSI.
248 m4_define([b4_c_ansi_function_def],
249 [$2
250 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)))[]dnl
251 ])
252
253
254 # b4_c_ansi_formals([DECL1, NAME1], ...)
255 # --------------------------------------
256 # Output the arguments ANSI-C definition.
257 m4_define([b4_c_ansi_formals],
258 [m4_case([$@],
259 [], [void],
260 [[]], [void],
261 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
262
263 m4_define([b4_c_ansi_formal],
264 [$1])
265
266
267 # b4_c_knr_formal_names([DECL1, NAME1], ...)
268 # ------------------------------------------
269 # Output the argument names.
270 m4_define([b4_c_knr_formal_names],
271 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
272
273 m4_define([b4_c_knr_formal_name],
274 [$2])
275
276
277 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
278 # ------------------------------------------
279 # Output the K&R argument declarations.
280 m4_define([b4_c_knr_formal_decls],
281 [m4_map_sep([b4_c_knr_formal_decl],
282 [
283 ],
284 [$@])])
285
286 m4_define([b4_c_knr_formal_decl],
287 [ $1;])
288
289
290
291 ## ------------------------------------------------------------ ##
292 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
293 ## ------------------------------------------------------------ ##
294
295
296 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
297 # -----------------------------------------------------------
298 # Declare the function NAME.
299 m4_define([b4_c_function_decl],
300 [#if defined (__STDC__) || defined (__cplusplus)
301 b4_c_ansi_function_decl($@)
302 #else
303 $2 $1 ();
304 #endif[]dnl
305 ])
306
307
308 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
309 # ----------------------------------------------------------------
310 # Declare the function NAME.
311 m4_define([b4_c_ansi_function_decl],
312 [$2 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)));[]dnl
313 ])
314
315
316
317
318 ## --------------------- ##
319 ## Calling C functions. ##
320 ## --------------------- ##
321
322
323 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
324 # -----------------------------------------------------------
325 # Call the function NAME with arguments NAME1, NAME2 etc.
326 m4_define([b4_c_function_call],
327 [$1 (b4_c_args(m4_shiftn(2, $@)))[]dnl
328 ])
329
330
331 # b4_c_args([DECL1, NAME1], ...)
332 # ------------------------------
333 # Output the arguments NAME1, NAME2...
334 m4_define([b4_c_args],
335 [m4_map_sep([b4_c_arg], [, ], [$@])])
336
337 m4_define([b4_c_arg],
338 [$2])
339
340
341 ## ----------- ##
342 ## Synclines. ##
343 ## ----------- ##
344
345
346 # b4_syncline(LINE, FILE)
347 # -----------------------
348 m4_define([b4_syncline],
349 [m4_if(b4_synclines_flag, 1,
350 [[#]line $1 $2])])
351
352
353 # b4_symbol_actions(FILENAME, LINENO,
354 # SYMBOL-TAG, SYMBOL-NUM,
355 # SYMBOL-ACTION, SYMBOL-TYPENAME)
356 # -------------------------------------------------
357 m4_define([b4_symbol_actions],
358 [m4_pushdef([b4_dollar_dollar],
359 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
360 m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
361 case $4: /* $3 */
362 b4_syncline([$2], [$1])
363 $5;
364 b4_syncline([@oline@], [@ofile@])
365 break;
366 m4_popdef([b4_at_dollar])dnl
367 m4_popdef([b4_dollar_dollar])dnl
368 ])
369
370
371 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
372 # -------------------------------------------
373 # Generate the "yydestruct" function, which declaration is issued using
374 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
375 # or "b4_c_function_def" for K&R.
376 m4_define_default([b4_yydestruct_generate],
377 [[/*-----------------------------------------------.
378 | Release the memory associated to this symbol. |
379 `-----------------------------------------------*/
380
381 ]$1([yydestruct],
382 [static void],
383 [[const char *yymsg], [yymsg]],
384 [[int yytype], [yytype]],
385 [[YYSTYPE *yyvaluep], [yyvaluep]]b4_location_if([,
386 [[YYLTYPE *yylocationp], [yylocationp]]]))[
387 {
388 /* Pacify ``unused variable'' warnings. */
389 (void) yyvaluep;
390 ]b4_location_if([ (void) yylocationp;
391 ])[
392 if (!yymsg)
393 yymsg = "Deleting";
394 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
395
396 switch (yytype)
397 {
398 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
399 default:
400 break;
401 }
402 }]dnl
403 ])
404
405
406 # b4_yysymprint_generate(FUNCTION-DECLARATOR)
407 # -------------------------------------------
408 # Generate the "yysymprint" function, which declaration is issued using
409 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
410 # or "b4_c_function_def" for K&R.
411 m4_define_default([b4_yysymprint_generate],
412 [[/*--------------------------------.
413 | Print this symbol on YYOUTPUT. |
414 `--------------------------------*/
415
416 ]$1([yysymprint],
417 [static void],
418 [[FILE *yyoutput], [yyoutput]],
419 [[int yytype], [yytype]],
420 [[YYSTYPE *yyvaluep], [yyvaluep]]b4_location_if([,
421 [[YYLTYPE *yylocationp], [yylocationp]]]))
422 {
423 /* Pacify ``unused variable'' warnings. */
424 (void) yyvaluep;
425 b4_location_if([ (void) yylocationp;
426 ])dnl
427 [
428 if (yytype < YYNTOKENS)
429 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
430 else
431 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
432
433 ]b4_location_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
434 YYFPUTS (": ", yyoutput);
435 ])dnl
436 [
437 # ifdef YYPRINT
438 if (yytype < YYNTOKENS)
439 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
440 # endif
441 switch (yytype)
442 {
443 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
444 [ default:
445 break;
446 }
447 YYFPUTC (')', yyoutput);
448 }
449 ]])