]> git.saurik.com Git - bison.git/blob - data/c.m4
doc: mfcalc: demonstrate %printer.
[bison.git] / data / c.m4
1 -*- Autoconf -*-
2
3 # C M4 Macros for Bison.
4
5 # Copyright (C) 2002, 2004-2012 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>.
19
20 ## ---------------- ##
21 ## Identification. ##
22 ## ---------------- ##
23
24 # b4_comment_(TEXT, OPEN, CONTINUE, END)
25 # -------------------------------------
26 # Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
27 # Avoid adding indentation to the first line, as the indentation comes
28 # from OPEN. That's why we don't patsubst([$1], [^\(.\)], [ \1]).
29 #
30 # Prefix all the output lines with PREFIX.
31 m4_define([b4_comment_], [$2[]m4_bpatsubst([$1], [
32 \(.\)], [
33 $3\1])$4])
34
35
36 # b4_c_comment(TEXT, [PREFIX])
37 # ----------------------------
38 # Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
39 # Avoid adding indentation to the first line, as the indentation comes
40 # from "/*". That's why we don't patsubst([$1], [^\(.\)], [ \1]).
41 #
42 # Prefix all the output lines with PREFIX.
43 m4_define([b4_c_comment],
44 [b4_comment_([$1], [$2/* ], [$2 ], [$2 */])])
45
46
47 # b4_comment(TEXT, [PREFIX])
48 # --------------------------
49 # By default, C comments.
50 m4_define([b4_comment], [b4_c_comment($@)])
51
52
53 # b4_identification
54 # -----------------
55 # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
56 # b4_pull_flag if they use the values of the %define variables api.pure or
57 # api.push-pull.
58 m4_define([b4_identification],
59 [[/* Identify Bison output. */
60 #define YYBISON 1
61
62 /* Bison version. */
63 #define YYBISON_VERSION "]b4_version["
64
65 /* Skeleton name. */
66 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
67
68 /* Pure parsers. */
69 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
70
71 /* Push parsers. */
72 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
73
74 /* Pull parsers. */
75 #define YYPULL ]b4_pull_flag])[
76
77 /* Using locations. */
78 #define YYLSP_NEEDED ]b4_locations_if([1], [0])[
79 ]])
80
81
82 ## ---------------- ##
83 ## Default values. ##
84 ## ---------------- ##
85
86 # If the %union is not named, its name is YYSTYPE.
87 m4_define_default([b4_union_name], [YYSTYPE])
88
89 # If the %name-prefix is not given, it is yy.
90 m4_define_default([b4_prefix], [yy])
91
92 ## ------------------------ ##
93 ## Pure/impure interfaces. ##
94 ## ------------------------ ##
95
96 # b4_user_args
97 # ------------
98 m4_define([b4_user_args],
99 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
100
101
102 # b4_parse_param
103 # --------------
104 # If defined, b4_parse_param arrives double quoted, but below we prefer
105 # it to be single quoted.
106 m4_define([b4_parse_param],
107 b4_parse_param)
108
109
110 # b4_parse_param_for(DECL, FORMAL, BODY)
111 # ---------------------------------------
112 # Iterate over the user parameters, binding the declaration to DECL,
113 # the formal name to FORMAL, and evaluating the BODY.
114 m4_define([b4_parse_param_for],
115 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
116 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
117 m4_pushdef([$2], m4_shift($1_$2))dnl
118 $3[]dnl
119 m4_popdef([$2])dnl
120 m4_popdef([$1])dnl
121 ])])
122
123 # b4_parse_param_use([VAL], [LOC])
124 # --------------------------------
125 # `YYUSE' VAL, LOC if locations are enabled, and all the parse-params.
126 m4_define([b4_parse_param_use],
127 [m4_ifvaln([$1], [ YYUSE([$1]);])dnl
128 b4_locations_if([m4_ifvaln([$2], [ YYUSE ([$2]);])])dnl
129 b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
130 ])dnl
131 ])
132
133
134 ## ------------ ##
135 ## Data Types. ##
136 ## ------------ ##
137
138 # b4_int_type(MIN, MAX)
139 # ---------------------
140 # Return the smallest int type able to handle numbers ranging from
141 # MIN to MAX (included).
142 m4_define([b4_int_type],
143 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
144 b4_ints_in($@, [-128], [127]), [1], [signed char],
145
146 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
147 b4_ints_in($@, [-32768], [32767]), [1], [short int],
148
149 m4_eval([0 <= $1]), [1], [unsigned int],
150
151 [int])])
152
153
154 # b4_int_type_for(NAME)
155 # ---------------------
156 # Return the smallest int type able to handle numbers ranging from
157 # `NAME_min' to `NAME_max' (included).
158 m4_define([b4_int_type_for],
159 [b4_int_type($1_min, $1_max)])
160
161
162 # b4_table_value_equals(TABLE, VALUE, LITERAL)
163 # --------------------------------------------
164 # Without inducing a comparison warning from the compiler, check if the
165 # literal value LITERAL equals VALUE from table TABLE, which must have
166 # TABLE_min and TABLE_max defined. YYID must be defined as an identity
167 # function that suppresses warnings about constant conditions.
168 m4_define([b4_table_value_equals],
169 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
170 || m4_indir([b4_]$1[_max]) < $3), [1],
171 [[YYID (0)]],
172 [[((]$2[) == (]$3[))]])])
173
174
175 ## ---------##
176 ## Values. ##
177 ## ---------##
178
179
180 # b4_null_define
181 # --------------
182 # Portability issues: define a YY_NULL appropriate for the current
183 # language (C, C++98, or C++11).
184 m4_define([b4_null_define],
185 [# ifndef YY_NULL
186 # if defined __cplusplus && 201103L <= __cplusplus
187 # define YY_NULL nullptr
188 # else
189 # define YY_NULL 0
190 # endif
191 # endif[]dnl
192 ])
193
194
195 # b4_null
196 # -------
197 # Return a null pointer constant.
198 m4_define([b4_null], [YY_NULL])
199
200 # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
201 # -------------------------------------------------------------
202 # Define "yy<TABLE-NAME>" which contents is CONTENT.
203 m4_define([b4_integral_parser_table_define],
204 [m4_ifvaln([$3], [b4_c_comment([$3], [ ])])dnl
205 static const b4_int_type_for([$2]) yy$1[[]] =
206 {
207 $2
208 };dnl
209 ])
210
211
212 ## ------------------------- ##
213 ## Assigning token numbers. ##
214 ## ------------------------- ##
215
216 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
217 # -----------------------------------------
218 # Output the definition of this token as #define.
219 m4_define([b4_token_define],
220 [#define b4_percent_define_get([api.tokens.prefix])$1 $2
221 ])
222
223
224 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
225 # -------------------------------------------------------
226 # Output the definition of the tokens (if there are) as #defines.
227 m4_define([b4_token_defines],
228 [m4_if([$#$1], [1], [],
229 [/* Tokens. */
230 m4_map([b4_token_define], [$@])])
231 ])
232
233
234 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
235 # ---------------------------------------
236 # Output the definition of this token as an enum.
237 m4_define([b4_token_enum],
238 [b4_percent_define_get([api.tokens.prefix])$1 = $2])
239
240
241 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
242 # -----------------------------------------------------
243 # Output the definition of the tokens (if there are) as enums.
244 m4_define([b4_token_enums],
245 [m4_if([$#$1], [1], [],
246 [/* Tokens. */
247 #ifndef YYTOKENTYPE
248 # define YYTOKENTYPE
249 /* Put the tokens into the symbol table, so that GDB and other debuggers
250 know about them. */
251 enum yytokentype {
252 m4_map_sep([ b4_token_enum], [,
253 ],
254 [$@])
255 };
256 #endif
257 ])])
258
259
260 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
261 # -------------------------------------------------------------
262 # Output the definition of the tokens (if there are any) as enums and, if POSIX
263 # Yacc is enabled, as #defines.
264 m4_define([b4_token_enums_defines],
265 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
266 ])
267
268
269 ## ----------------- ##
270 ## Semantic Values. ##
271 ## ----------------- ##
272
273
274 # b4_symbol_value(VAL, [TYPE])
275 # ----------------------------
276 # Given a semantic value VAL ($$, $1 etc.), extract its value of type
277 # TYPE if TYPE is given, otherwise just return VAL. The result can be
278 # used safetly, it is put in parens to avoid nasty precedence issues.
279 # TYPE is *not* put in braces, provide some if needed.
280 m4_define([b4_symbol_value],
281 [($1[]m4_ifval([$2], [.$2]))])
282
283
284
285 ## --------------------------------------------- ##
286 ## Defining C functions in both K&R and ANSI-C. ##
287 ## --------------------------------------------- ##
288
289
290 # b4_modern_c
291 # -----------
292 # A predicate useful in #if to determine whether C is ancient or modern.
293 #
294 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
295 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
296 # reasons, but it defines __C99__FUNC__ so check that as well.
297 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
298 # Consider a C++ compiler to be modern if it defines __cplusplus.
299 #
300 m4_define([b4_c_modern],
301 [[(defined __STDC__ || defined __C99__FUNC__ \
302 || defined __cplusplus || defined _MSC_VER)]])
303
304 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
305 # ----------------------------------------------------------
306 # Declare the function NAME.
307 m4_define([b4_c_function_def],
308 [#if b4_c_modern
309 b4_c_ansi_function_def($@)
310 #else
311 $2
312 $1 (b4_c_knr_formal_names(m4_shift2($@)))
313 b4_c_knr_formal_decls(m4_shift2($@))
314 #endif[]dnl
315 ])
316
317
318 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
319 # ---------------------------------------------------------------
320 # Declare the function NAME in ANSI.
321 m4_define([b4_c_ansi_function_def],
322 [$2
323 $1 (b4_c_ansi_formals(m4_shift2($@)))[]dnl
324 ])
325
326
327 # b4_c_ansi_formals([DECL1, NAME1], ...)
328 # --------------------------------------
329 # Output the arguments ANSI-C definition.
330 m4_define([b4_c_ansi_formals],
331 [m4_if([$#], [0], [void],
332 [$#$1], [1], [void],
333 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
334
335 m4_define([b4_c_ansi_formal],
336 [$1])
337
338
339 # b4_c_knr_formal_names([DECL1, NAME1], ...)
340 # ------------------------------------------
341 # Output the argument names.
342 m4_define([b4_c_knr_formal_names],
343 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
344
345 m4_define([b4_c_knr_formal_name],
346 [$2])
347
348
349 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
350 # ------------------------------------------
351 # Output the K&R argument declarations.
352 m4_define([b4_c_knr_formal_decls],
353 [m4_map_sep([b4_c_knr_formal_decl],
354 [
355 ],
356 [$@])])
357
358 m4_define([b4_c_knr_formal_decl],
359 [ $1;])
360
361
362
363 ## ------------------------------------------------------------ ##
364 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
365 ## ------------------------------------------------------------ ##
366
367
368 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
369 # -----------------------------------------------------------
370 # Declare the function NAME.
371 m4_define([b4_c_function_decl],
372 [#if b4_c_modern
373 b4_c_ansi_function_decl($@)
374 #else
375 $2 $1 ();
376 #endif[]dnl
377 ])
378
379
380 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
381 # ----------------------------------------------------------------
382 # Declare the function NAME.
383 m4_define([b4_c_ansi_function_decl],
384 [$2 $1 (b4_c_ansi_formals(m4_shift2($@)));[]dnl
385 ])
386
387
388
389
390 ## --------------------- ##
391 ## Calling C functions. ##
392 ## --------------------- ##
393
394
395 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
396 # -----------------------------------------------------------
397 # Call the function NAME with arguments NAME1, NAME2 etc.
398 m4_define([b4_c_function_call],
399 [$1 (b4_c_args(m4_shift2($@)))[]dnl
400 ])
401
402
403 # b4_c_args([DECL1, NAME1], ...)
404 # ------------------------------
405 # Output the arguments NAME1, NAME2...
406 m4_define([b4_c_args],
407 [m4_map_sep([b4_c_arg], [, ], [$@])])
408
409 m4_define([b4_c_arg],
410 [$2])
411
412
413 ## ----------- ##
414 ## Synclines. ##
415 ## ----------- ##
416
417 # b4_sync_start(LINE, FILE)
418 # -----------------------
419 m4_define([b4_sync_start], [[#]line $1 $2])
420
421
422 ## -------------- ##
423 ## User actions. ##
424 ## -------------- ##
425
426 # b4_case(LABEL, STATEMENTS)
427 # --------------------------
428 m4_define([b4_case],
429 [ case $1:
430 $2
431 b4_syncline([@oline@], [@ofile@])
432 break;])
433
434
435 # b4_predicate_case(LABEL, CONDITIONS)
436 # ------------------------------------
437 m4_define([b4_predicate_case],
438 [ case $1:
439 if (! ($2)) YYERROR;
440 b4_syncline([@oline@], [@ofile@])
441 break;])
442
443
444 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
445 # -------------------------------------------
446 # Generate the "yydestruct" function, which declaration is issued using
447 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
448 # or "b4_c_function_def" for K&R.
449 m4_define_default([b4_yydestruct_generate],
450 [[/*-----------------------------------------------.
451 | Release the memory associated to this symbol. |
452 `-----------------------------------------------*/
453
454 /*ARGSUSED*/
455 ]$1([yydestruct],
456 [static void],
457 [[const char *yymsg], [yymsg]],
458 [[int yytype], [yytype]],
459 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
460 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
461 m4_ifset([b4_parse_param], [, b4_parse_param]))[
462 {
463 ]b4_parse_param_use([yyvaluep], [yylocationp])dnl
464 [ if (!yymsg)
465 yymsg = "Deleting";
466 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
467
468 switch (yytype)
469 {
470 ]b4_symbol_foreach([b4_symbol_destructor])dnl
471 [ default:
472 break;
473 }
474 }]dnl
475 ])
476
477
478 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
479 # ------------------------------------------------
480 # Generate the "yy_symbol_print" function, which declaration is issued using
481 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
482 # or "b4_c_function_def" for K&R.
483 m4_define_default([b4_yy_symbol_print_generate],
484 [[
485 /*--------------------------------.
486 | Print this symbol on YYOUTPUT. |
487 `--------------------------------*/
488
489 /*ARGSUSED*/
490 ]$1([yy_symbol_value_print],
491 [static void],
492 [[FILE *yyoutput], [yyoutput]],
493 [[int yytype], [yytype]],
494 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
495 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
496 m4_ifset([b4_parse_param], [, b4_parse_param]))[
497 {
498 ]b4_parse_param_use([yyoutput], [yylocationp])dnl
499 [ if (!yyvaluep)
500 return;]
501 dnl glr.c does not feature yytoknum.
502 m4_if(b4_skeleton, ["yacc.c"],
503 [[# ifdef YYPRINT
504 if (yytype < YYNTOKENS)
505 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
506 # endif
507 ]])dnl
508 [ switch (yytype)
509 {
510 ]b4_symbol_foreach([b4_symbol_printer])dnl
511 [ default:
512 break;
513 }
514 }
515
516
517 /*--------------------------------.
518 | Print this symbol on YYOUTPUT. |
519 `--------------------------------*/
520
521 ]$1([yy_symbol_print],
522 [static void],
523 [[FILE *yyoutput], [yyoutput]],
524 [[int yytype], [yytype]],
525 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
526 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
527 m4_ifset([b4_parse_param], [, b4_parse_param]))[
528 {
529 if (yytype < YYNTOKENS)
530 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
531 else
532 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
533
534 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
535 YYFPRINTF (yyoutput, ": ");
536 ])dnl
537 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
538 b4_locations_if([, yylocationp])[]b4_user_args[);
539 YYFPRINTF (yyoutput, ")");
540 }]dnl
541 ])