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