3 # C M4 Macros for Bison.
4 # Copyright (C) 2002, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
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 3 of the License, or
9 # (at your option) any later version.
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.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 ## ---------------- ##
21 ## ---------------- ##
25 m4_define([b4_comment], [/* m4_bpatsubst([$1], [
31 m4_define([b4_identification],
32 [/* Identify Bison output. */
36 [#]define YYBISON_VERSION "b4_version"
39 [#]define YYSKELETON_NAME b4_skeleton
42 [#]define YYPURE b4_pure_flag
45 [#]define YYPUSH b4_push_flag
48 [#]define YYPULL b4_pull_flag
50 /* Using locations. */
51 [#]define YYLSP_NEEDED b4_locations_flag
55 ## ---------------- ##
57 ## ---------------- ##
59 # If the %union is not named, its name is YYSTYPE.
60 m4_define_default([b4_union_name], [YYSTYPE])
62 # If the %name-prefix is not given, it is yy.
63 m4_define_default([b4_prefix], [yy])
65 ## ------------------------ ##
66 ## Pure/impure interfaces. ##
67 ## ------------------------ ##
71 m4_define([b4_user_args],
72 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
77 # If defined, b4_parse_param arrives double quoted, but below we prefer
78 # it to be single quoted.
79 m4_define([b4_parse_param],
83 # b4_parse_param_for(DECL, FORMAL, BODY)
84 # ---------------------------------------
85 # Iterate over the user parameters, binding the declaration to DECL,
86 # the formal name to FORMAL, and evaluating the BODY.
87 m4_define([b4_parse_param_for],
88 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
89 [m4_pushdef([$1], m4_fst($1_$2))dnl
90 m4_pushdef([$2], m4_shift($1_$2))dnl
98 # `YYUSE' all the parse-params.
99 m4_define([b4_parse_param_use],
100 [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
109 # b4_int_type(MIN, MAX)
110 # ---------------------
111 # Return the smallest int type able to handle numbers ranging from
112 # MIN to MAX (included).
113 m4_define([b4_int_type],
114 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
115 b4_ints_in($@, [-128], [127]), [1], [signed char],
117 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
118 b4_ints_in($@, [-32768], [32767]), [1], [short int],
120 m4_eval([0 <= $1]), [1], [unsigned int],
125 # b4_int_type_for(NAME)
126 # ---------------------
127 # Return the smallest int type able to handle numbers ranging from
128 # `NAME_min' to `NAME_max' (included).
129 m4_define([b4_int_type_for],
130 [b4_int_type($1_min, $1_max)])
138 # Return a null pointer constant. NULL infringes on the user name
139 # space in C, so use 0 rather than NULL.
140 m4_define([b4_null], [0])
143 ## ------------------------- ##
144 ## Assigning token numbers. ##
145 ## ------------------------- ##
147 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
148 # -----------------------------------------
149 # Output the definition of this token as #define.
150 m4_define([b4_token_define],
155 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
156 # -------------------------------------------------------
157 # Output the definition of the tokens (if there are) as #defines.
158 m4_define([b4_token_defines],
159 [m4_if([$@], [[]], [],
161 m4_map([b4_token_define], [$@])])
165 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
166 # ---------------------------------------
167 # Output the definition of this token as an enum.
168 m4_define([b4_token_enum],
172 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
173 # -----------------------------------------------------
174 # Output the definition of the tokens (if there are) as enums.
175 m4_define([b4_token_enums],
176 [m4_if([$@], [[]], [],
180 /* Put the tokens into the symbol table, so that GDB and other debuggers
183 m4_map_sep([ b4_token_enum], [,
191 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
192 # -------------------------------------------------------------
193 # Output the definition of the tokens (if there are any) as enums and, if POSIX
194 # Yacc is enabled, as #defines.
195 m4_define([b4_token_enums_defines],
196 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
201 ## --------------------------------------------- ##
202 ## Defining C functions in both K&R and ANSI-C. ##
203 ## --------------------------------------------- ##
208 # A predicate useful in #if to determine whether C is ancient or modern.
210 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
211 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
212 # reasons, but it defines __C99__FUNC__ so check that as well.
213 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
214 # Consider a C++ compiler to be modern if it defines __cplusplus.
216 m4_define([b4_c_modern],
217 [[(defined __STDC__ || defined __C99__FUNC__ \
218 || defined __cplusplus || defined _MSC_VER)]])
220 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
221 # ----------------------------------------------------------
222 # Declare the function NAME.
223 m4_define([b4_c_function_def],
225 b4_c_ansi_function_def($@)
228 $1 (b4_c_knr_formal_names(m4_shiftn(2, $@)))
229 b4_c_knr_formal_decls(m4_shiftn(2, $@))
234 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
235 # ---------------------------------------------------------------
236 # Declare the function NAME in ANSI.
237 m4_define([b4_c_ansi_function_def],
239 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)))[]dnl
243 # b4_c_ansi_formals([DECL1, NAME1], ...)
244 # --------------------------------------
245 # Output the arguments ANSI-C definition.
246 m4_define([b4_c_ansi_formals],
250 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
252 m4_define([b4_c_ansi_formal],
256 # b4_c_knr_formal_names([DECL1, NAME1], ...)
257 # ------------------------------------------
258 # Output the argument names.
259 m4_define([b4_c_knr_formal_names],
260 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
262 m4_define([b4_c_knr_formal_name],
266 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
267 # ------------------------------------------
268 # Output the K&R argument declarations.
269 m4_define([b4_c_knr_formal_decls],
270 [m4_map_sep([b4_c_knr_formal_decl],
275 m4_define([b4_c_knr_formal_decl],
280 ## ------------------------------------------------------------ ##
281 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
282 ## ------------------------------------------------------------ ##
285 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
286 # -----------------------------------------------------------
287 # Declare the function NAME.
288 m4_define([b4_c_function_decl],
289 [#if defined __STDC__ || defined __cplusplus
290 b4_c_ansi_function_decl($@)
297 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
298 # ----------------------------------------------------------------
299 # Declare the function NAME.
300 m4_define([b4_c_ansi_function_decl],
301 [$2 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)));[]dnl
307 ## --------------------- ##
308 ## Calling C functions. ##
309 ## --------------------- ##
312 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
313 # -----------------------------------------------------------
314 # Call the function NAME with arguments NAME1, NAME2 etc.
315 m4_define([b4_c_function_call],
316 [$1 (b4_c_args(m4_shiftn(2, $@)))[]dnl
320 # b4_c_args([DECL1, NAME1], ...)
321 # ------------------------------
322 # Output the arguments NAME1, NAME2...
323 m4_define([b4_c_args],
324 [m4_map_sep([b4_c_arg], [, ], [$@])])
326 m4_define([b4_c_arg],
334 # b4_sync_start(LINE, FILE)
335 # -----------------------
336 m4_define([b4_sync_start], [[#]line $1 $2])
343 # b4_case(LABEL, STATEMENTS)
344 # --------------------------
350 # b4_symbol_actions(FILENAME, LINENO,
351 # SYMBOL-TAG, SYMBOL-NUM,
352 # SYMBOL-ACTION, SYMBOL-TYPENAME)
353 # -------------------------------------------------
354 m4_define([b4_symbol_actions],
355 [m4_pushdef([b4_dollar_dollar],
356 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
357 m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
359 b4_syncline([$2], [$1])
361 b4_syncline([@oline@], [@ofile@])
363 m4_popdef([b4_at_dollar])dnl
364 m4_popdef([b4_dollar_dollar])dnl
368 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
369 # -------------------------------------------
370 # Generate the "yydestruct" function, which declaration is issued using
371 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
372 # or "b4_c_function_def" for K&R.
373 m4_define_default([b4_yydestruct_generate],
374 [[/*-----------------------------------------------.
375 | Release the memory associated to this symbol. |
376 `-----------------------------------------------*/
381 [[const char *yymsg], [yymsg]],
382 [[int yytype], [yytype]],
383 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
384 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
385 m4_ifset([b4_parse_param], [, b4_parse_param]))[
388 ]b4_locations_if([ YYUSE (yylocationp);
390 b4_parse_param_use[]dnl
394 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
398 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
406 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
407 # ------------------------------------------------
408 # Generate the "yy_symbol_print" function, which declaration is issued using
409 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
410 # or "b4_c_function_def" for K&R.
411 m4_define_default([b4_yy_symbol_print_generate],
413 /*--------------------------------.
414 | Print this symbol on YYOUTPUT. |
415 `--------------------------------*/
418 ]$1([yy_symbol_value_print],
420 [[FILE *yyoutput], [yyoutput]],
421 [[int yytype], [yytype]],
422 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
423 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
424 m4_ifset([b4_parse_param], [, b4_parse_param]))[
428 ]b4_locations_if([ YYUSE (yylocationp);
430 b4_parse_param_use[]dnl
432 if (yytype < YYNTOKENS)
433 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
439 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
446 /*--------------------------------.
447 | Print this symbol on YYOUTPUT. |
448 `--------------------------------*/
450 ]$1([yy_symbol_print],
452 [[FILE *yyoutput], [yyoutput]],
453 [[int yytype], [yytype]],
454 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
455 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
456 m4_ifset([b4_parse_param], [, b4_parse_param]))[
458 if (yytype < YYNTOKENS)
459 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
461 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
463 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
464 YYFPRINTF (yyoutput, ": ");
466 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
467 b4_locations_if([, yylocationp])[]b4_user_args[);
468 YYFPRINTF (yyoutput, ")");