]> git.saurik.com Git - bison.git/blob - tests/actions.at
fad7f77bac3f4e25891234de9f8781152a779fe3
[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 [[%start-header {
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], [[%end-header {]], [[%after-header {]])
194 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
195 [static int yylex (]AT_LEX_FORMALS[);
196 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
197 [}
198
199 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
200
201 /* FIXME: This %printer isn't actually tested. */
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 %token END 0
230 %destructor
231 { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
232 END
233
234 %%
235 /*
236 This grammar is made to exercise error recovery.
237 "Lines" starting with `(' support error recovery, with
238 ')' as synchronizing token. Lines starting with 'x' can never
239 be recovered from if in error.
240 */
241
242 input:
243 /* Nothing. */
244 {
245 $$ = 0;
246 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
247 }
248 | line input /* Right recursive to load the stack so that popping at
249 END can be exercised. */
250 {
251 $$ = 2;
252 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
253 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
254 }
255 ;
256
257 line:
258 thing thing thing ';'
259 {
260 $$ = $1;
261 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
262 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
263 $3, RANGE (@3), $4, RANGE (@4));
264 }
265 | '(' thing thing ')'
266 {
267 $$ = $1;
268 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
269 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
270 $3, RANGE (@3), $4, RANGE (@4));
271 }
272 | '(' thing ')'
273 {
274 $$ = $1;
275 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
276 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
277 }
278 | '(' error ')'
279 {
280 $$ = -1;
281 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
282 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
283 }
284 ;
285
286 thing:
287 'x'
288 {
289 $$ = $1;
290 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
291 $$, RANGE (@$), $1, RANGE (@1));
292 }
293 ;
294 %%
295 /* Alias to ARGV[1]. */
296 const char *source = 0;
297
298 static int
299 yylex (]AT_LEX_FORMALS[)
300 {
301 static unsigned int counter = 0;
302
303 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
304 /* As in BASIC, line numbers go from 10 to 10. */
305 ]AT_LALR1_CC_IF(
306 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
307 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
308 ],
309 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
310 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
311 ])[
312
313 if (source[c])
314 printf ("sending: '%c'", source[c]);
315 else
316 printf ("sending: END");
317 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
318 return source[c];
319 }
320
321 ]AT_LALR1_CC_IF(
322 [/* A C++ error reporting function. */
323 void
324 yy::parser::error (const location& l, const std::string& m)
325 {
326 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
327 }
328
329 static bool yydebug;
330 int
331 yyparse ()
332 {
333 yy::parser parser;
334 parser.set_debug_level (yydebug);
335 return parser.parse ();
336 }
337 ],
338 [static void
339 yyerror (const char *msg)
340 {
341 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
342 }])[
343
344 int
345 main (int argc, const char *argv[])
346 {
347 int status;
348 yydebug = !!getenv ("YYDEBUG");
349 assert (argc == 2);
350 source = argv[1];
351 status = yyparse ();
352 switch (status)
353 {
354 case 0: printf ("Successful parse.\n"); break;
355 case 1: printf ("Parsing FAILED.\n"); break;
356 default: printf ("Parsing FAILED (status %d).\n", status); break;
357 }
358 return status;
359 }
360 ]])
361
362 AT_LALR1_CC_IF(
363 [AT_CHECK([bison -o input.cc input.y])
364 AT_COMPILE_CXX([input])],
365 [AT_CHECK([bison -o input.c input.y])
366 AT_COMPILE([input])])
367
368
369 # Check the location of "empty"
370 # -----------------------------
371 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
372 # an empty "line" nterm.
373 # FIXME: This location is not satisfying. Depend on the lookahead?
374 AT_PARSER_CHECK([./input '(x)'], 0,
375 [[sending: '(' (0@0-9)
376 sending: 'x' (1@10-19)
377 thing (1@10-19): 'x' (1@10-19)
378 sending: ')' (2@20-29)
379 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
380 sending: END (3@30-39)
381 input (0@29-29): /* Nothing */
382 input (2@0-29): line (0@0-29) input (0@29-29)
383 Freeing token END (3@30-39)
384 Freeing nterm input (2@0-29)
385 Successful parse.
386 ]])
387
388
389 # Check locations in error recovery
390 # ---------------------------------
391 # '(y)' is an error, but can be recovered from. But what's the location
392 # of the error itself ('y'), and of the resulting reduction ('(error)').
393 AT_PARSER_CHECK([./input '(y)'], 0,
394 [[sending: '(' (0@0-9)
395 sending: 'y' (1@10-19)
396 10-19: syntax error, unexpected 'y', expecting 'x'
397 Freeing token 'y' (1@10-19)
398 sending: ')' (2@20-29)
399 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
400 sending: END (3@30-39)
401 input (0@29-29): /* Nothing */
402 input (2@0-29): line (-1@0-29) input (0@29-29)
403 Freeing token END (3@30-39)
404 Freeing nterm input (2@0-29)
405 Successful parse.
406 ]])
407
408
409 # Syntax errors caught by the parser
410 # ----------------------------------
411 # Exercise the discarding of stack top and input until `error'
412 # can be reduced.
413 #
414 # '(', 'x', 'x', 'x', 'x', 'x', ')',
415 #
416 # Load the stack and provoke an error that cannot be caught by the
417 # grammar, to check that the stack is cleared. And make sure the
418 # lookahead is freed.
419 #
420 # '(', 'x', ')',
421 # '(', 'x', ')',
422 # 'y'
423 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
424 [[sending: '(' (0@0-9)
425 sending: 'x' (1@10-19)
426 thing (1@10-19): 'x' (1@10-19)
427 sending: 'x' (2@20-29)
428 thing (2@20-29): 'x' (2@20-29)
429 sending: 'x' (3@30-39)
430 30-39: syntax error, unexpected 'x', expecting ')'
431 Freeing nterm thing (2@20-29)
432 Freeing nterm thing (1@10-19)
433 Freeing token 'x' (3@30-39)
434 sending: 'x' (4@40-49)
435 Freeing token 'x' (4@40-49)
436 sending: 'x' (5@50-59)
437 Freeing token 'x' (5@50-59)
438 sending: ')' (6@60-69)
439 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
440 sending: '(' (7@70-79)
441 sending: 'x' (8@80-89)
442 thing (8@80-89): 'x' (8@80-89)
443 sending: ')' (9@90-99)
444 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
445 sending: '(' (10@100-109)
446 sending: 'x' (11@110-119)
447 thing (11@110-119): 'x' (11@110-119)
448 sending: ')' (12@120-129)
449 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
450 sending: 'y' (13@130-139)
451 input (0@129-129): /* Nothing */
452 input (2@100-129): line (10@100-129) input (0@129-129)
453 input (2@70-129): line (7@70-99) input (2@100-129)
454 input (2@0-129): line (-1@0-69) input (2@70-129)
455 130-139: syntax error, unexpected 'y', expecting END
456 Freeing nterm input (2@0-129)
457 Freeing token 'y' (13@130-139)
458 Parsing FAILED.
459 ]])
460
461
462 # Syntax error caught by the parser where lookahead = END
463 # --------------------------------------------------------
464 # Load the stack and provoke an error that cannot be caught by the
465 # grammar, to check that the stack is cleared. And make sure the
466 # lookahead is freed.
467 #
468 # '(', 'x', ')',
469 # '(', 'x', ')',
470 # 'x'
471 AT_PARSER_CHECK([./input '(x)(x)x'], 1,
472 [[sending: '(' (0@0-9)
473 sending: 'x' (1@10-19)
474 thing (1@10-19): 'x' (1@10-19)
475 sending: ')' (2@20-29)
476 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
477 sending: '(' (3@30-39)
478 sending: 'x' (4@40-49)
479 thing (4@40-49): 'x' (4@40-49)
480 sending: ')' (5@50-59)
481 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
482 sending: 'x' (6@60-69)
483 thing (6@60-69): 'x' (6@60-69)
484 sending: END (7@70-79)
485 70-79: syntax error, unexpected END, expecting 'x'
486 Freeing nterm thing (6@60-69)
487 Freeing nterm line (3@30-59)
488 Freeing nterm line (0@0-29)
489 Freeing token END (7@70-79)
490 Parsing FAILED.
491 ]])
492
493
494 # Check destruction upon stack overflow
495 # -------------------------------------
496 # Upon stack overflow, all symbols on the stack should be destroyed.
497 # Only check for yacc.c.
498 AT_YACC_IF([
499 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
500 [[sending: '(' (0@0-9)
501 sending: 'x' (1@10-19)
502 thing (1@10-19): 'x' (1@10-19)
503 sending: ')' (2@20-29)
504 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
505 sending: '(' (3@30-39)
506 sending: 'x' (4@40-49)
507 thing (4@40-49): 'x' (4@40-49)
508 sending: ')' (5@50-59)
509 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
510 sending: '(' (6@60-69)
511 sending: 'x' (7@70-79)
512 thing (7@70-79): 'x' (7@70-79)
513 sending: ')' (8@80-89)
514 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
515 sending: '(' (9@90-99)
516 sending: 'x' (10@100-109)
517 thing (10@100-109): 'x' (10@100-109)
518 sending: ')' (11@110-119)
519 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
520 sending: '(' (12@120-129)
521 sending: 'x' (13@130-139)
522 thing (13@130-139): 'x' (13@130-139)
523 sending: ')' (14@140-149)
524 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
525 sending: '(' (15@150-159)
526 sending: 'x' (16@160-169)
527 thing (16@160-169): 'x' (16@160-169)
528 sending: ')' (17@170-179)
529 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
530 sending: '(' (18@180-189)
531 sending: 'x' (19@190-199)
532 thing (19@190-199): 'x' (19@190-199)
533 sending: ')' (20@200-209)
534 200-209: memory exhausted
535 Freeing nterm thing (19@190-199)
536 Freeing nterm line (15@150-179)
537 Freeing nterm line (12@120-149)
538 Freeing nterm line (9@90-119)
539 Freeing nterm line (6@60-89)
540 Freeing nterm line (3@30-59)
541 Freeing nterm line (0@0-29)
542 Parsing FAILED (status 2).
543 ]])
544 ])
545
546 ])
547
548
549 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
550 # ---------------------------------------------------------------------------
551 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
552 [AT_SETUP([Printers and Destructors $2: $1])
553
554 $3
555 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
556 [%error-verbose
557 %debug
558 %verbose
559 %locations
560 $1], [$2])
561
562 AT_CLEANUP
563 ])
564
565
566 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
567 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
568
569 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
570 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
571
572 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
573 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
574
575
576
577 ## --------------------------------- ##
578 ## Default %printer and %destructor. ##
579 ## --------------------------------- ##
580
581 # Check that the right %printer and %destructor are called, that they're not
582 # called for $end, and that $$ and @$ work correctly.
583
584 AT_SETUP([Default %printer and %destructor])
585
586 AT_DATA_GRAMMAR([[input.y]],
587 [[%error-verbose
588 %debug
589 %locations
590 %initial-action {
591 @$.first_line = @$.last_line = 1;
592 @$.first_column = @$.last_column = 1;
593 }
594
595 %{
596 # include <stdio.h>
597 # include <stdlib.h>
598 static void yyerror (const char *msg);
599 static int yylex (void);
600 # define USE(SYM)
601 %}
602
603 %printer {
604 fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column);
605 } %symbol-default
606 %destructor {
607 fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column);
608 } %symbol-default
609
610 %printer {
611 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
612 } 'b' 'c'
613 %destructor {
614 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
615 } 'b' 'c'
616
617 %%
618
619 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
620
621 %%
622
623 static int
624 yylex (void)
625 {
626 static const char *input = "abcd";
627 static int column = 1;
628 yylval = *input++;
629 yylloc.first_line = yylloc.last_line = 1;
630 yylloc.first_column = yylloc.last_column = column++;
631 return yylval;
632 }
633
634 static void
635 yyerror (const char *msg)
636 {
637 fprintf (stderr, "%s\n", msg);
638 }
639
640 int
641 main (void)
642 {
643 yydebug = 1;
644 return yyparse ();
645 }
646 ]])
647
648 AT_CHECK([bison -o input.c input.y])
649 AT_COMPILE([input])
650 AT_PARSER_CHECK([./input], 1,
651 [[Default destructor for 'd' @ 4.
652 'b'/'c' destructor for 'c' @ 3.
653 'b'/'c' destructor for 'b' @ 2.
654 Default destructor for 'a' @ 1.
655 ]],
656 [[Starting parse
657 Entering state 0
658 Reading a token: Next token is token 'a' (1.1-1.1: Default printer for 'a' @ 1)
659 Shifting token 'a' (1.1-1.1: Default printer for 'a' @ 1)
660 Entering state 1
661 Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
662 Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
663 Entering state 3
664 Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
665 Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
666 Entering state 5
667 Reading a token: Next token is token 'd' (1.4-1.4: Default printer for 'd' @ 4)
668 Shifting token 'd' (1.4-1.4: Default printer for 'd' @ 4)
669 Entering state 6
670 Reading a token: Now at end of input.
671 syntax error, unexpected $end, expecting 'e'
672 Error: popping token 'd' (1.4-1.4: Default printer for 'd' @ 4)
673 Stack now 0 1 3 5
674 Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
675 Stack now 0 1 3
676 Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
677 Stack now 0 1
678 Error: popping token 'a' (1.1-1.1: Default printer for 'a' @ 1)
679 Stack now 0
680 Cleanup: discarding lookahead token $end (1.5-1.5: )
681 Stack now 0
682 ]])
683
684 AT_CLEANUP
685
686
687
688 ## ----------------------------------- ##
689 ## Per-type %printer and %destructor. ##
690 ## ----------------------------------- ##
691
692 AT_SETUP([Per-type %printer and %destructor])
693
694 AT_DATA_GRAMMAR([[input.y]],
695 [[%error-verbose
696 %debug
697
698 %{
699 # include <stdio.h>
700 # include <stdlib.h>
701 static void yyerror (const char *msg);
702 static int yylex (void);
703 # define USE(SYM)
704 %}
705
706 %union { int field0; int field1; int field2; }
707 %type <field0> start 'a' 'g'
708 %type <field1> 'e'
709 %type <field2> 'f'
710 %printer {
711 fprintf (yyoutput, "%%symbol-default/<field2>/e printer");
712 } %symbol-default 'e' <field2>
713 %destructor {
714 fprintf (stdout, "%%symbol-default/<field2>/e destructor.\n");
715 } %symbol-default 'e' <field2>
716
717 %type <field1> 'b'
718 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
719 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
720
721 %type <field0> 'c'
722 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
723 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
724
725 %type <field1> 'd'
726 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
727 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
728
729 %%
730
731 start:
732 'a' 'b' 'c' 'd' 'e' 'f' 'g'
733 {
734 USE(($1, $2, $3, $4, $5, $6, $7));
735 $$ = 'S';
736 }
737 ;
738
739 %%
740
741 static int
742 yylex (void)
743 {
744 static const char *input = "abcdef";
745 return *input++;
746 }
747
748 static void
749 yyerror (const char *msg)
750 {
751 fprintf (stderr, "%s\n", msg);
752 }
753
754 int
755 main (void)
756 {
757 yydebug = 1;
758 return yyparse ();
759 }
760 ]])
761
762 AT_CHECK([bison -o input.c input.y])
763 AT_COMPILE([input])
764 AT_PARSER_CHECK([./input], 1,
765 [[%symbol-default/<field2>/e destructor.
766 %symbol-default/<field2>/e destructor.
767 'd' destructor.
768 'c' destructor.
769 <field1> destructor.
770 %symbol-default/<field2>/e destructor.
771 ]],
772 [[Starting parse
773 Entering state 0
774 Reading a token: Next token is token 'a' (%symbol-default/<field2>/e printer)
775 Shifting token 'a' (%symbol-default/<field2>/e printer)
776 Entering state 1
777 Reading a token: Next token is token 'b' (<field1> printer)
778 Shifting token 'b' (<field1> printer)
779 Entering state 3
780 Reading a token: Next token is token 'c' ('c' printer)
781 Shifting token 'c' ('c' printer)
782 Entering state 5
783 Reading a token: Next token is token 'd' ('d' printer)
784 Shifting token 'd' ('d' printer)
785 Entering state 6
786 Reading a token: Next token is token 'e' (%symbol-default/<field2>/e printer)
787 Shifting token 'e' (%symbol-default/<field2>/e printer)
788 Entering state 7
789 Reading a token: Next token is token 'f' (%symbol-default/<field2>/e printer)
790 Shifting token 'f' (%symbol-default/<field2>/e printer)
791 Entering state 8
792 Reading a token: Now at end of input.
793 syntax error, unexpected $end, expecting 'g'
794 Error: popping token 'f' (%symbol-default/<field2>/e printer)
795 Stack now 0 1 3 5 6 7
796 Error: popping token 'e' (%symbol-default/<field2>/e printer)
797 Stack now 0 1 3 5 6
798 Error: popping token 'd' ('d' printer)
799 Stack now 0 1 3 5
800 Error: popping token 'c' ('c' printer)
801 Stack now 0 1 3
802 Error: popping token 'b' (<field1> printer)
803 Stack now 0 1
804 Error: popping token 'a' (%symbol-default/<field2>/e printer)
805 Stack now 0
806 Cleanup: discarding lookahead token $end ()
807 Stack now 0
808 ]])
809
810 AT_CLEANUP
811
812
813
814 ## ------------------------------------------------------------- ##
815 ## Default %printer and %destructor for user-defined end token. ##
816 ## ------------------------------------------------------------- ##
817
818 AT_SETUP([Default %printer and %destructor for user-defined end token])
819
820 AT_DATA_GRAMMAR([[input.y]],
821 [[%error-verbose
822 %debug
823 %locations
824 %initial-action {
825 @$.first_line = @$.last_line = 1;
826 @$.first_column = @$.last_column = 1;
827 }
828
829 %{
830 # include <stdio.h>
831 # include <stdlib.h>
832 static void yyerror (const char *msg);
833 static int yylex (void);
834 # define USE(SYM)
835 %}
836
837 %token END 0
838 %printer {
839 fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column);
840 } %symbol-default
841 %destructor {
842 fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column);
843 } %symbol-default
844
845 %%
846
847 start: { $$ = 'S'; } ;
848
849 %%
850
851 static int
852 yylex (void)
853 {
854 yylval = 'E';
855 yylloc.first_line = yylloc.last_line = 1;
856 yylloc.first_column = yylloc.last_column = 1;
857 return 0;
858 }
859
860 static void
861 yyerror (const char *msg)
862 {
863 fprintf (stderr, "%s\n", msg);
864 }
865
866 int
867 main (void)
868 {
869 yydebug = 1;
870 return yyparse ();
871 }
872 ]])
873
874 AT_CHECK([bison -o input.c input.y])
875 AT_COMPILE([input])
876 AT_PARSER_CHECK([./input], 0,
877 [[Default destructor for 'E' @ 1.
878 Default destructor for 'S' @ 1.
879 ]],
880 [[Starting parse
881 Entering state 0
882 Reducing stack by rule 1 (line 37):
883 -> $$ = nterm start (1.1-1.1: Default printer for 'S' @ 1)
884 Stack now 0
885 Entering state 1
886 Reading a token: Now at end of input.
887 Shifting token END (1.1-1.1: Default printer for 'E' @ 1)
888 Entering state 2
889 Stack now 0 1 2
890 Cleanup: popping token END (1.1-1.1: Default printer for 'E' @ 1)
891 Cleanup: popping nterm start (1.1-1.1: Default printer for 'S' @ 1)
892 ]])
893
894 AT_CLEANUP
895
896
897
898 ## ------------------------------------------------------------------ ##
899 ## Default %printer and %destructor are not for error or $undefined. ##
900 ## ------------------------------------------------------------------ ##
901
902 AT_SETUP([Default %printer and %destructor are not for error or \$undefined])
903
904 # If Bison were to apply the default %printer and %destructor to the error
905 # token or to $undefined:
906 # - For the error token:
907 # - It would generate warnings for unused $n.
908 # - It would invoke the %printer and %destructor on the error token's
909 # semantic value, which would be initialized from the lookahead, which
910 # would be destroyed separately.
911 # - For $undefined, who knows what the semantic value would be.
912
913 AT_DATA_GRAMMAR([[input.y]],
914 [[%debug
915
916 %{
917 # include <stdio.h>
918 static void yyerror (const char *msg);
919 static int yylex (void);
920 # define USE(SYM)
921 %}
922
923 %printer {
924 fprintf (yyoutput, "'%c'", $$);
925 } %symbol-default
926 %destructor {
927 fprintf (stderr, "DESTROY '%c'\n", $$);
928 } %symbol-default
929
930 %%
931
932 start:
933 { $$ = 'S'; }
934 /* In order to reveal the problems that this bug caused during parsing, add
935 * $2 to USE. */
936 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
937 ;
938
939 %%
940
941 static int
942 yylex (void)
943 {
944 static const char *input = "abd";
945 yylval = *input++;
946 return yylval;
947 }
948
949 static void
950 yyerror (const char *msg)
951 {
952 fprintf (stderr, "%s\n", msg);
953 }
954
955 int
956 main (void)
957 {
958 yydebug = 1;
959 return yyparse ();
960 }
961 ]])
962
963 AT_CHECK([bison -o input.c input.y])
964 AT_COMPILE([input])
965 AT_PARSER_CHECK([./input], [1], [],
966 [[Starting parse
967 Entering state 0
968 Reading a token: Next token is token 'a' ('a')
969 Shifting token 'a' ('a')
970 Entering state 1
971 Reading a token: Next token is token 'b' ('b')
972 syntax error
973 Shifting token error ()
974 Entering state 3
975 Next token is token 'b' ('b')
976 Shifting token 'b' ('b')
977 Entering state 5
978 Reading a token: Next token is token $undefined ()
979 Error: popping token 'b' ('b')
980 DESTROY 'b'
981 Stack now 0 1 3
982 Error: popping token error ()
983 Stack now 0 1
984 Shifting token error ()
985 Entering state 3
986 Next token is token $undefined ()
987 Error: discarding token $undefined ()
988 Error: popping token error ()
989 Stack now 0 1
990 Shifting token error ()
991 Entering state 3
992 Reading a token: Now at end of input.
993 Cleanup: discarding lookahead token $end ()
994 Stack now 0 1 3
995 Cleanup: popping token error ()
996 Cleanup: popping token 'a' ('a')
997 DESTROY 'a'
998 ]])
999
1000 AT_CLEANUP
1001
1002
1003
1004 ## ------------------------------------------------------ ##
1005 ## Default %printer and %destructor are not for $accept. ##
1006 ## ------------------------------------------------------ ##
1007
1008 AT_SETUP([Default %printer and %destructor are not for \$accept])
1009
1010 # If YYSTYPE is a union and Bison were to apply the default %printer and
1011 # %destructor to $accept:
1012 # - The %printer and %destructor code generated for $accept would always be
1013 # dead code because $accept is currently never shifted onto the stack.
1014 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1015 # to declare `%type <field> $accept'. (Also true for $undefined.)
1016 # - Thus, the compiler might complain that the user code assumes the wrong
1017 # type for $$ since the code might assume the type associated with a
1018 # specific union field, which is especially reasonable in C++ since that
1019 # type may be a base type. This test case checks for this problem. (Also
1020 # true for $undefined and the error token, so there are three warnings for
1021 # %printer and three for %destructor.)
1022
1023 AT_DATA_GRAMMAR([[input.y]],
1024 [[%debug /* So that %printer is actually compiled. */
1025
1026 %{
1027 # include <stdio.h>
1028 static void yyerror (const char *msg);
1029 static int yylex (void);
1030 # define USE(SYM)
1031 %}
1032
1033 %printer {
1034 char chr = $$;
1035 fprintf (yyoutput, "'%c'", chr);
1036 } %symbol-default
1037 %destructor {
1038 char chr = $$;
1039 fprintf (stderr, "DESTROY '%c'\n", chr);
1040 } %symbol-default
1041
1042 %union { char chr; }
1043 %type <chr> start
1044
1045 %%
1046
1047 start: { USE($$); } ;
1048
1049 %%
1050
1051 static int
1052 yylex (void)
1053 {
1054 return 0;
1055 }
1056
1057 static void
1058 yyerror (const char *msg)
1059 {
1060 fprintf (stderr, "%s\n", msg);
1061 }
1062
1063 int
1064 main (void)
1065 {
1066 return yyparse ();
1067 }
1068 ]])
1069
1070 AT_CHECK([bison -o input.c input.y])
1071 AT_COMPILE([input])
1072
1073 AT_CLEANUP