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
44 /* Using locations. */
45 [#]define YYLSP_NEEDED b4_locations_flag
49 ## ---------------- ##
51 ## ---------------- ##
53 # If the %union is not named, its name is YYSTYPE.
54 m4_define_default([b4_union_name], [YYSTYPE])
56 # If the %name-prefix is not given, it is yy.
57 m4_define_default([b4_prefix], [yy])
59 ## ------------------------ ##
60 ## Pure/impure interfaces. ##
61 ## ------------------------ ##
65 m4_define([b4_user_args],
66 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
71 # If defined, b4_parse_param arrives double quoted, but below we prefer
72 # it to be single quoted.
73 m4_define([b4_parse_param],
77 # b4_parse_param_for(DECL, FORMAL, BODY)
78 # ---------------------------------------
79 # Iterate over the user parameters, binding the declaration to DECL,
80 # the formal name to FORMAL, and evaluating the BODY.
81 m4_define([b4_parse_param_for],
82 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
83 [m4_pushdef([$1], m4_fst($1_$2))dnl
84 m4_pushdef([$2], m4_shift($1_$2))dnl
92 # `YYUSE' all the parse-params.
93 m4_define([b4_parse_param_use],
94 [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
103 # b4_int_type(MIN, MAX)
104 # ---------------------
105 # Return the smallest int type able to handle numbers ranging from
106 # MIN to MAX (included).
107 m4_define([b4_int_type],
108 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
109 b4_ints_in($@, [-128], [127]), [1], [signed char],
111 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
112 b4_ints_in($@, [-32768], [32767]), [1], [short int],
114 m4_eval([0 <= $1]), [1], [unsigned int],
119 # b4_int_type_for(NAME)
120 # ---------------------
121 # Return the smallest int type able to handle numbers ranging from
122 # `NAME_min' to `NAME_max' (included).
123 m4_define([b4_int_type_for],
124 [b4_int_type($1_min, $1_max)])
132 # Return a null pointer constant. NULL infringes on the user name
133 # space in C, so use 0 rather than NULL.
134 m4_define([b4_null], [0])
137 ## ------------------------- ##
138 ## Assigning token numbers. ##
139 ## ------------------------- ##
141 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
142 # -----------------------------------------
143 # Output the definition of this token as #define.
144 m4_define([b4_token_define],
149 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
150 # -------------------------------------------------------
151 # Output the definition of the tokens (if there are) as #defines.
152 m4_define([b4_token_defines],
153 [m4_if([$@], [[]], [],
155 m4_map([b4_token_define], [$@])])
159 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
160 # ---------------------------------------
161 # Output the definition of this token as an enum.
162 m4_define([b4_token_enum],
166 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
167 # -----------------------------------------------------
168 # Output the definition of the tokens (if there are) as enums.
169 m4_define([b4_token_enums],
170 [m4_if([$@], [[]], [],
174 /* Put the tokens into the symbol table, so that GDB and other debuggers
177 m4_map_sep([ b4_token_enum], [,
185 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
186 # -------------------------------------------------------------
187 # Output the definition of the tokens (if there are any) as enums and, if POSIX
188 # Yacc is enabled, as #defines.
189 m4_define([b4_token_enums_defines],
190 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
195 ## --------------------------------------------- ##
196 ## Defining C functions in both K&R and ANSI-C. ##
197 ## --------------------------------------------- ##
202 # A predicate useful in #if to determine whether C is ancient or modern.
204 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
205 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
206 # reasons, but it defines __C99__FUNC__ so check that as well.
207 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
208 # Consider a C++ compiler to be modern if it defines __cplusplus.
210 m4_define([b4_c_modern],
211 [[(defined __STDC__ || defined __C99__FUNC__ \
212 || defined __cplusplus || defined _MSC_VER)]])
214 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
215 # ----------------------------------------------------------
216 # Declare the function NAME.
217 m4_define([b4_c_function_def],
219 b4_c_ansi_function_def($@)
222 $1 (b4_c_knr_formal_names(m4_shiftn(2, $@)))
223 b4_c_knr_formal_decls(m4_shiftn(2, $@))
228 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
229 # ---------------------------------------------------------------
230 # Declare the function NAME in ANSI.
231 m4_define([b4_c_ansi_function_def],
233 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)))[]dnl
237 # b4_c_ansi_formals([DECL1, NAME1], ...)
238 # --------------------------------------
239 # Output the arguments ANSI-C definition.
240 m4_define([b4_c_ansi_formals],
244 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
246 m4_define([b4_c_ansi_formal],
250 # b4_c_knr_formal_names([DECL1, NAME1], ...)
251 # ------------------------------------------
252 # Output the argument names.
253 m4_define([b4_c_knr_formal_names],
254 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
256 m4_define([b4_c_knr_formal_name],
260 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
261 # ------------------------------------------
262 # Output the K&R argument declarations.
263 m4_define([b4_c_knr_formal_decls],
264 [m4_map_sep([b4_c_knr_formal_decl],
269 m4_define([b4_c_knr_formal_decl],
274 ## ------------------------------------------------------------ ##
275 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
276 ## ------------------------------------------------------------ ##
279 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
280 # -----------------------------------------------------------
281 # Declare the function NAME.
282 m4_define([b4_c_function_decl],
283 [#if defined __STDC__ || defined __cplusplus
284 b4_c_ansi_function_decl($@)
291 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
292 # ----------------------------------------------------------------
293 # Declare the function NAME.
294 m4_define([b4_c_ansi_function_decl],
295 [$2 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)));[]dnl
301 ## --------------------- ##
302 ## Calling C functions. ##
303 ## --------------------- ##
306 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
307 # -----------------------------------------------------------
308 # Call the function NAME with arguments NAME1, NAME2 etc.
309 m4_define([b4_c_function_call],
310 [$1 (b4_c_args(m4_shiftn(2, $@)))[]dnl
314 # b4_c_args([DECL1, NAME1], ...)
315 # ------------------------------
316 # Output the arguments NAME1, NAME2...
317 m4_define([b4_c_args],
318 [m4_map_sep([b4_c_arg], [, ], [$@])])
320 m4_define([b4_c_arg],
328 # b4_sync_start(LINE, FILE)
329 # -----------------------
330 m4_define([b4_sync_start], [[#]line $1 $2])
337 # b4_case(LABEL, STATEMENTS)
338 # --------------------------
344 # b4_symbol_actions(FILENAME, LINENO,
345 # SYMBOL-TAG, SYMBOL-NUM,
346 # SYMBOL-ACTION, SYMBOL-TYPENAME)
347 # -------------------------------------------------
348 m4_define([b4_symbol_actions],
349 [m4_pushdef([b4_dollar_dollar],
350 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
351 m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
353 b4_syncline([$2], [$1])
355 b4_syncline([@oline@], [@ofile@])
357 m4_popdef([b4_at_dollar])dnl
358 m4_popdef([b4_dollar_dollar])dnl
362 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
363 # -------------------------------------------
364 # Generate the "yydestruct" function, which declaration is issued using
365 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
366 # or "b4_c_function_def" for K&R.
367 m4_define_default([b4_yydestruct_generate],
368 [[/*-----------------------------------------------.
369 | Release the memory associated to this symbol. |
370 `-----------------------------------------------*/
375 [[const char *yymsg], [yymsg]],
376 [[int yytype], [yytype]],
377 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
378 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
379 m4_ifset([b4_parse_param], [, b4_parse_param]))[
382 ]b4_locations_if([ YYUSE (yylocationp);
384 b4_parse_param_use[]dnl
388 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
392 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
400 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
401 # ------------------------------------------------
402 # Generate the "yy_symbol_print" function, which declaration is issued using
403 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
404 # or "b4_c_function_def" for K&R.
405 m4_define_default([b4_yy_symbol_print_generate],
407 /*--------------------------------.
408 | Print this symbol on YYOUTPUT. |
409 `--------------------------------*/
412 ]$1([yy_symbol_value_print],
414 [[FILE *yyoutput], [yyoutput]],
415 [[int yytype], [yytype]],
416 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
417 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
418 m4_ifset([b4_parse_param], [, b4_parse_param]))[
422 ]b4_locations_if([ YYUSE (yylocationp);
424 b4_parse_param_use[]dnl
426 if (yytype < YYNTOKENS)
427 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
433 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
440 /*--------------------------------.
441 | Print this symbol on YYOUTPUT. |
442 `--------------------------------*/
444 ]$1([yy_symbol_print],
446 [[FILE *yyoutput], [yyoutput]],
447 [[int yytype], [yytype]],
448 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
449 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
450 m4_ifset([b4_parse_param], [, b4_parse_param]))[
452 if (yytype < YYNTOKENS)
453 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
455 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
457 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
458 YYFPRINTF (yyoutput, ": ");
460 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
461 b4_locations_if([, yylocationp])[]b4_user_args[);
462 YYFPRINTF (yyoutput, ")");