]> git.saurik.com Git - bison.git/blob - data/c.m4
(b4_symbol_actions): Don't assume %union was used.
[bison.git] / data / c.m4
1 m4_divert(-1) -*- Autoconf -*-
2
3 # C M4 Macros for Bison.
4 # Copyright (C) 2002, 2004 Free Software Foundation, Inc.
5
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.
10
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.
15
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., 59 Temple Place, Suite 330, Boston, MA
19 # 02111-1307 USA
20
21
22 ## ---------------- ##
23 ## Identification. ##
24 ## ---------------- ##
25
26 # b4_copyright(TITLE, YEARS)
27 # --------------------------
28 m4_define([b4_copyright],
29 [/* A Bison parser, made by GNU Bison b4_version. */
30
31 /* $1,
32 Copyright (C) $2 Free Software Foundation, Inc.
33
34 This program is free software; you can redistribute it and/or modify
35 it under the terms of the GNU General Public License as published by
36 the Free Software Foundation; either version 2, or (at your option)
37 any later version.
38
39 This program is distributed in the hope that it will be useful,
40 but WITHOUT ANY WARRANTY; without even the implied warranty of
41 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 GNU General Public License for more details.
43
44 You should have received a copy of the GNU General Public License
45 along with this program; if not, write to the Free Software
46 Foundation, Inc., 59 Temple Place - Suite 330,
47 Boston, MA 02111-1307, USA. */])
48
49
50 # b4_identification
51 # -----------------
52 m4_define([b4_identification],
53 [/* Identify Bison output. */
54 [#]define YYBISON 1
55
56 /* Skeleton name. */
57 [#]define YYSKELETON_NAME b4_skeleton
58
59 /* Pure parsers. */
60 [#]define YYPURE b4_pure
61
62 /* Using locations. */
63 [#]define YYLSP_NEEDED b4_locations_flag
64 ])
65
66
67
68 ## ---------------- ##
69 ## Default values. ##
70 ## ---------------- ##
71
72 m4_define_default([b4_epilogue], [])
73
74
75
76 ## ------------------------ ##
77 ## Pure/impure interfaces. ##
78 ## ------------------------ ##
79
80
81 # b4_user_args
82 # ------------
83 m4_define([b4_user_args],
84 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
85
86
87 # b4_parse_param
88 # --------------
89 # If defined, b4_parse_param arrives double quoted, but below we prefer
90 # it to be single quoted.
91 m4_define_default([b4_parse_param])
92 m4_define([b4_parse_param],
93 b4_parse_param))
94
95
96
97 ## ------------ ##
98 ## Data Types. ##
99 ## ------------ ##
100
101
102 # b4_ints_in(INT1, INT2, LOW, HIGH)
103 # ---------------------------------
104 # Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
105 m4_define([b4_ints_in],
106 [m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
107
108
109 # b4_int_type(MIN, MAX)
110 # ---------------------
111 # Return the smallest int type able to handle numbers ranging from
112 # MIN to MAX (included).
113 m4_define([b4_int_type],
114 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
115 b4_ints_in($@, [-128], [127]), [1], [signed char],
116
117 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
118 b4_ints_in($@, [-32768], [32767]), [1], [short int],
119
120 m4_eval([0 <= $1]), [1], [unsigned int],
121
122 [int])])
123
124
125 # b4_int_type_for(NAME)
126 # ---------------------
127 # Return the smallest int type able to handle numbers ranging from
128 # `NAME_min' to `NAME_max' (included).
129 m4_define([b4_int_type_for],
130 [b4_int_type($1_min, $1_max)])
131
132
133 ## ------------------ ##
134 ## Decoding options. ##
135 ## ------------------ ##
136
137
138 # b4_location_if(IF-TRUE, IF-FALSE)
139 # ---------------------------------
140 # Expand IF-TRUE, if locations are used, IF-FALSE otherwise.
141 m4_define([b4_location_if],
142 [m4_if(b4_locations_flag, [1],
143 [$1],
144 [$2])])
145
146
147 # b4_pure_if(IF-TRUE, IF-FALSE)
148 # -----------------------------
149 # Expand IF-TRUE, if %pure-parser, IF-FALSE otherwise.
150 m4_define([b4_pure_if],
151 [m4_if(b4_pure, [1],
152 [$1],
153 [$2])])
154
155
156
157 ## ------------------------- ##
158 ## Assigning token numbers. ##
159 ## ------------------------- ##
160
161 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
162 # -----------------------------------------
163 # Output the definition of this token as #define.
164 m4_define([b4_token_define],
165 [#define $1 $2
166 ])
167
168
169 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
170 # ---------------------------------------
171 # Output the definition of this token as an enum.
172 m4_define([b4_token_enum],
173 [$1 = $2])
174
175
176 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
177 # -------------------------------------------------------
178 # Output the definition of the tokens (if there are) as enums and #define.
179 m4_define([b4_token_defines],
180 [m4_if([$@], [[]], [],
181 [/* Tokens. */
182 #ifndef YYTOKENTYPE
183 # define YYTOKENTYPE
184 /* Put the tokens into the symbol table, so that GDB and other debuggers
185 know about them. */
186 enum yytokentype {
187 m4_map_sep([ b4_token_enum], [,
188 ],
189 [$@])
190 };
191 #endif
192 m4_map([b4_token_define], [$@])
193 ])
194 ])
195
196
197
198 ## --------------------------------------------- ##
199 ## Defining C functions in both K&R and ANSI-C. ##
200 ## --------------------------------------------- ##
201
202
203 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
204 # ----------------------------------------------------------
205 # Declare the function NAME.
206 m4_define([b4_c_function_def],
207 [#if defined (__STDC__) || defined (__cplusplus)
208 b4_c_ansi_function_def($@)
209 #else
210 $2
211 $1 (b4_c_knr_formal_names(m4_shiftn(2, $@)))
212 b4_c_knr_formal_decls(m4_shiftn(2, $@))
213 #endif[]dnl
214 ])
215
216
217 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
218 # ---------------------------------------------------------------
219 # Declare the function NAME in ANSI.
220 m4_define([b4_c_ansi_function_def],
221 [$2
222 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)))[]dnl
223 ])
224
225
226 # b4_c_ansi_formals([DECL1, NAME1], ...)
227 # --------------------------------------
228 # Output the arguments ANSI-C definition.
229 m4_define([b4_c_ansi_formals],
230 [m4_case([$@],
231 [], [void],
232 [[]], [void],
233 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
234
235 m4_define([b4_c_ansi_formal],
236 [$1])
237
238
239 # b4_c_knr_formal_names([DECL1, NAME1], ...)
240 # ------------------------------------------
241 # Output the argument names.
242 m4_define([b4_c_knr_formal_names],
243 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
244
245 m4_define([b4_c_knr_formal_name],
246 [$2])
247
248
249 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
250 # ------------------------------------------
251 # Output the K&R argument declarations.
252 m4_define([b4_c_knr_formal_decls],
253 [m4_map_sep([b4_c_knr_formal_decl],
254 [
255 ],
256 [$@])])
257
258 m4_define([b4_c_knr_formal_decl],
259 [ $1;])
260
261
262
263 ## ------------------------------------------------------------ ##
264 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
265 ## ------------------------------------------------------------ ##
266
267
268 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
269 # -----------------------------------------------------------
270 # Declare the function NAME.
271 m4_define([b4_c_function_decl],
272 [#if defined (__STDC__) || defined (__cplusplus)
273 b4_c_ansi_function_decl($@)
274 #else
275 $2 $1 ();
276 #endif[]dnl
277 ])
278
279
280 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
281 # ----------------------------------------------------------------
282 # Declare the function NAME.
283 m4_define([b4_c_ansi_function_decl],
284 [$2 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)));[]dnl
285 ])
286
287
288
289
290 ## --------------------- ##
291 ## Calling C functions. ##
292 ## --------------------- ##
293
294
295 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
296 # -----------------------------------------------------------
297 # Call the function NAME with arguments NAME1, NAME2 etc.
298 m4_define([b4_c_function_call],
299 [$1 (b4_c_args(m4_shiftn(2, $@)))[]dnl
300 ])
301
302
303 # b4_c_args([DECL1, NAME1], ...)
304 # ------------------------------
305 # Output the arguments NAME1, NAME2...
306 m4_define([b4_c_args],
307 [m4_map_sep([b4_c_arg], [, ], [$@])])
308
309 m4_define([b4_c_arg],
310 [$2])
311
312
313 ## ----------- ##
314 ## Synclines. ##
315 ## ----------- ##
316
317
318 # b4_syncline(LINE, FILE)
319 # -----------------------
320 m4_define([b4_syncline],
321 [m4_if(b4_synclines_flag, 1,
322 [[#]line $1 $2])])
323
324
325 # b4_symbol_actions(FILENAME, LINENO,
326 # SYMBOL-TAG, SYMBOL-NUM,
327 # SYMBOL-ACTION, SYMBOL-TYPENAME)
328 # -------------------------------------------------
329 m4_define([b4_symbol_actions],
330 [m4_pushdef([b4_dollar_dollar],
331 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
332 m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
333 case $4: /* $3 */
334 b4_syncline([$2], [$1])
335 $5;
336 b4_syncline([@oline@], [@ofile@])
337 break;
338 m4_popdef([b4_at_dollar])dnl
339 m4_popdef([b4_dollar_dollar])dnl
340 ])
341
342
343 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
344 # -------------------------------------------
345 # Generate the "yydestruct" function, which declaration is issued using
346 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
347 # or "b4_c_function_def" for K&R.
348 m4_define([b4_yydestruct_generate],
349 [[/*-----------------------------------------------.
350 | Release the memory associated to this symbol. |
351 `-----------------------------------------------*/
352
353 ]$1([yydestruct],
354 [static void],
355 [[int yytype], [yytype]],
356 [[YYSTYPE *yyvaluep], [yyvaluep]]b4_location_if([,
357 [[YYLTYPE *yylocationp], [yylocationp]]]))[
358 {
359 /* Pacify ``unused variable'' warnings. */
360 (void) yyvaluep;
361 ]b4_location_if([ (void) yylocationp;
362 ])[
363 switch (yytype)
364 {
365 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
366 default:
367 break;
368 }
369 }]dnl
370 ])
371
372
373 # b4_yysymprint_generate(FUNCTION-DECLARATOR)
374 # -------------------------------------------
375 # Generate the "yysymprint" function, which declaration is issued using
376 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
377 # or "b4_c_function_def" for K&R.
378 m4_define([b4_yysymprint_generate],
379 [[/*--------------------------------.
380 | Print this symbol on YYOUTPUT. |
381 `--------------------------------*/
382
383 ]$1([yysymprint],
384 [static void],
385 [[FILE *yyoutput], [yyoutput]],
386 [[int yytype], [yytype]],
387 [[YYSTYPE *yyvaluep], [yyvaluep]]b4_location_if([,
388 [[YYLTYPE *yylocationp], [yylocationp]]]))
389 {
390 /* Pacify ``unused variable'' warnings. */
391 (void) yyvaluep;
392 b4_location_if([ (void) yylocationp;
393 ])dnl
394
395 if (yytype < YYNTOKENS)
396 {
397 YYFPRINTF (yyoutput, "token %s (", yytname[[yytype]]);
398 # ifdef YYPRINT
399 YYPRINT (yyoutput, yytoknum[[yytype]], *yyvaluep);
400 # endif
401 }
402 else
403 YYFPRINTF (yyoutput, "nterm %s (", yytname[[yytype]]);
404
405 switch (yytype)
406 {
407 m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
408 default:
409 break;
410 }
411 YYFPRINTF (yyoutput, ")");
412 }
413 ])