]> git.saurik.com Git - bison.git/blob - data/c.m4
yysyntax_error: avoid duplicate lookahead collection.
[bison.git] / data / c.m4
1 -*- Autoconf -*-
2
3 # C M4 Macros for Bison.
4 # Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009
5 # Free Software Foundation, Inc.
6
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.
11 #
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.
16 #
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/>.
19
20 ## ---------------- ##
21 ## Identification. ##
22 ## ---------------- ##
23
24 # b4_comment_(TEXT, OPEN, CONTINUE, END)
25 # -------------------------------------
26 # Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
27 # Avoid adding indentation to the first line, as the indentation comes
28 # from OPEN. That's why we don't patsubst([$1], [^\(.\)], [ \1]).
29 #
30 # Prefix all the output lines with PREFIX.
31 m4_define([b4_comment_], [$2[]m4_bpatsubst([$1], [
32 \(.\)], [
33 $3\1])$4])
34
35
36 # b4_c_comment(TEXT, [PREFIX])
37 # ----------------------------
38 # Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
39 # Avoid adding indentation to the first line, as the indentation comes
40 # from "/*". That's why we don't patsubst([$1], [^\(.\)], [ \1]).
41 #
42 # Prefix all the output lines with PREFIX.
43 m4_define([b4_c_comment],
44 [b4_comment_([$1], [$2/* ], [$2 ], [$2 */])])
45
46
47 # b4_comment(TEXT, [PREFIX])
48 # --------------------------
49 # By default, C comments.
50 m4_define([b4_comment], [b4_c_comment($@)])
51
52
53 # b4_identification
54 # -----------------
55 # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
56 # b4_pull_flag if they use the values of the %define variables api.pure or
57 # api.push-pull.
58 m4_define([b4_identification],
59 [[/* Identify Bison output. */
60 #define YYBISON 1
61
62 /* Bison version. */
63 #define YYBISON_VERSION "]b4_version["
64
65 /* Skeleton name. */
66 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
67
68 /* Pure parsers. */
69 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
70
71 /* Push parsers. */
72 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
73
74 /* Pull parsers. */
75 #define YYPULL ]b4_pull_flag])[
76
77 /* Using locations. */
78 #define YYLSP_NEEDED ]b4_locations_if([1], [0])[
79 ]])
80
81
82 ## ---------------- ##
83 ## Default values. ##
84 ## ---------------- ##
85
86 # If the %union is not named, its name is YYSTYPE.
87 m4_define_default([b4_union_name], [YYSTYPE])
88
89 # If the %name-prefix is not given, it is yy.
90 m4_define_default([b4_prefix], [yy])
91
92 ## ------------------------ ##
93 ## Pure/impure interfaces. ##
94 ## ------------------------ ##
95
96 # b4_user_args
97 # ------------
98 m4_define([b4_user_args],
99 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
100
101
102 # b4_parse_param
103 # --------------
104 # If defined, b4_parse_param arrives double quoted, but below we prefer
105 # it to be single quoted.
106 m4_define([b4_parse_param],
107 b4_parse_param)
108
109
110 # b4_parse_param_for(DECL, FORMAL, BODY)
111 # ---------------------------------------
112 # Iterate over the user parameters, binding the declaration to DECL,
113 # the formal name to FORMAL, and evaluating the BODY.
114 m4_define([b4_parse_param_for],
115 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
116 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
117 m4_pushdef([$2], m4_shift($1_$2))dnl
118 $3[]dnl
119 m4_popdef([$2])dnl
120 m4_popdef([$1])dnl
121 ])])
122
123 # b4_parse_param_use
124 # ------------------
125 # `YYUSE' all the parse-params.
126 m4_define([b4_parse_param_use],
127 [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
128 ])dnl
129 ])
130
131
132 ## ------------ ##
133 ## Data Types. ##
134 ## ------------ ##
135
136 # b4_int_type(MIN, MAX)
137 # ---------------------
138 # Return the smallest int type able to handle numbers ranging from
139 # MIN to MAX (included).
140 m4_define([b4_int_type],
141 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
142 b4_ints_in($@, [-128], [127]), [1], [signed char],
143
144 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
145 b4_ints_in($@, [-32768], [32767]), [1], [short int],
146
147 m4_eval([0 <= $1]), [1], [unsigned int],
148
149 [int])])
150
151
152 # b4_int_type_for(NAME)
153 # ---------------------
154 # Return the smallest int type able to handle numbers ranging from
155 # `NAME_min' to `NAME_max' (included).
156 m4_define([b4_int_type_for],
157 [b4_int_type($1_min, $1_max)])
158
159
160 # b4_table_value_equals(TABLE, VALUE, LITERAL)
161 # --------------------------------------------
162 # Without inducing a comparison warning from the compiler, check if the
163 # literal value LITERAL equals VALUE from table TABLE, which must have
164 # TABLE_min and TABLE_max defined. YYID must be defined as an identity
165 # function that suppresses warnings about constant conditions.
166 m4_define([b4_table_value_equals],
167 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
168 || m4_indir([b4_]$1[_max]) < $3), [1],
169 [[YYID (0)]],
170 [[((]$2[) == (]$3[))]])])
171
172 ## ---------##
173 ## Values. ##
174 ## ---------##
175
176 # b4_null
177 ---------
178 # Return a null pointer constant. NULL infringes on the user name
179 # space in C, so use 0 rather than NULL.
180 m4_define([b4_null], [0])
181
182
183 # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
184 # -------------------------------------------------------------
185 # Define "yy<TABLE-NAME>" which contents is CONTENT.
186 m4_define([b4_integral_parser_table_define],
187 [m4_ifval([$3], [b4_c_comment([$3], [ ])
188 ])dnl
189 static const b4_int_type_for([$2]) yy$1[[]] =
190 {
191 $2
192 };dnl
193 ])
194
195
196 ## ------------------------- ##
197 ## Assigning token numbers. ##
198 ## ------------------------- ##
199
200 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
201 # -----------------------------------------
202 # Output the definition of this token as #define.
203 m4_define([b4_token_define],
204 [#define b4_percent_define_get([api.tokens.prefix])$1 $2
205 ])
206
207
208 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
209 # -------------------------------------------------------
210 # Output the definition of the tokens (if there are) as #defines.
211 m4_define([b4_token_defines],
212 [m4_if([$#$1], [1], [],
213 [/* Tokens. */
214 m4_map([b4_token_define], [$@])])
215 ])
216
217
218 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
219 # ---------------------------------------
220 # Output the definition of this token as an enum.
221 m4_define([b4_token_enum],
222 [b4_percent_define_get([api.tokens.prefix])$1 = $2])
223
224
225 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
226 # -----------------------------------------------------
227 # Output the definition of the tokens (if there are) as enums.
228 m4_define([b4_token_enums],
229 [m4_if([$#$1], [1], [],
230 [/* Tokens. */
231 #ifndef YYTOKENTYPE
232 # define YYTOKENTYPE
233 /* Put the tokens into the symbol table, so that GDB and other debuggers
234 know about them. */
235 enum yytokentype {
236 m4_map_sep([ b4_token_enum], [,
237 ],
238 [$@])
239 };
240 #endif
241 ])])
242
243
244 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
245 # -------------------------------------------------------------
246 # Output the definition of the tokens (if there are any) as enums and, if POSIX
247 # Yacc is enabled, as #defines.
248 m4_define([b4_token_enums_defines],
249 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
250 ])
251
252
253 ## ----------------- ##
254 ## Semantic Values. ##
255 ## ----------------- ##
256
257
258 # b4_symbol_value(VAL, [TYPE])
259 # ----------------------------
260 # Given a semantic value VAL ($$, $1 etc.), extract its value of type
261 # TYPE if TYPE is given, otherwise just return VAL. The result can be
262 # used safetly, it is put in parens to avoid nasty precedence issues.
263 # TYPE is *not* put in braces, provide some if needed.
264 m4_define([b4_symbol_value],
265 [($1[]m4_ifval([$2], [.$2]))])
266
267
268
269 ## --------------------------------------------- ##
270 ## Defining C functions in both K&R and ANSI-C. ##
271 ## --------------------------------------------- ##
272
273
274 # b4_modern_c
275 # -----------
276 # A predicate useful in #if to determine whether C is ancient or modern.
277 #
278 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
279 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
280 # reasons, but it defines __C99__FUNC__ so check that as well.
281 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
282 # Consider a C++ compiler to be modern if it defines __cplusplus.
283 #
284 m4_define([b4_c_modern],
285 [[(defined __STDC__ || defined __C99__FUNC__ \
286 || defined __cplusplus || defined _MSC_VER)]])
287
288 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
289 # ----------------------------------------------------------
290 # Declare the function NAME.
291 m4_define([b4_c_function_def],
292 [#if b4_c_modern
293 b4_c_ansi_function_def($@)
294 #else
295 $2
296 $1 (b4_c_knr_formal_names(m4_shift2($@)))
297 b4_c_knr_formal_decls(m4_shift2($@))
298 #endif[]dnl
299 ])
300
301
302 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
303 # ---------------------------------------------------------------
304 # Declare the function NAME in ANSI.
305 m4_define([b4_c_ansi_function_def],
306 [$2
307 $1 (b4_c_ansi_formals(m4_shift2($@)))[]dnl
308 ])
309
310
311 # b4_c_ansi_formals([DECL1, NAME1], ...)
312 # --------------------------------------
313 # Output the arguments ANSI-C definition.
314 m4_define([b4_c_ansi_formals],
315 [m4_if([$#], [0], [void],
316 [$#$1], [1], [void],
317 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
318
319 m4_define([b4_c_ansi_formal],
320 [$1])
321
322
323 # b4_c_knr_formal_names([DECL1, NAME1], ...)
324 # ------------------------------------------
325 # Output the argument names.
326 m4_define([b4_c_knr_formal_names],
327 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
328
329 m4_define([b4_c_knr_formal_name],
330 [$2])
331
332
333 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
334 # ------------------------------------------
335 # Output the K&R argument declarations.
336 m4_define([b4_c_knr_formal_decls],
337 [m4_map_sep([b4_c_knr_formal_decl],
338 [
339 ],
340 [$@])])
341
342 m4_define([b4_c_knr_formal_decl],
343 [ $1;])
344
345
346
347 ## ------------------------------------------------------------ ##
348 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
349 ## ------------------------------------------------------------ ##
350
351
352 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
353 # -----------------------------------------------------------
354 # Declare the function NAME.
355 m4_define([b4_c_function_decl],
356 [#if b4_c_modern
357 b4_c_ansi_function_decl($@)
358 #else
359 $2 $1 ();
360 #endif[]dnl
361 ])
362
363
364 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
365 # ----------------------------------------------------------------
366 # Declare the function NAME.
367 m4_define([b4_c_ansi_function_decl],
368 [$2 $1 (b4_c_ansi_formals(m4_shift2($@)));[]dnl
369 ])
370
371
372
373
374 ## --------------------- ##
375 ## Calling C functions. ##
376 ## --------------------- ##
377
378
379 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
380 # -----------------------------------------------------------
381 # Call the function NAME with arguments NAME1, NAME2 etc.
382 m4_define([b4_c_function_call],
383 [$1 (b4_c_args(m4_shift2($@)))[]dnl
384 ])
385
386
387 # b4_c_args([DECL1, NAME1], ...)
388 # ------------------------------
389 # Output the arguments NAME1, NAME2...
390 m4_define([b4_c_args],
391 [m4_map_sep([b4_c_arg], [, ], [$@])])
392
393 m4_define([b4_c_arg],
394 [$2])
395
396
397 ## ----------- ##
398 ## Synclines. ##
399 ## ----------- ##
400
401 # b4_sync_start(LINE, FILE)
402 # -----------------------
403 m4_define([b4_sync_start], [[#]line $1 $2])
404
405
406 ## -------------- ##
407 ## User actions. ##
408 ## -------------- ##
409
410 # b4_case(LABEL, STATEMENTS)
411 # --------------------------
412 m4_define([b4_case],
413 [ case $1:
414 $2
415 b4_syncline([@oline@], [@ofile@])
416 break;])
417
418
419 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
420 # -------------------------------------------
421 # Generate the "yydestruct" function, which declaration is issued using
422 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
423 # or "b4_c_function_def" for K&R.
424 m4_define_default([b4_yydestruct_generate],
425 [[/*-----------------------------------------------.
426 | Release the memory associated to this symbol. |
427 `-----------------------------------------------*/
428
429 /*ARGSUSED*/
430 ]$1([yydestruct],
431 [static void],
432 [[const char *yymsg], [yymsg]],
433 [[int yytype], [yytype]],
434 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
435 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
436 m4_ifset([b4_parse_param], [, b4_parse_param]))[
437 {
438 YYUSE (yyvaluep);
439 ]b4_locations_if([ YYUSE (yylocationp);
440 ])dnl
441 b4_parse_param_use[]dnl
442 [
443 if (!yymsg)
444 yymsg = "Deleting";
445 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
446
447 switch (yytype)
448 {
449 ]b4_symbol_foreach([b4_symbol_destructor])dnl
450 [ default:
451 break;
452 }
453 }]dnl
454 ])
455
456
457 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
458 # ------------------------------------------------
459 # Generate the "yy_symbol_print" function, which declaration is issued using
460 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
461 # or "b4_c_function_def" for K&R.
462 m4_define_default([b4_yy_symbol_print_generate],
463 [[
464 /*--------------------------------.
465 | Print this symbol on YYOUTPUT. |
466 `--------------------------------*/
467
468 /*ARGSUSED*/
469 ]$1([yy_symbol_value_print],
470 [static void],
471 [[FILE *yyoutput], [yyoutput]],
472 [[int yytype], [yytype]],
473 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
474 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
475 m4_ifset([b4_parse_param], [, b4_parse_param]))[
476 {
477 if (!yyvaluep)
478 return;
479 ]b4_locations_if([ YYUSE (yylocationp);
480 ])dnl
481 b4_parse_param_use[]dnl
482 [# ifdef YYPRINT
483 if (yytype < YYNTOKENS)
484 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
485 # else
486 YYUSE (yyoutput);
487 # endif
488 switch (yytype)
489 {
490 ]b4_symbol_foreach([b4_symbol_printer])dnl
491 [ default:
492 break;
493 }
494 }
495
496
497 /*--------------------------------.
498 | Print this symbol on YYOUTPUT. |
499 `--------------------------------*/
500
501 ]$1([yy_symbol_print],
502 [static void],
503 [[FILE *yyoutput], [yyoutput]],
504 [[int yytype], [yytype]],
505 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
506 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
507 m4_ifset([b4_parse_param], [, b4_parse_param]))[
508 {
509 if (yytype < YYNTOKENS)
510 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
511 else
512 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
513
514 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
515 YYFPRINTF (yyoutput, ": ");
516 ])dnl
517 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
518 b4_locations_if([, yylocationp])[]b4_user_args[);
519 YYFPRINTF (yyoutput, ")");
520 }]dnl
521 ])