3 # C M4 Macros for Bison.
5 # Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free
6 # Software Foundation, Inc.
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ## ---------------- ##
23 ## ---------------- ##
27 m4_define([b4_comment], [/* m4_bpatsubst([$1], [
33 # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
34 # b4_pull_flag if they use the values of the %define variables api.pure or
36 m4_define([b4_identification],
37 [[/* Identify Bison output. */
41 #define YYBISON_VERSION "]b4_version["
44 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
47 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
50 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
53 #define YYPULL ]b4_pull_flag])[
55 /* Using locations. */
56 #define YYLSP_NEEDED ]b4_locations_flag[
60 ## ---------------- ##
62 ## ---------------- ##
64 # If the %union is not named, its name is YYSTYPE.
65 m4_define_default([b4_union_name], [YYSTYPE])
67 # If the %name-prefix is not given, it is yy.
68 m4_define_default([b4_prefix], [yy])
70 ## ------------------------ ##
71 ## Pure/impure interfaces. ##
72 ## ------------------------ ##
76 m4_define([b4_user_args],
77 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
82 # If defined, b4_parse_param arrives double quoted, but below we prefer
83 # it to be single quoted.
84 m4_define([b4_parse_param],
88 # b4_parse_param_for(DECL, FORMAL, BODY)
89 # ---------------------------------------
90 # Iterate over the user parameters, binding the declaration to DECL,
91 # the formal name to FORMAL, and evaluating the BODY.
92 m4_define([b4_parse_param_for],
93 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
94 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
95 m4_pushdef([$2], m4_shift($1_$2))dnl
103 # `YYUSE' all the parse-params.
104 m4_define([b4_parse_param_use],
105 [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
114 # b4_int_type(MIN, MAX)
115 # ---------------------
116 # Return the smallest int type able to handle numbers ranging from
117 # MIN to MAX (included).
118 m4_define([b4_int_type],
119 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
120 b4_ints_in($@, [-128], [127]), [1], [signed char],
122 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
123 b4_ints_in($@, [-32768], [32767]), [1], [short int],
125 m4_eval([0 <= $1]), [1], [unsigned int],
130 # b4_int_type_for(NAME)
131 # ---------------------
132 # Return the smallest int type able to handle numbers ranging from
133 # `NAME_min' to `NAME_max' (included).
134 m4_define([b4_int_type_for],
135 [b4_int_type($1_min, $1_max)])
145 # Return a null pointer constant. NULL infringes on the user name
146 # space in C, so use 0 rather than NULL.
147 m4_define([b4_null], [0])
152 ## ------------------------- ##
153 ## Assigning token numbers. ##
154 ## ------------------------- ##
156 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
157 # -----------------------------------------
158 # Output the definition of this token as #define.
159 m4_define([b4_token_define],
164 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
165 # -------------------------------------------------------
166 # Output the definition of the tokens (if there are) as #defines.
167 m4_define([b4_token_defines],
168 [m4_if([$#$1], [1], [],
170 m4_map([b4_token_define], [$@])])
174 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
175 # ---------------------------------------
176 # Output the definition of this token as an enum.
177 m4_define([b4_token_enum],
181 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
182 # -----------------------------------------------------
183 # Output the definition of the tokens (if there are) as enums.
184 m4_define([b4_token_enums],
185 [m4_if([$#$1], [1], [],
189 /* Put the tokens into the symbol table, so that GDB and other debuggers
192 m4_map_sep([ b4_token_enum], [,
200 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
201 # -------------------------------------------------------------
202 # Output the definition of the tokens (if there are any) as enums and, if POSIX
203 # Yacc is enabled, as #defines.
204 m4_define([b4_token_enums_defines],
205 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
210 ## --------------------------------------------- ##
211 ## Defining C functions in both K&R and ANSI-C. ##
212 ## --------------------------------------------- ##
217 # A predicate useful in #if to determine whether C is ancient or modern.
219 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
220 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
221 # reasons, but it defines __C99__FUNC__ so check that as well.
222 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
223 # Consider a C++ compiler to be modern if it defines __cplusplus.
225 m4_define([b4_c_modern],
226 [[(defined __STDC__ || defined __C99__FUNC__ \
227 || defined __cplusplus || defined _MSC_VER)]])
229 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
230 # ----------------------------------------------------------
231 # Declare the function NAME.
232 m4_define([b4_c_function_def],
234 b4_c_ansi_function_def($@)
237 $1 (b4_c_knr_formal_names(m4_shift2($@)))
238 b4_c_knr_formal_decls(m4_shift2($@))
243 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
244 # ---------------------------------------------------------------
245 # Declare the function NAME in ANSI.
246 m4_define([b4_c_ansi_function_def],
248 $1 (b4_c_ansi_formals(m4_shift2($@)))[]dnl
252 # b4_c_ansi_formals([DECL1, NAME1], ...)
253 # --------------------------------------
254 # Output the arguments ANSI-C definition.
255 m4_define([b4_c_ansi_formals],
256 [m4_if([$#], [0], [void],
258 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
260 m4_define([b4_c_ansi_formal],
264 # b4_c_knr_formal_names([DECL1, NAME1], ...)
265 # ------------------------------------------
266 # Output the argument names.
267 m4_define([b4_c_knr_formal_names],
268 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
270 m4_define([b4_c_knr_formal_name],
274 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
275 # ------------------------------------------
276 # Output the K&R argument declarations.
277 m4_define([b4_c_knr_formal_decls],
278 [m4_map_sep([b4_c_knr_formal_decl],
283 m4_define([b4_c_knr_formal_decl],
288 ## ------------------------------------------------------------ ##
289 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
290 ## ------------------------------------------------------------ ##
293 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
294 # -----------------------------------------------------------
295 # Declare the function NAME.
296 m4_define([b4_c_function_decl],
297 [#if defined __STDC__ || defined __cplusplus
298 b4_c_ansi_function_decl($@)
305 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
306 # ----------------------------------------------------------------
307 # Declare the function NAME.
308 m4_define([b4_c_ansi_function_decl],
309 [$2 $1 (b4_c_ansi_formals(m4_shift2($@)));[]dnl
315 ## --------------------- ##
316 ## Calling C functions. ##
317 ## --------------------- ##
320 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
321 # -----------------------------------------------------------
322 # Call the function NAME with arguments NAME1, NAME2 etc.
323 m4_define([b4_c_function_call],
324 [$1 (b4_c_args(m4_shift2($@)))[]dnl
328 # b4_c_args([DECL1, NAME1], ...)
329 # ------------------------------
330 # Output the arguments NAME1, NAME2...
331 m4_define([b4_c_args],
332 [m4_map_sep([b4_c_arg], [, ], [$@])])
334 m4_define([b4_c_arg],
342 # b4_sync_start(LINE, FILE)
343 # -----------------------
344 m4_define([b4_sync_start], [[#]line $1 $2])
351 # b4_case(LABEL, STATEMENTS)
352 # --------------------------
358 # b4_symbol_actions(FILENAME, LINENO,
359 # SYMBOL-TAG, SYMBOL-NUM,
360 # SYMBOL-ACTION, SYMBOL-TYPENAME)
361 # -------------------------------------------------
362 m4_define([b4_symbol_actions],
363 [m4_pushdef([b4_dollar_dollar],
364 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
365 m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
367 b4_syncline([$2], [$1])
369 b4_syncline([@oline@], [@ofile@])
371 m4_popdef([b4_at_dollar])dnl
372 m4_popdef([b4_dollar_dollar])dnl
376 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
377 # -------------------------------------------
378 # Generate the "yydestruct" function, which declaration is issued using
379 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
380 # or "b4_c_function_def" for K&R.
381 m4_define_default([b4_yydestruct_generate],
382 [[/*-----------------------------------------------.
383 | Release the memory associated to this symbol. |
384 `-----------------------------------------------*/
389 [[const char *yymsg], [yymsg]],
390 [[int yytype], [yytype]],
391 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
392 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
393 m4_ifset([b4_parse_param], [, b4_parse_param]))[
396 ]b4_locations_if([ YYUSE (yylocationp);
398 b4_parse_param_use[]dnl
402 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
406 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
414 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
415 # ------------------------------------------------
416 # Generate the "yy_symbol_print" function, which declaration is issued using
417 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
418 # or "b4_c_function_def" for K&R.
419 m4_define_default([b4_yy_symbol_print_generate],
421 /*--------------------------------.
422 | Print this symbol on YYOUTPUT. |
423 `--------------------------------*/
426 ]$1([yy_symbol_value_print],
428 [[FILE *yyoutput], [yyoutput]],
429 [[int yytype], [yytype]],
430 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
431 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
432 m4_ifset([b4_parse_param], [, b4_parse_param]))[
436 ]b4_locations_if([ YYUSE (yylocationp);
438 b4_parse_param_use[]dnl
440 if (yytype < YYNTOKENS)
441 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
447 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
454 /*--------------------------------.
455 | Print this symbol on YYOUTPUT. |
456 `--------------------------------*/
458 ]$1([yy_symbol_print],
460 [[FILE *yyoutput], [yyoutput]],
461 [[int yytype], [yytype]],
462 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
463 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
464 m4_ifset([b4_parse_param], [, b4_parse_param]))[
466 if (yytype < YYNTOKENS)
467 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
469 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
471 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
472 YYFPRINTF (yyoutput, ": ");
474 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
475 b4_locations_if([, yylocationp])[]b4_user_args[);
476 YYFPRINTF (yyoutput, ")");