3 # C M4 Macros for Bison.
5 # Copyright (C) 2002, 2004-2012 Free Software Foundation, Inc.
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.
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.
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/>.
20 ## ---------------- ##
22 ## ---------------- ##
26 m4_define([b4_comment], [/* m4_bpatsubst([$1], [
32 # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
33 # b4_pull_flag if they use the values of the %define variables api.pure or
35 m4_define([b4_identification],
36 [[/* Identify Bison output. */
40 #define YYBISON_VERSION "]b4_version["
43 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
46 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
49 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
52 #define YYPULL ]b4_pull_flag])[
54 /* Using locations. */
55 #define YYLSP_NEEDED ]b4_locations_flag[
59 ## ---------------- ##
61 ## ---------------- ##
63 # If the %union is not named, its name is YYSTYPE.
64 m4_define_default([b4_union_name], [YYSTYPE])
66 # If the %name-prefix is not given, it is yy.
67 m4_define_default([b4_prefix], [yy])
69 ## ------------------------ ##
70 ## Pure/impure interfaces. ##
71 ## ------------------------ ##
75 m4_define([b4_user_args],
76 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
81 # If defined, b4_parse_param arrives double quoted, but below we prefer
82 # it to be single quoted.
83 m4_define([b4_parse_param],
87 # b4_parse_param_for(DECL, FORMAL, BODY)
88 # ---------------------------------------
89 # Iterate over the user parameters, binding the declaration to DECL,
90 # the formal name to FORMAL, and evaluating the BODY.
91 m4_define([b4_parse_param_for],
92 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
93 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
94 m4_pushdef([$2], m4_shift($1_$2))dnl
102 # `YYUSE' all the parse-params.
103 m4_define([b4_parse_param_use],
104 [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
113 # b4_int_type(MIN, MAX)
114 # ---------------------
115 # Return the smallest int type able to handle numbers ranging from
116 # MIN to MAX (included).
117 m4_define([b4_int_type],
118 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
119 b4_ints_in($@, [-128], [127]), [1], [signed char],
121 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
122 b4_ints_in($@, [-32768], [32767]), [1], [short int],
124 m4_eval([0 <= $1]), [1], [unsigned int],
129 # b4_int_type_for(NAME)
130 # ---------------------
131 # Return the smallest int type able to handle numbers ranging from
132 # `NAME_min' to `NAME_max' (included).
133 m4_define([b4_int_type_for],
134 [b4_int_type($1_min, $1_max)])
137 # b4_table_value_equals(TABLE, VALUE, LITERAL)
138 # --------------------------------------------
139 # Without inducing a comparison warning from the compiler, check if the
140 # literal value LITERAL equals VALUE from table TABLE, which must have
141 # TABLE_min and TABLE_max defined. YYID must be defined as an identity
142 # function that suppresses warnings about constant conditions.
143 m4_define([b4_table_value_equals],
144 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
145 || m4_indir([b4_]$1[_max]) < $3), [1],
147 [[((]$2[) == (]$3[))]])])
155 # Return a null pointer constant. NULL infringes on the user name
156 # space in C, so use 0 rather than NULL.
157 m4_define([b4_null], [0])
162 ## ------------------------- ##
163 ## Assigning token numbers. ##
164 ## ------------------------- ##
166 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
167 # -----------------------------------------
168 # Output the definition of this token as #define.
169 m4_define([b4_token_define],
174 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
175 # -------------------------------------------------------
176 # Output the definition of the tokens (if there are) as #defines.
177 m4_define([b4_token_defines],
178 [m4_if([$#$1], [1], [],
180 m4_map([b4_token_define], [$@])])
184 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
185 # ---------------------------------------
186 # Output the definition of this token as an enum.
187 m4_define([b4_token_enum],
191 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
192 # -----------------------------------------------------
193 # Output the definition of the tokens (if there are) as enums.
194 m4_define([b4_token_enums],
195 [m4_if([$#$1], [1], [],
199 /* Put the tokens into the symbol table, so that GDB and other debuggers
202 m4_map_sep([ b4_token_enum], [,
210 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
211 # -------------------------------------------------------------
212 # Output the definition of the tokens (if there are any) as enums and, if POSIX
213 # Yacc is enabled, as #defines.
214 m4_define([b4_token_enums_defines],
215 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
220 ## --------------------------------------------- ##
221 ## Defining C functions in both K&R and ANSI-C. ##
222 ## --------------------------------------------- ##
227 # A predicate useful in #if to determine whether C is ancient or modern.
229 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
230 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
231 # reasons, but it defines __C99__FUNC__ so check that as well.
232 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
233 # Consider a C++ compiler to be modern if it defines __cplusplus.
235 m4_define([b4_c_modern],
236 [[(defined __STDC__ || defined __C99__FUNC__ \
237 || defined __cplusplus || defined _MSC_VER)]])
239 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
240 # ----------------------------------------------------------
241 # Declare the function NAME.
242 m4_define([b4_c_function_def],
244 b4_c_ansi_function_def($@)
247 $1 (b4_c_knr_formal_names(m4_shift2($@)))
248 b4_c_knr_formal_decls(m4_shift2($@))
253 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
254 # ---------------------------------------------------------------
255 # Declare the function NAME in ANSI.
256 m4_define([b4_c_ansi_function_def],
258 $1 (b4_c_ansi_formals(m4_shift2($@)))[]dnl
262 # b4_c_ansi_formals([DECL1, NAME1], ...)
263 # --------------------------------------
264 # Output the arguments ANSI-C definition.
265 m4_define([b4_c_ansi_formals],
266 [m4_if([$#], [0], [void],
268 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
270 m4_define([b4_c_ansi_formal],
274 # b4_c_knr_formal_names([DECL1, NAME1], ...)
275 # ------------------------------------------
276 # Output the argument names.
277 m4_define([b4_c_knr_formal_names],
278 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
280 m4_define([b4_c_knr_formal_name],
284 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
285 # ------------------------------------------
286 # Output the K&R argument declarations.
287 m4_define([b4_c_knr_formal_decls],
288 [m4_map_sep([b4_c_knr_formal_decl],
293 m4_define([b4_c_knr_formal_decl],
298 ## ------------------------------------------------------------ ##
299 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
300 ## ------------------------------------------------------------ ##
303 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
304 # -----------------------------------------------------------
305 # Declare the function NAME.
306 m4_define([b4_c_function_decl],
307 [#if defined __STDC__ || defined __cplusplus
308 b4_c_ansi_function_decl($@)
315 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
316 # ----------------------------------------------------------------
317 # Declare the function NAME.
318 m4_define([b4_c_ansi_function_decl],
319 [$2 $1 (b4_c_ansi_formals(m4_shift2($@)));[]dnl
325 ## --------------------- ##
326 ## Calling C functions. ##
327 ## --------------------- ##
330 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
331 # -----------------------------------------------------------
332 # Call the function NAME with arguments NAME1, NAME2 etc.
333 m4_define([b4_c_function_call],
334 [$1 (b4_c_args(m4_shift2($@)))[]dnl
338 # b4_c_args([DECL1, NAME1], ...)
339 # ------------------------------
340 # Output the arguments NAME1, NAME2...
341 m4_define([b4_c_args],
342 [m4_map_sep([b4_c_arg], [, ], [$@])])
344 m4_define([b4_c_arg],
352 # b4_sync_start(LINE, FILE)
353 # -----------------------
354 m4_define([b4_sync_start], [[#]line $1 $2])
361 # b4_case(LABEL, STATEMENTS)
362 # --------------------------
368 # b4_symbol_actions(FILENAME, LINENO,
369 # SYMBOL-TAG, SYMBOL-NUM,
370 # SYMBOL-ACTION, SYMBOL-TYPENAME)
371 # -------------------------------------------------
372 m4_define([b4_symbol_actions],
373 [m4_pushdef([b4_dollar_dollar],
374 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
375 m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
377 b4_syncline([$2], [$1])
379 b4_syncline([@oline@], [@ofile@])
381 m4_popdef([b4_at_dollar])dnl
382 m4_popdef([b4_dollar_dollar])dnl
386 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
387 # -------------------------------------------
388 # Generate the "yydestruct" function, which declaration is issued using
389 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
390 # or "b4_c_function_def" for K&R.
391 m4_define_default([b4_yydestruct_generate],
392 [[/*-----------------------------------------------.
393 | Release the memory associated to this symbol. |
394 `-----------------------------------------------*/
399 [[const char *yymsg], [yymsg]],
400 [[int yytype], [yytype]],
401 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
402 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
403 m4_ifset([b4_parse_param], [, b4_parse_param]))[
406 ]b4_locations_if([ YYUSE (yylocationp);
408 b4_parse_param_use[]dnl
412 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
416 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
424 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
425 # ------------------------------------------------
426 # Generate the "yy_symbol_print" function, which declaration is issued using
427 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
428 # or "b4_c_function_def" for K&R.
429 m4_define_default([b4_yy_symbol_print_generate],
431 /*--------------------------------.
432 | Print this symbol on YYOUTPUT. |
433 `--------------------------------*/
436 ]$1([yy_symbol_value_print],
438 [[FILE *yyoutput], [yyoutput]],
439 [[int yytype], [yytype]],
440 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
441 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
442 m4_ifset([b4_parse_param], [, b4_parse_param]))[
446 ]b4_locations_if([ YYUSE (yylocationp);
448 b4_parse_param_use[]dnl
450 if (yytype < YYNTOKENS)
451 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
457 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
464 /*--------------------------------.
465 | Print this symbol on YYOUTPUT. |
466 `--------------------------------*/
468 ]$1([yy_symbol_print],
470 [[FILE *yyoutput], [yyoutput]],
471 [[int yytype], [yytype]],
472 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
473 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
474 m4_ifset([b4_parse_param], [, b4_parse_param]))[
476 if (yytype < YYNTOKENS)
477 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
479 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
481 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
482 YYFPRINTF (yyoutput, ": ");
484 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
485 b4_locations_if([, yylocationp])[]b4_user_args[);
486 YYFPRINTF (yyoutput, ")");