1 m4_divert(-1) -*- Autoconf -*-
3 # C M4 Macros for Bison.
4 # Copyright (C) 2002, 2004, 2005, 2006 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 2 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, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 ## ---------------- ##
23 ## ---------------- ##
27 m4_define([b4_comment], [/* m4_bpatsubst([$1], [
33 m4_define([b4_identification],
34 [/* Identify Bison output. */
38 [#]define YYBISON_VERSION "b4_version"
41 [#]define YYSKELETON_NAME b4_skeleton
44 [#]define YYPURE b4_pure_flag
47 [#]define YYPUSH b4_push_flag
49 /* Using locations. */
50 [#]define YYLSP_NEEDED b4_locations_flag
54 ## ---------------- ##
56 ## ---------------- ##
58 # If the %union is not named, its name is YYSTYPE.
59 m4_define_default([b4_union_name], [YYSTYPE])
61 # If the %name-prefix is not given, it is yy.
62 m4_define_default([b4_prefix], [yy])
64 ## ------------------------ ##
65 ## Pure/impure interfaces. ##
66 ## ------------------------ ##
70 m4_define([b4_user_args],
71 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
76 # If defined, b4_parse_param arrives double quoted, but below we prefer
77 # it to be single quoted.
78 m4_define([b4_parse_param],
82 # b4_parse_param_for(DECL, FORMAL, BODY)
83 # ---------------------------------------
84 # Iterate over the user parameters, binding the declaration to DECL,
85 # the formal name to FORMAL, and evaluating the BODY.
86 m4_define([b4_parse_param_for],
87 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
88 [m4_pushdef([$1], m4_fst($1_$2))dnl
89 m4_pushdef([$2], m4_shift($1_$2))dnl
97 # `YYUSE' all the parse-params.
98 m4_define([b4_parse_param_use],
99 [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
108 # b4_int_type(MIN, MAX)
109 # ---------------------
110 # Return the smallest int type able to handle numbers ranging from
111 # MIN to MAX (included).
112 m4_define([b4_int_type],
113 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
114 b4_ints_in($@, [-128], [127]), [1], [signed char],
116 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
117 b4_ints_in($@, [-32768], [32767]), [1], [short int],
119 m4_eval([0 <= $1]), [1], [unsigned int],
124 # b4_int_type_for(NAME)
125 # ---------------------
126 # Return the smallest int type able to handle numbers ranging from
127 # `NAME_min' to `NAME_max' (included).
128 m4_define([b4_int_type_for],
129 [b4_int_type($1_min, $1_max)])
137 # Return a null pointer constant. NULL infringes on the user name
138 # space in C, so use 0 rather than NULL.
139 m4_define([b4_null], [0])
142 ## ------------------------- ##
143 ## Assigning token numbers. ##
144 ## ------------------------- ##
146 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
147 # -----------------------------------------
148 # Output the definition of this token as #define.
149 m4_define([b4_token_define],
154 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
155 # -------------------------------------------------------
156 # Output the definition of the tokens (if there are) as #defines.
157 m4_define([b4_token_defines],
158 [m4_if([$@], [[]], [],
160 m4_map([b4_token_define], [$@])])
164 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
165 # ---------------------------------------
166 # Output the definition of this token as an enum.
167 m4_define([b4_token_enum],
171 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
172 # -----------------------------------------------------
173 # Output the definition of the tokens (if there are) as enums.
174 m4_define([b4_token_enums],
175 [m4_if([$@], [[]], [],
179 /* Put the tokens into the symbol table, so that GDB and other debuggers
182 m4_map_sep([ b4_token_enum], [,
190 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
191 # -------------------------------------------------------------
192 # Output the definition of the tokens (if there are any) as enums and, if POSIX
193 # Yacc is enabled, as #defines.
194 m4_define([b4_token_enums_defines],
195 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
200 ## --------------------------------------------- ##
201 ## Defining C functions in both K&R and ANSI-C. ##
202 ## --------------------------------------------- ##
207 # A predicate useful in #if to determine whether C is ancient or modern.
209 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
210 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
211 # reasons, but it defines __C99__FUNC__ so check that as well.
212 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
213 # Consider a C++ compiler to be modern if it defines __cplusplus.
215 m4_define([b4_c_modern],
216 [[(defined __STDC__ || defined __C99__FUNC__ \
217 || defined __cplusplus || defined _MSC_VER)]])
219 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
220 # ----------------------------------------------------------
221 # Declare the function NAME.
222 m4_define([b4_c_function_def],
224 b4_c_ansi_function_def($@)
227 $1 (b4_c_knr_formal_names(m4_shiftn(2, $@)))
228 b4_c_knr_formal_decls(m4_shiftn(2, $@))
233 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
234 # ---------------------------------------------------------------
235 # Declare the function NAME in ANSI.
236 m4_define([b4_c_ansi_function_def],
238 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)))[]dnl
242 # b4_c_ansi_formals([DECL1, NAME1], ...)
243 # --------------------------------------
244 # Output the arguments ANSI-C definition.
245 m4_define([b4_c_ansi_formals],
249 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
251 m4_define([b4_c_ansi_formal],
255 # b4_c_knr_formal_names([DECL1, NAME1], ...)
256 # ------------------------------------------
257 # Output the argument names.
258 m4_define([b4_c_knr_formal_names],
259 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
261 m4_define([b4_c_knr_formal_name],
265 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
266 # ------------------------------------------
267 # Output the K&R argument declarations.
268 m4_define([b4_c_knr_formal_decls],
269 [m4_map_sep([b4_c_knr_formal_decl],
274 m4_define([b4_c_knr_formal_decl],
279 ## ------------------------------------------------------------ ##
280 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
281 ## ------------------------------------------------------------ ##
284 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
285 # -----------------------------------------------------------
286 # Declare the function NAME.
287 m4_define([b4_c_function_decl],
288 [#if defined __STDC__ || defined __cplusplus
289 b4_c_ansi_function_decl($@)
296 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
297 # ----------------------------------------------------------------
298 # Declare the function NAME.
299 m4_define([b4_c_ansi_function_decl],
300 [$2 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)));[]dnl
306 ## --------------------- ##
307 ## Calling C functions. ##
308 ## --------------------- ##
311 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
312 # -----------------------------------------------------------
313 # Call the function NAME with arguments NAME1, NAME2 etc.
314 m4_define([b4_c_function_call],
315 [$1 (b4_c_args(m4_shiftn(2, $@)))[]dnl
319 # b4_c_args([DECL1, NAME1], ...)
320 # ------------------------------
321 # Output the arguments NAME1, NAME2...
322 m4_define([b4_c_args],
323 [m4_map_sep([b4_c_arg], [, ], [$@])])
325 m4_define([b4_c_arg],
333 # b4_sync_start(LINE, FILE)
334 # -----------------------
335 m4_define([b4_sync_start], [[#]line $1 $2])
342 # b4_case(LABEL, STATEMENTS)
343 # --------------------------
349 # b4_symbol_actions(FILENAME, LINENO,
350 # SYMBOL-TAG, SYMBOL-NUM,
351 # SYMBOL-ACTION, SYMBOL-TYPENAME)
352 # -------------------------------------------------
353 m4_define([b4_symbol_actions],
354 [m4_pushdef([b4_dollar_dollar],
355 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
356 m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
358 b4_syncline([$2], [$1])
360 b4_syncline([@oline@], [@ofile@])
362 m4_popdef([b4_at_dollar])dnl
363 m4_popdef([b4_dollar_dollar])dnl
367 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
368 # -------------------------------------------
369 # Generate the "yydestruct" function, which declaration is issued using
370 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
371 # or "b4_c_function_def" for K&R.
372 m4_define_default([b4_yydestruct_generate],
373 [[/*-----------------------------------------------.
374 | Release the memory associated to this symbol. |
375 `-----------------------------------------------*/
380 [[const char *yymsg], [yymsg]],
381 [[int yytype], [yytype]],
382 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
383 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
384 m4_ifset([b4_parse_param], [, b4_parse_param]))[
387 ]b4_locations_if([ YYUSE (yylocationp);
389 b4_parse_param_use[]dnl
393 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
397 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
405 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
406 # ------------------------------------------------
407 # Generate the "yy_symbol_print" function, which declaration is issued using
408 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
409 # or "b4_c_function_def" for K&R.
410 m4_define_default([b4_yy_symbol_print_generate],
412 /*--------------------------------.
413 | Print this symbol on YYOUTPUT. |
414 `--------------------------------*/
417 ]$1([yy_symbol_value_print],
419 [[FILE *yyoutput], [yyoutput]],
420 [[int yytype], [yytype]],
421 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
422 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
423 m4_ifset([b4_parse_param], [, b4_parse_param]))[
427 ]b4_locations_if([ YYUSE (yylocationp);
429 b4_parse_param_use[]dnl
431 if (yytype < YYNTOKENS)
432 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
438 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
445 /*--------------------------------.
446 | Print this symbol on YYOUTPUT. |
447 `--------------------------------*/
449 ]$1([yy_symbol_print],
451 [[FILE *yyoutput], [yyoutput]],
452 [[int yytype], [yytype]],
453 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
454 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
455 m4_ifset([b4_parse_param], [, b4_parse_param]))[
457 if (yytype < YYNTOKENS)
458 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
460 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
462 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
463 YYFPRINTF (yyoutput, ": ");
465 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
466 b4_locations_if([, yylocationp])[]b4_user_args[);
467 YYFPRINTF (yyoutput, ")");