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
50 [#]define YYPULL b4_pull_flag
52 /* Using locations. */
53 [#]define YYLSP_NEEDED b4_locations_flag
57 ## ---------------- ##
59 ## ---------------- ##
61 # If the %union is not named, its name is YYSTYPE.
62 m4_define_default([b4_union_name], [YYSTYPE])
64 # If the %name-prefix is not given, it is yy.
65 m4_define_default([b4_prefix], [yy])
67 ## ------------------------ ##
68 ## Pure/impure interfaces. ##
69 ## ------------------------ ##
73 m4_define([b4_user_args],
74 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
79 # If defined, b4_parse_param arrives double quoted, but below we prefer
80 # it to be single quoted.
81 m4_define([b4_parse_param],
85 # b4_parse_param_for(DECL, FORMAL, BODY)
86 # ---------------------------------------
87 # Iterate over the user parameters, binding the declaration to DECL,
88 # the formal name to FORMAL, and evaluating the BODY.
89 m4_define([b4_parse_param_for],
90 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
91 [m4_pushdef([$1], m4_fst($1_$2))dnl
92 m4_pushdef([$2], m4_shift($1_$2))dnl
100 # `YYUSE' all the parse-params.
101 m4_define([b4_parse_param_use],
102 [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
111 # b4_int_type(MIN, MAX)
112 # ---------------------
113 # Return the smallest int type able to handle numbers ranging from
114 # MIN to MAX (included).
115 m4_define([b4_int_type],
116 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
117 b4_ints_in($@, [-128], [127]), [1], [signed char],
119 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
120 b4_ints_in($@, [-32768], [32767]), [1], [short int],
122 m4_eval([0 <= $1]), [1], [unsigned int],
127 # b4_int_type_for(NAME)
128 # ---------------------
129 # Return the smallest int type able to handle numbers ranging from
130 # `NAME_min' to `NAME_max' (included).
131 m4_define([b4_int_type_for],
132 [b4_int_type($1_min, $1_max)])
140 # Return a null pointer constant. NULL infringes on the user name
141 # space in C, so use 0 rather than NULL.
142 m4_define([b4_null], [0])
145 ## ------------------------- ##
146 ## Assigning token numbers. ##
147 ## ------------------------- ##
149 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
150 # -----------------------------------------
151 # Output the definition of this token as #define.
152 m4_define([b4_token_define],
157 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
158 # -------------------------------------------------------
159 # Output the definition of the tokens (if there are) as #defines.
160 m4_define([b4_token_defines],
161 [m4_if([$@], [[]], [],
163 m4_map([b4_token_define], [$@])])
167 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
168 # ---------------------------------------
169 # Output the definition of this token as an enum.
170 m4_define([b4_token_enum],
174 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
175 # -----------------------------------------------------
176 # Output the definition of the tokens (if there are) as enums.
177 m4_define([b4_token_enums],
178 [m4_if([$@], [[]], [],
182 /* Put the tokens into the symbol table, so that GDB and other debuggers
185 m4_map_sep([ b4_token_enum], [,
193 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
194 # -------------------------------------------------------------
195 # Output the definition of the tokens (if there are any) as enums and, if POSIX
196 # Yacc is enabled, as #defines.
197 m4_define([b4_token_enums_defines],
198 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
203 ## --------------------------------------------- ##
204 ## Defining C functions in both K&R and ANSI-C. ##
205 ## --------------------------------------------- ##
210 # A predicate useful in #if to determine whether C is ancient or modern.
212 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
213 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
214 # reasons, but it defines __C99__FUNC__ so check that as well.
215 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
216 # Consider a C++ compiler to be modern if it defines __cplusplus.
218 m4_define([b4_c_modern],
219 [[(defined __STDC__ || defined __C99__FUNC__ \
220 || defined __cplusplus || defined _MSC_VER)]])
222 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
223 # ----------------------------------------------------------
224 # Declare the function NAME.
225 m4_define([b4_c_function_def],
227 b4_c_ansi_function_def($@)
230 $1 (b4_c_knr_formal_names(m4_shiftn(2, $@)))
231 b4_c_knr_formal_decls(m4_shiftn(2, $@))
236 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
237 # ---------------------------------------------------------------
238 # Declare the function NAME in ANSI.
239 m4_define([b4_c_ansi_function_def],
241 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)))[]dnl
245 # b4_c_ansi_formals([DECL1, NAME1], ...)
246 # --------------------------------------
247 # Output the arguments ANSI-C definition.
248 m4_define([b4_c_ansi_formals],
252 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
254 m4_define([b4_c_ansi_formal],
258 # b4_c_knr_formal_names([DECL1, NAME1], ...)
259 # ------------------------------------------
260 # Output the argument names.
261 m4_define([b4_c_knr_formal_names],
262 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
264 m4_define([b4_c_knr_formal_name],
268 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
269 # ------------------------------------------
270 # Output the K&R argument declarations.
271 m4_define([b4_c_knr_formal_decls],
272 [m4_map_sep([b4_c_knr_formal_decl],
277 m4_define([b4_c_knr_formal_decl],
282 ## ------------------------------------------------------------ ##
283 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
284 ## ------------------------------------------------------------ ##
287 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
288 # -----------------------------------------------------------
289 # Declare the function NAME.
290 m4_define([b4_c_function_decl],
291 [#if defined __STDC__ || defined __cplusplus
292 b4_c_ansi_function_decl($@)
299 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
300 # ----------------------------------------------------------------
301 # Declare the function NAME.
302 m4_define([b4_c_ansi_function_decl],
303 [$2 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)));[]dnl
309 ## --------------------- ##
310 ## Calling C functions. ##
311 ## --------------------- ##
314 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
315 # -----------------------------------------------------------
316 # Call the function NAME with arguments NAME1, NAME2 etc.
317 m4_define([b4_c_function_call],
318 [$1 (b4_c_args(m4_shiftn(2, $@)))[]dnl
322 # b4_c_args([DECL1, NAME1], ...)
323 # ------------------------------
324 # Output the arguments NAME1, NAME2...
325 m4_define([b4_c_args],
326 [m4_map_sep([b4_c_arg], [, ], [$@])])
328 m4_define([b4_c_arg],
336 # b4_sync_start(LINE, FILE)
337 # -----------------------
338 m4_define([b4_sync_start], [[#]line $1 $2])
345 # b4_case(LABEL, STATEMENTS)
346 # --------------------------
352 # b4_symbol_actions(FILENAME, LINENO,
353 # SYMBOL-TAG, SYMBOL-NUM,
354 # SYMBOL-ACTION, SYMBOL-TYPENAME)
355 # -------------------------------------------------
356 m4_define([b4_symbol_actions],
357 [m4_pushdef([b4_dollar_dollar],
358 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
359 m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
361 b4_syncline([$2], [$1])
363 b4_syncline([@oline@], [@ofile@])
365 m4_popdef([b4_at_dollar])dnl
366 m4_popdef([b4_dollar_dollar])dnl
370 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
371 # -------------------------------------------
372 # Generate the "yydestruct" function, which declaration is issued using
373 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
374 # or "b4_c_function_def" for K&R.
375 m4_define_default([b4_yydestruct_generate],
376 [[/*-----------------------------------------------.
377 | Release the memory associated to this symbol. |
378 `-----------------------------------------------*/
383 [[const char *yymsg], [yymsg]],
384 [[int yytype], [yytype]],
385 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
386 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
387 m4_ifset([b4_parse_param], [, b4_parse_param]))[
390 ]b4_locations_if([ YYUSE (yylocationp);
392 b4_parse_param_use[]dnl
396 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
400 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
408 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
409 # ------------------------------------------------
410 # Generate the "yy_symbol_print" function, which declaration is issued using
411 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
412 # or "b4_c_function_def" for K&R.
413 m4_define_default([b4_yy_symbol_print_generate],
415 /*--------------------------------.
416 | Print this symbol on YYOUTPUT. |
417 `--------------------------------*/
420 ]$1([yy_symbol_value_print],
422 [[FILE *yyoutput], [yyoutput]],
423 [[int yytype], [yytype]],
424 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
425 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
426 m4_ifset([b4_parse_param], [, b4_parse_param]))[
430 ]b4_locations_if([ YYUSE (yylocationp);
432 b4_parse_param_use[]dnl
434 if (yytype < YYNTOKENS)
435 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
441 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
448 /*--------------------------------.
449 | Print this symbol on YYOUTPUT. |
450 `--------------------------------*/
452 ]$1([yy_symbol_print],
454 [[FILE *yyoutput], [yyoutput]],
455 [[int yytype], [yytype]],
456 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
457 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
458 m4_ifset([b4_parse_param], [, b4_parse_param]))[
460 if (yytype < YYNTOKENS)
461 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
463 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
465 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
466 YYFPRINTF (yyoutput, ": ");
468 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
469 b4_locations_if([, yylocationp])[]b4_user_args[);
470 YYFPRINTF (yyoutput, ")");