3 # C M4 Macros for Bison.
5 # Copyright (C) 2002, 2004-2012 Free Software Foundation, Inc.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 # Convert STRING into a valid C macro name.
25 [m4_toupper(m4_bpatsubst(m4_quote($1), [[^a-zA-Z0-9]+], [_]))])
30 # A valid C macro name to use as a CPP header guard for FILE.
31 m4_define([b4_cpp_guard],
32 [b4_tocpp(m4_defn([b4_prefix])/[$1])])
35 # b4_cpp_guard_open(FILE)
36 # b4_cpp_guard_close(FILE)
37 # ------------------------
38 # Open/close CPP inclusion guards for FILE.
39 m4_define([b4_cpp_guard_open],
40 [#ifndef b4_cpp_guard([$1])
41 # define b4_cpp_guard([$1])])
43 m4_define([b4_cpp_guard_close],
44 [#endif b4_comment([!b4_cpp_guard([$1])])])
47 ## ---------------- ##
49 ## ---------------- ##
53 m4_define([b4_comment], [/* m4_bpatsubst([$1], [
59 # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
60 # b4_pull_flag if they use the values of the %define variables api.pure or
62 m4_define([b4_identification],
63 [[/* Identify Bison output. */
67 #define YYBISON_VERSION "]b4_version["
70 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
73 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
76 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
79 #define YYPULL ]b4_pull_flag])[
83 ## ---------------- ##
85 ## ---------------- ##
87 # If the %union is not named, its name is YYSTYPE.
88 m4_define_default([b4_union_name], [YYSTYPE])
90 # If the %name-prefix is not given, it is yy.
91 m4_define_default([b4_prefix], [yy])
93 ## ------------------------ ##
94 ## Pure/impure interfaces. ##
95 ## ------------------------ ##
99 m4_define([b4_user_args],
100 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
105 # If defined, b4_parse_param arrives double quoted, but below we prefer
106 # it to be single quoted.
107 m4_define([b4_parse_param],
111 # b4_parse_param_for(DECL, FORMAL, BODY)
112 # ---------------------------------------
113 # Iterate over the user parameters, binding the declaration to DECL,
114 # the formal name to FORMAL, and evaluating the BODY.
115 m4_define([b4_parse_param_for],
116 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
117 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
118 m4_pushdef([$2], m4_shift($1_$2))dnl
126 # `YYUSE' all the parse-params.
127 m4_define([b4_parse_param_use],
128 [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
137 # b4_int_type(MIN, MAX)
138 # ---------------------
139 # Return the smallest int type able to handle numbers ranging from
140 # MIN to MAX (included).
141 m4_define([b4_int_type],
142 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
143 b4_ints_in($@, [-128], [127]), [1], [signed char],
145 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
146 b4_ints_in($@, [-32768], [32767]), [1], [short int],
148 m4_eval([0 <= $1]), [1], [unsigned int],
153 # b4_int_type_for(NAME)
154 # ---------------------
155 # Return the smallest int type able to handle numbers ranging from
156 # `NAME_min' to `NAME_max' (included).
157 m4_define([b4_int_type_for],
158 [b4_int_type($1_min, $1_max)])
161 # b4_table_value_equals(TABLE, VALUE, LITERAL)
162 # --------------------------------------------
163 # Without inducing a comparison warning from the compiler, check if the
164 # literal value LITERAL equals VALUE from table TABLE, which must have
165 # TABLE_min and TABLE_max defined. YYID must be defined as an identity
166 # function that suppresses warnings about constant conditions.
167 m4_define([b4_table_value_equals],
168 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
169 || m4_indir([b4_]$1[_max]) < $3), [1],
171 [[((]$2[) == (]$3[))]])])
181 # Portability issues: define a YY_NULL appropriate for the current
182 # language (C, C++98, or C++11).
183 m4_define([b4_null_define],
185 # if defined __cplusplus && 201103L <= __cplusplus
186 # define YY_NULL nullptr
196 # Return a null pointer constant.
197 m4_define([b4_null], [YY_NULL])
201 ## ------------------------- ##
202 ## Assigning token numbers. ##
203 ## ------------------------- ##
205 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
206 # -----------------------------------------
207 # Output the definition of this token as #define.
208 m4_define([b4_token_define],
213 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
214 # -------------------------------------------------------
215 # Output the definition of the tokens (if there are) as #defines.
216 m4_define([b4_token_defines],
217 [m4_if([$#$1], [1], [],
219 m4_map([b4_token_define], [$@])])
223 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
224 # ---------------------------------------
225 # Output the definition of this token as an enum.
226 m4_define([b4_token_enum],
230 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
231 # -----------------------------------------------------
232 # Output the definition of the tokens (if there are) as enums.
233 m4_define([b4_token_enums],
234 [m4_if([$#$1], [1], [],
238 /* Put the tokens into the symbol table, so that GDB and other debuggers
241 m4_map_sep([ b4_token_enum], [,
249 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
250 # -------------------------------------------------------------
251 # Output the definition of the tokens (if there are any) as enums and, if POSIX
252 # Yacc is enabled, as #defines.
253 m4_define([b4_token_enums_defines],
254 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
259 ## --------------------------------------------- ##
260 ## Defining C functions in both K&R and ANSI-C. ##
261 ## --------------------------------------------- ##
266 # A predicate useful in #if to determine whether C is ancient or modern.
268 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
269 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
270 # reasons, but it defines __C99__FUNC__ so check that as well.
271 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
272 # Consider a C++ compiler to be modern if it defines __cplusplus.
274 m4_define([b4_c_modern],
275 [[(defined __STDC__ || defined __C99__FUNC__ \
276 || defined __cplusplus || defined _MSC_VER)]])
278 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
279 # ----------------------------------------------------------
280 # Declare the function NAME.
281 m4_define([b4_c_function_def],
283 b4_c_ansi_function_def($@)
286 $1 (b4_c_knr_formal_names(m4_shift2($@)))
287 b4_c_knr_formal_decls(m4_shift2($@))
292 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
293 # ---------------------------------------------------------------
294 # Declare the function NAME in ANSI.
295 m4_define([b4_c_ansi_function_def],
297 $1 (b4_c_ansi_formals(m4_shift2($@)))[]dnl
301 # b4_c_ansi_formals([DECL1, NAME1], ...)
302 # --------------------------------------
303 # Output the arguments ANSI-C definition.
304 m4_define([b4_c_ansi_formals],
305 [m4_if([$#], [0], [void],
307 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
309 m4_define([b4_c_ansi_formal],
313 # b4_c_knr_formal_names([DECL1, NAME1], ...)
314 # ------------------------------------------
315 # Output the argument names.
316 m4_define([b4_c_knr_formal_names],
317 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
319 m4_define([b4_c_knr_formal_name],
323 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
324 # ------------------------------------------
325 # Output the K&R argument declarations.
326 m4_define([b4_c_knr_formal_decls],
327 [m4_map_sep([b4_c_knr_formal_decl],
332 m4_define([b4_c_knr_formal_decl],
337 ## ------------------------------------------------------------ ##
338 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
339 ## ------------------------------------------------------------ ##
342 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
343 # -----------------------------------------------------------
344 # Declare the function NAME.
345 m4_define([b4_c_function_decl],
346 [#if defined __STDC__ || defined __cplusplus
347 b4_c_ansi_function_decl($@)
354 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
355 # ----------------------------------------------------------------
356 # Declare the function NAME.
357 m4_define([b4_c_ansi_function_decl],
358 [$2 $1 (b4_c_ansi_formals(m4_shift2($@)));[]dnl
364 ## --------------------- ##
365 ## Calling C functions. ##
366 ## --------------------- ##
369 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
370 # -----------------------------------------------------------
371 # Call the function NAME with arguments NAME1, NAME2 etc.
372 m4_define([b4_c_function_call],
373 [$1 (b4_c_args(m4_shift2($@)))[]dnl
377 # b4_c_args([DECL1, NAME1], ...)
378 # ------------------------------
379 # Output the arguments NAME1, NAME2...
380 m4_define([b4_c_args],
381 [m4_map_sep([b4_c_arg], [, ], [$@])])
383 m4_define([b4_c_arg],
391 # b4_sync_start(LINE, FILE)
392 # -----------------------
393 m4_define([b4_sync_start], [[#]line $1 $2])
400 # b4_case(LABEL, STATEMENTS)
401 # --------------------------
407 # b4_symbol_actions(FILENAME, LINENO,
408 # SYMBOL-TAG, SYMBOL-NUM,
409 # SYMBOL-ACTION, SYMBOL-TYPENAME)
410 # -------------------------------------------------
411 m4_define([b4_symbol_actions],
412 [m4_pushdef([b4_dollar_dollar],
413 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
414 m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
416 b4_syncline([$2], [$1])
418 b4_syncline([@oline@], [@ofile@])
420 m4_popdef([b4_at_dollar])dnl
421 m4_popdef([b4_dollar_dollar])dnl
425 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
426 # -------------------------------------------
427 # Generate the "yydestruct" 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_yydestruct_generate],
431 [[/*-----------------------------------------------.
432 | Release the memory associated to this symbol. |
433 `-----------------------------------------------*/
438 [[const char *yymsg], [yymsg]],
439 [[int yytype], [yytype]],
440 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
441 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
442 m4_ifset([b4_parse_param], [, b4_parse_param]))[
445 ]b4_locations_if([ YYUSE (yylocationp);
447 b4_parse_param_use[]dnl
451 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
455 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
463 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
464 # ------------------------------------------------
465 # Generate the "yy_symbol_print" function, which declaration is issued using
466 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
467 # or "b4_c_function_def" for K&R.
468 m4_define_default([b4_yy_symbol_print_generate],
470 /*--------------------------------.
471 | Print this symbol on YYOUTPUT. |
472 `--------------------------------*/
475 ]$1([yy_symbol_value_print],
477 [[FILE *yyoutput], [yyoutput]],
478 [[int yytype], [yytype]],
479 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
480 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
481 m4_ifset([b4_parse_param], [, b4_parse_param]))[
483 FILE *yyo = yyoutput;
487 ]b4_locations_if([ YYUSE (yylocationp);
489 b4_parse_param_use[]dnl
491 if (yytype < YYNTOKENS)
492 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
498 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
505 /*--------------------------------.
506 | Print this symbol on YYOUTPUT. |
507 `--------------------------------*/
509 ]$1([yy_symbol_print],
511 [[FILE *yyoutput], [yyoutput]],
512 [[int yytype], [yytype]],
513 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
514 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
515 m4_ifset([b4_parse_param], [, b4_parse_param]))[
517 if (yytype < YYNTOKENS)
518 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
520 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
522 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
523 YYFPRINTF (yyoutput, ": ");
525 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
526 b4_locations_if([, yylocationp])[]b4_user_args[);
527 YYFPRINTF (yyoutput, ")");
535 # b4_declare_yylstype
537 # Declarations that might either go into the header (if --defines) or
538 # in the parser body. Declare YYSTYPE/YYLTYPE, and yylval/yylloc.
539 m4_define([b4_declare_yylstype],
540 [[#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
541 ]m4_ifdef([b4_stype],
542 [[typedef union ]b4_union_name[
546 # define YYSTYPE_IS_TRIVIAL 1]],
547 [m4_if(b4_tag_seen_flag, 0,
548 [[typedef int YYSTYPE;
549 # define YYSTYPE_IS_TRIVIAL 1]])])[
550 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
551 # define YYSTYPE_IS_DECLARED 1
552 #endif]b4_locations_if([[
554 #if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED
555 typedef struct YYLTYPE
562 # define yyltype YYLTYPE /* obsolescent; will be withdrawn */
563 # define YYLTYPE_IS_DECLARED 1
564 # define YYLTYPE_IS_TRIVIAL 1
567 b4_pure_if([], [[extern YYSTYPE ]b4_prefix[lval;
568 ]b4_locations_if([[extern YYLTYPE ]b4_prefix[lloc;]])])[]dnl
573 m4_define([b4_declare_yydebug],
574 [[/* Enabling traces. */
576 # define YYDEBUG ]b4_debug_flag[
579 extern int ]b4_prefix[debug;