]> git.saurik.com Git - bison.git/blob - tests/actions.at
Don't put the pre-prologue in the header file. For the yacc.c code
[bison.git] / tests / actions.at
1 # Executing Actions. -*- Autotest -*-
2 # Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 # 02110-1301, USA.
18
19 AT_BANNER([[User Actions.]])
20
21 ## ------------------ ##
22 ## Mid-rule actions. ##
23 ## ------------------ ##
24
25 AT_SETUP([Mid-rule actions])
26
27 # Bison once forgot the mid-rule actions. It was because the action
28 # was attached to the host rule (the one with the mid-rule action),
29 # instead of being attached to the empty rule dedicated to this
30 # action.
31
32 AT_DATA_GRAMMAR([[input.y]],
33 [[%error-verbose
34 %debug
35 %{
36 # include <stdio.h>
37 # include <stdlib.h>
38 static void yyerror (const char *msg);
39 static int yylex (void);
40 %}
41 %%
42 exp: { putchar ('0'); }
43 '1' { putchar ('1'); }
44 '2' { putchar ('2'); }
45 '3' { putchar ('3'); }
46 '4' { putchar ('4'); }
47 '5' { putchar ('5'); }
48 '6' { putchar ('6'); }
49 '7' { putchar ('7'); }
50 '8' { putchar ('8'); }
51 '9' { putchar ('9'); }
52 { putchar ('\n'); }
53 ;
54 %%
55 static int
56 yylex (void)
57 {
58 static const char *input = "123456789";
59 return *input++;
60 }
61
62 static void
63 yyerror (const char *msg)
64 {
65 fprintf (stderr, "%s\n", msg);
66 }
67
68 int
69 main (void)
70 {
71 return yyparse ();
72 }
73 ]])
74
75 AT_CHECK([bison -d -v -o input.c input.y])
76 AT_COMPILE([input])
77 AT_PARSER_CHECK([./input], 0,
78 [[0123456789
79 ]])
80
81 AT_CLEANUP
82
83
84
85
86
87 ## ---------------- ##
88 ## Exotic Dollars. ##
89 ## ---------------- ##
90
91 AT_SETUP([Exotic Dollars])
92
93 AT_DATA_GRAMMAR([[input.y]],
94 [[%error-verbose
95 %debug
96 %{
97 # include <stdio.h>
98 # include <stdlib.h>
99 static void yyerror (const char *msg);
100 static int yylex (void);
101 # define USE(Var)
102 %}
103
104 %union
105 {
106 int val;
107 };
108
109 %type <val> a_1 a_2 a_5
110 sum_of_the_five_previous_values
111
112 %%
113 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
114 sum_of_the_five_previous_values
115 {
116 USE (($1, $2, $<foo>3, $<foo>4, $5));
117 printf ("%d\n", $6);
118 }
119 ;
120 a_1: { $$ = 1; };
121 a_2: { $$ = 2; };
122 a_5: { $$ = 5; };
123
124 sum_of_the_five_previous_values:
125 {
126 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
127 }
128 ;
129
130 %%
131 static int
132 yylex (void)
133 {
134 return EOF;
135 }
136
137 static void
138 yyerror (const char *msg)
139 {
140 fprintf (stderr, "%s\n", msg);
141 }
142
143 int
144 main (void)
145 {
146 return yyparse ();
147 }
148 ]])
149
150 AT_CHECK([bison -d -v -o input.c input.y], 0)
151 AT_COMPILE([input])
152 AT_PARSER_CHECK([./input], 0,
153 [[15
154 ]])
155
156 AT_CLEANUP
157
158
159
160 ## -------------------------- ##
161 ## Printers and Destructors. ##
162 ## -------------------------- ##
163
164 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
165 # -----------------------------------------------------------------------------
166 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
167 [# Make sure complex $n work.
168 m4_if([$1$2$3], $[1]$[2]$[3], [],
169 [m4_fatal([$0: Invalid arguments: $@])])dnl
170
171 # Be sure to pass all the %directives to this macro to have correct
172 # helping macros. So don't put any directly in the Bison file.
173 AT_BISON_OPTION_PUSHDEFS([$5])
174 AT_DATA_GRAMMAR([[input.y]],
175 [[%before-definitions {
176 #include <stdio.h>
177 #include <stdlib.h>
178 #include <assert.h>
179
180 #define YYINITDEPTH 10
181 #define YYMAXDEPTH 10
182 ]AT_LALR1_CC_IF(
183 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
184 [#define RANGE(Location) (Location).first_line, (Location).last_line])
185 [}
186
187 $5]
188 m4_ifval([$6], [%union
189 {
190 int ival;
191 }])
192 AT_LALR1_CC_IF([%define "global_tokens_and_yystype"])
193 m4_ifval([$6], [[%after-definitions {]], [[%before-definitions {]])
194 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;
195 m4_ifval([$6], , [#define YYSTYPE int])])
196 [static int yylex (]AT_LEX_FORMALS[);
197 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
198 [}
199
200 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input])[
201
202 %printer
203 {
204 ]AT_LALR1_CC_IF([debug_stream () << $$;],
205 [fprintf (yyoutput, "%d", $$)])[;
206 }
207 input line thing 'x' 'y'
208
209 %destructor
210 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
211 input
212
213 %destructor
214 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
215 line
216
217 %destructor
218 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
219 thing
220
221 %destructor
222 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
223 'x'
224
225 %destructor
226 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
227 'y'
228
229 %%
230 /*
231 This grammar is made to exercise error recovery.
232 "Lines" starting with `(' support error recovery, with
233 ')' as synchronizing token. Lines starting with 'x' can never
234 be recovered from if in error.
235 */
236
237 input:
238 /* Nothing. */
239 {
240 $$ = 0;
241 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
242 }
243 | line input /* Right recursive to load the stack so that popping at
244 EOF can be exercised. */
245 {
246 $$ = 2;
247 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
248 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
249 }
250 ;
251
252 line:
253 thing thing thing ';'
254 {
255 $$ = $1;
256 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
257 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
258 $3, RANGE (@3), $4, RANGE (@4));
259 }
260 | '(' thing thing ')'
261 {
262 $$ = $1;
263 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
264 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
265 $3, RANGE (@3), $4, RANGE (@4));
266 }
267 | '(' thing ')'
268 {
269 $$ = $1;
270 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
271 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
272 }
273 | '(' error ')'
274 {
275 $$ = -1;
276 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
277 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
278 }
279 ;
280
281 thing:
282 'x'
283 {
284 $$ = $1;
285 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
286 $$, RANGE (@$), $1, RANGE (@1));
287 }
288 ;
289 %%
290 /* Alias to ARGV[1]. */
291 const char *source = 0;
292
293 static int
294 yylex (]AT_LEX_FORMALS[)
295 {
296 static unsigned int counter = 0;
297
298 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
299 /* As in BASIC, line numbers go from 10 to 10. */
300 ]AT_LALR1_CC_IF(
301 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
302 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
303 ],
304 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
305 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
306 ])[
307
308 if (source[c])
309 printf ("sending: '%c'", source[c]);
310 else
311 printf ("sending: EOF");
312 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
313 return source[c];
314 }
315
316 ]AT_LALR1_CC_IF(
317 [/* A C++ error reporting function. */
318 void
319 yy::parser::error (const location& l, const std::string& m)
320 {
321 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
322 }
323
324 static bool yydebug;
325 int
326 yyparse ()
327 {
328 yy::parser parser;
329 parser.set_debug_level (yydebug);
330 return parser.parse ();
331 }
332 ],
333 [static void
334 yyerror (const char *msg)
335 {
336 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
337 }])[
338
339 int
340 main (int argc, const char *argv[])
341 {
342 int status;
343 yydebug = !!getenv ("YYDEBUG");
344 assert (argc == 2);
345 source = argv[1];
346 status = yyparse ();
347 switch (status)
348 {
349 case 0: printf ("Successful parse.\n"); break;
350 case 1: printf ("Parsing FAILED.\n"); break;
351 default: printf ("Parsing FAILED (status %d).\n", status); break;
352 }
353 return status;
354 }
355 ]])
356
357 AT_LALR1_CC_IF(
358 [AT_CHECK([bison -o input.cc input.y])
359 AT_COMPILE_CXX([input])],
360 [AT_CHECK([bison -o input.c input.y])
361 AT_COMPILE([input])])
362
363
364 # Check the location of "empty"
365 # -----------------------------
366 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
367 # an empty "line" nterm.
368 # FIXME: This location is not satisfying. Depend on the lookahead?
369 AT_PARSER_CHECK([./input '(x)'], 0,
370 [[sending: '(' (0@0-9)
371 sending: 'x' (1@10-19)
372 thing (1@10-19): 'x' (1@10-19)
373 sending: ')' (2@20-29)
374 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
375 sending: EOF (3@30-39)
376 input (0@29-29): /* Nothing */
377 input (2@0-29): line (0@0-29) input (0@29-29)
378 Freeing nterm input (2@0-29)
379 Successful parse.
380 ]])
381
382
383 # Check locations in error recovery
384 # ---------------------------------
385 # '(y)' is an error, but can be recovered from. But what's the location
386 # of the error itself ('y'), and of the resulting reduction ('(error)').
387 AT_PARSER_CHECK([./input '(y)'], 0,
388 [[sending: '(' (0@0-9)
389 sending: 'y' (1@10-19)
390 10-19: syntax error, unexpected 'y', expecting 'x'
391 Freeing token 'y' (1@10-19)
392 sending: ')' (2@20-29)
393 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
394 sending: EOF (3@30-39)
395 input (0@29-29): /* Nothing */
396 input (2@0-29): line (-1@0-29) input (0@29-29)
397 Freeing nterm input (2@0-29)
398 Successful parse.
399 ]])
400
401
402 # Syntax errors caught by the parser
403 # ----------------------------------
404 # Exercise the discarding of stack top and input until `error'
405 # can be reduced.
406 #
407 # '(', 'x', 'x', 'x', 'x', 'x', ')',
408 #
409 # Load the stack and provoke an error that cannot be caught by the
410 # grammar, to check that the stack is cleared. And make sure the
411 # lookahead is freed.
412 #
413 # '(', 'x', ')',
414 # '(', 'x', ')',
415 # 'y'
416 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
417 [[sending: '(' (0@0-9)
418 sending: 'x' (1@10-19)
419 thing (1@10-19): 'x' (1@10-19)
420 sending: 'x' (2@20-29)
421 thing (2@20-29): 'x' (2@20-29)
422 sending: 'x' (3@30-39)
423 30-39: syntax error, unexpected 'x', expecting ')'
424 Freeing nterm thing (2@20-29)
425 Freeing nterm thing (1@10-19)
426 Freeing token 'x' (3@30-39)
427 sending: 'x' (4@40-49)
428 Freeing token 'x' (4@40-49)
429 sending: 'x' (5@50-59)
430 Freeing token 'x' (5@50-59)
431 sending: ')' (6@60-69)
432 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
433 sending: '(' (7@70-79)
434 sending: 'x' (8@80-89)
435 thing (8@80-89): 'x' (8@80-89)
436 sending: ')' (9@90-99)
437 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
438 sending: '(' (10@100-109)
439 sending: 'x' (11@110-119)
440 thing (11@110-119): 'x' (11@110-119)
441 sending: ')' (12@120-129)
442 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
443 sending: 'y' (13@130-139)
444 input (0@129-129): /* Nothing */
445 input (2@100-129): line (10@100-129) input (0@129-129)
446 input (2@70-129): line (7@70-99) input (2@100-129)
447 input (2@0-129): line (-1@0-69) input (2@70-129)
448 130-139: syntax error, unexpected 'y', expecting $end
449 Freeing nterm input (2@0-129)
450 Freeing token 'y' (13@130-139)
451 Parsing FAILED.
452 ]])
453
454 # Check destruction upon stack overflow
455 # -------------------------------------
456 # Upon stack overflow, all symbols on the stack should be destroyed.
457 # Only check for yacc.c.
458 AT_YACC_IF([
459 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
460 [[sending: '(' (0@0-9)
461 sending: 'x' (1@10-19)
462 thing (1@10-19): 'x' (1@10-19)
463 sending: ')' (2@20-29)
464 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
465 sending: '(' (3@30-39)
466 sending: 'x' (4@40-49)
467 thing (4@40-49): 'x' (4@40-49)
468 sending: ')' (5@50-59)
469 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
470 sending: '(' (6@60-69)
471 sending: 'x' (7@70-79)
472 thing (7@70-79): 'x' (7@70-79)
473 sending: ')' (8@80-89)
474 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
475 sending: '(' (9@90-99)
476 sending: 'x' (10@100-109)
477 thing (10@100-109): 'x' (10@100-109)
478 sending: ')' (11@110-119)
479 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
480 sending: '(' (12@120-129)
481 sending: 'x' (13@130-139)
482 thing (13@130-139): 'x' (13@130-139)
483 sending: ')' (14@140-149)
484 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
485 sending: '(' (15@150-159)
486 sending: 'x' (16@160-169)
487 thing (16@160-169): 'x' (16@160-169)
488 sending: ')' (17@170-179)
489 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
490 sending: '(' (18@180-189)
491 sending: 'x' (19@190-199)
492 thing (19@190-199): 'x' (19@190-199)
493 sending: ')' (20@200-209)
494 200-209: memory exhausted
495 Freeing nterm thing (19@190-199)
496 Freeing nterm line (15@150-179)
497 Freeing nterm line (12@120-149)
498 Freeing nterm line (9@90-119)
499 Freeing nterm line (6@60-89)
500 Freeing nterm line (3@30-59)
501 Freeing nterm line (0@0-29)
502 Parsing FAILED (status 2).
503 ]])
504 ])
505
506 ])
507
508
509 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
510 # ---------------------------------------------------------------------------
511 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
512 [AT_SETUP([Printers and Destructors $2: $1])
513
514 $3
515 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
516 [%error-verbose
517 %debug
518 %verbose
519 %locations
520 $1], [$2])
521
522 AT_CLEANUP
523 ])
524
525
526 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
527 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
528
529 # These tests currently fail on a Debian GNU/Linux 3.0r2 x86 host,
530 # but the 2nd test succeeds on a Solaris 9 sparc hosts (Forte 7 cc).
531 # Skip them until we figure out what the problem is.
532 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
533 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
534
535 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
536 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])