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)])
138 # b4_table_value_equals(TABLE, VALUE, LITERAL)
139 # --------------------------------------------
140 # Without inducing a comparison warning from the compiler, check if the
141 # literal value LITERAL equals VALUE from table TABLE, which must have
142 # TABLE_min and TABLE_max defined. YYID must be defined as an identity
143 # function that suppresses warnings about constant conditions.
144 m4_define([b4_table_value_equals],
145 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
146 || m4_indir([b4_]$1[_max]) < $3), [1],
148 [[((]$2[) == (]$3[))]])])
156 # Return a null pointer constant. NULL infringes on the user name
157 # space in C, so use 0 rather than NULL.
158 m4_define([b4_null], [0])
163 ## ------------------------- ##
164 ## Assigning token numbers. ##
165 ## ------------------------- ##
167 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
168 # -----------------------------------------
169 # Output the definition of this token as #define.
170 m4_define([b4_token_define],
175 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
176 # -------------------------------------------------------
177 # Output the definition of the tokens (if there are) as #defines.
178 m4_define([b4_token_defines],
179 [m4_if([$#$1], [1], [],
181 m4_map([b4_token_define], [$@])])
185 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
186 # ---------------------------------------
187 # Output the definition of this token as an enum.
188 m4_define([b4_token_enum],
192 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
193 # -----------------------------------------------------
194 # Output the definition of the tokens (if there are) as enums.
195 m4_define([b4_token_enums],
196 [m4_if([$#$1], [1], [],
200 /* Put the tokens into the symbol table, so that GDB and other debuggers
203 m4_map_sep([ b4_token_enum], [,
211 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
212 # -------------------------------------------------------------
213 # Output the definition of the tokens (if there are any) as enums and, if POSIX
214 # Yacc is enabled, as #defines.
215 m4_define([b4_token_enums_defines],
216 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
221 ## --------------------------------------------- ##
222 ## Defining C functions in both K&R and ANSI-C. ##
223 ## --------------------------------------------- ##
228 # A predicate useful in #if to determine whether C is ancient or modern.
230 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
231 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
232 # reasons, but it defines __C99__FUNC__ so check that as well.
233 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
234 # Consider a C++ compiler to be modern if it defines __cplusplus.
236 m4_define([b4_c_modern],
237 [[(defined __STDC__ || defined __C99__FUNC__ \
238 || defined __cplusplus || defined _MSC_VER)]])
240 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
241 # ----------------------------------------------------------
242 # Declare the function NAME.
243 m4_define([b4_c_function_def],
245 b4_c_ansi_function_def($@)
248 $1 (b4_c_knr_formal_names(m4_shift2($@)))
249 b4_c_knr_formal_decls(m4_shift2($@))
254 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
255 # ---------------------------------------------------------------
256 # Declare the function NAME in ANSI.
257 m4_define([b4_c_ansi_function_def],
259 $1 (b4_c_ansi_formals(m4_shift2($@)))[]dnl
263 # b4_c_ansi_formals([DECL1, NAME1], ...)
264 # --------------------------------------
265 # Output the arguments ANSI-C definition.
266 m4_define([b4_c_ansi_formals],
267 [m4_if([$#], [0], [void],
269 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
271 m4_define([b4_c_ansi_formal],
275 # b4_c_knr_formal_names([DECL1, NAME1], ...)
276 # ------------------------------------------
277 # Output the argument names.
278 m4_define([b4_c_knr_formal_names],
279 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
281 m4_define([b4_c_knr_formal_name],
285 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
286 # ------------------------------------------
287 # Output the K&R argument declarations.
288 m4_define([b4_c_knr_formal_decls],
289 [m4_map_sep([b4_c_knr_formal_decl],
294 m4_define([b4_c_knr_formal_decl],
299 ## ------------------------------------------------------------ ##
300 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
301 ## ------------------------------------------------------------ ##
304 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
305 # -----------------------------------------------------------
306 # Declare the function NAME.
307 m4_define([b4_c_function_decl],
308 [#if defined __STDC__ || defined __cplusplus
309 b4_c_ansi_function_decl($@)
316 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
317 # ----------------------------------------------------------------
318 # Declare the function NAME.
319 m4_define([b4_c_ansi_function_decl],
320 [$2 $1 (b4_c_ansi_formals(m4_shift2($@)));[]dnl
326 ## --------------------- ##
327 ## Calling C functions. ##
328 ## --------------------- ##
331 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
332 # -----------------------------------------------------------
333 # Call the function NAME with arguments NAME1, NAME2 etc.
334 m4_define([b4_c_function_call],
335 [$1 (b4_c_args(m4_shift2($@)))[]dnl
339 # b4_c_args([DECL1, NAME1], ...)
340 # ------------------------------
341 # Output the arguments NAME1, NAME2...
342 m4_define([b4_c_args],
343 [m4_map_sep([b4_c_arg], [, ], [$@])])
345 m4_define([b4_c_arg],
353 # b4_sync_start(LINE, FILE)
354 # -----------------------
355 m4_define([b4_sync_start], [[#]line $1 $2])
362 # b4_case(LABEL, STATEMENTS)
363 # --------------------------
369 # b4_symbol_actions(FILENAME, LINENO,
370 # SYMBOL-TAG, SYMBOL-NUM,
371 # SYMBOL-ACTION, SYMBOL-TYPENAME)
372 # -------------------------------------------------
373 m4_define([b4_symbol_actions],
374 [m4_pushdef([b4_dollar_dollar],
375 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
376 m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
378 b4_syncline([$2], [$1])
380 b4_syncline([@oline@], [@ofile@])
382 m4_popdef([b4_at_dollar])dnl
383 m4_popdef([b4_dollar_dollar])dnl
387 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
388 # -------------------------------------------
389 # Generate the "yydestruct" function, which declaration is issued using
390 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
391 # or "b4_c_function_def" for K&R.
392 m4_define_default([b4_yydestruct_generate],
393 [[/*-----------------------------------------------.
394 | Release the memory associated to this symbol. |
395 `-----------------------------------------------*/
400 [[const char *yymsg], [yymsg]],
401 [[int yytype], [yytype]],
402 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
403 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
404 m4_ifset([b4_parse_param], [, b4_parse_param]))[
407 ]b4_locations_if([ YYUSE (yylocationp);
409 b4_parse_param_use[]dnl
413 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
417 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
425 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
426 # ------------------------------------------------
427 # Generate the "yy_symbol_print" function, which declaration is issued using
428 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
429 # or "b4_c_function_def" for K&R.
430 m4_define_default([b4_yy_symbol_print_generate],
432 /*--------------------------------.
433 | Print this symbol on YYOUTPUT. |
434 `--------------------------------*/
437 ]$1([yy_symbol_value_print],
439 [[FILE *yyoutput], [yyoutput]],
440 [[int yytype], [yytype]],
441 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
442 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
443 m4_ifset([b4_parse_param], [, b4_parse_param]))[
447 ]b4_locations_if([ YYUSE (yylocationp);
449 b4_parse_param_use[]dnl
451 if (yytype < YYNTOKENS)
452 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
458 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
465 /*--------------------------------.
466 | Print this symbol on YYOUTPUT. |
467 `--------------------------------*/
469 ]$1([yy_symbol_print],
471 [[FILE *yyoutput], [yyoutput]],
472 [[int yytype], [yytype]],
473 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
474 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
475 m4_ifset([b4_parse_param], [, b4_parse_param]))[
477 if (yytype < YYNTOKENS)
478 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
480 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
482 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
483 YYFPRINTF (yyoutput, ": ");
485 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
486 b4_locations_if([, yylocationp])[]b4_user_args[);
487 YYFPRINTF (yyoutput, ")");