]> git.saurik.com Git - bison.git/blob - tests/actions.at
Replace m4/warning.m4 with warnings module.
[bison.git] / tests / actions.at
1 # Executing Actions. -*- Autotest -*-
2 # Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
3 # Foundation, Inc.
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 AT_BANNER([[User Actions.]])
19
20 ## ------------------ ##
21 ## Mid-rule actions. ##
22 ## ------------------ ##
23
24 AT_SETUP([Mid-rule actions])
25
26 # Bison once forgot the mid-rule actions. It was because the action
27 # was attached to the host rule (the one with the mid-rule action),
28 # instead of being attached to the empty rule dedicated to this
29 # action.
30
31 AT_DATA_GRAMMAR([[input.y]],
32 [[%error-verbose
33 %debug
34 %{
35 # include <stdio.h>
36 # include <stdlib.h>
37 static void yyerror (const char *msg);
38 static int yylex (void);
39 %}
40 %%
41 exp: { putchar ('0'); }
42 '1' { putchar ('1'); }
43 '2' { putchar ('2'); }
44 '3' { putchar ('3'); }
45 '4' { putchar ('4'); }
46 '5' { putchar ('5'); }
47 '6' { putchar ('6'); }
48 '7' { putchar ('7'); }
49 '8' { putchar ('8'); }
50 '9' { putchar ('9'); }
51 { putchar ('\n'); }
52 ;
53 %%
54 static int
55 yylex (void)
56 {
57 static char const input[] = "123456789";
58 static size_t toknum;
59 if (! (toknum < sizeof input))
60 abort ();
61 return input[toknum++];
62 }
63
64 static void
65 yyerror (const char *msg)
66 {
67 fprintf (stderr, "%s\n", msg);
68 }
69
70 int
71 main (void)
72 {
73 return yyparse ();
74 }
75 ]])
76
77 AT_BISON_CHECK([-d -v -o input.c input.y])
78 AT_COMPILE([input])
79 AT_PARSER_CHECK([./input], 0,
80 [[0123456789
81 ]])
82
83 AT_CLEANUP
84
85
86
87
88
89 ## ---------------- ##
90 ## Exotic Dollars. ##
91 ## ---------------- ##
92
93 AT_SETUP([Exotic Dollars])
94
95 AT_DATA_GRAMMAR([[input.y]],
96 [[%error-verbose
97 %debug
98 %{
99 # include <stdio.h>
100 # include <stdlib.h>
101 static void yyerror (const char *msg);
102 static int yylex (void);
103 # define USE(Var)
104 %}
105
106 %union
107 {
108 int val;
109 };
110
111 %type <val> a_1 a_2 a_5
112 sum_of_the_five_previous_values
113
114 %%
115 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
116 sum_of_the_five_previous_values
117 {
118 USE (($1, $2, $<foo>3, $<foo>4, $5));
119 printf ("%d\n", $6);
120 }
121 ;
122 a_1: { $$ = 1; };
123 a_2: { $$ = 2; };
124 a_5: { $$ = 5; };
125
126 sum_of_the_five_previous_values:
127 {
128 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
129 }
130 ;
131
132 %%
133 static int
134 yylex (void)
135 {
136 static int called;
137 if (called++)
138 abort ();
139 return EOF;
140 }
141
142 static void
143 yyerror (const char *msg)
144 {
145 fprintf (stderr, "%s\n", msg);
146 }
147
148 int
149 main (void)
150 {
151 return yyparse ();
152 }
153 ]])
154
155 AT_BISON_CHECK([-d -v -o input.c input.y], 0)
156 AT_COMPILE([input])
157 AT_PARSER_CHECK([./input], 0,
158 [[15
159 ]])
160
161 AT_CLEANUP
162
163
164
165 ## -------------------------- ##
166 ## Printers and Destructors. ##
167 ## -------------------------- ##
168
169 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
170 # -----------------------------------------------------------------------------
171 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
172 [# Make sure complex $n work.
173 m4_if([$1$2$3], $[1]$[2]$[3], [],
174 [m4_fatal([$0: Invalid arguments: $@])])dnl
175
176 # Be sure to pass all the %directives to this macro to have correct
177 # helping macros. So don't put any directly in the Bison file.
178 AT_BISON_OPTION_PUSHDEFS([$5])
179 AT_DATA_GRAMMAR([[input.y]],
180 [[%code requires {
181 #include <stdio.h>
182 #include <stdlib.h>
183 #include <string.h>
184 #include <assert.h>
185
186 #define YYINITDEPTH 10
187 #define YYMAXDEPTH 10
188 ]AT_LALR1_CC_IF(
189 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
190 [#define RANGE(Location) (Location).first_line, (Location).last_line])
191 [}
192
193 $5]
194 m4_ifval([$6], [%union
195 {
196 int ival;
197 }])
198 AT_LALR1_CC_IF([%define global_tokens_and_yystype])
199 m4_ifval([$6], [[%code provides {]], [[%code {]])
200 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
201 [static int yylex (]AT_LEX_FORMALS[);
202 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
203 [}
204
205 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
206
207 /* FIXME: This %printer isn't actually tested. */
208 %printer
209 {
210 ]AT_LALR1_CC_IF([debug_stream () << $$;],
211 [fprintf (yyoutput, "%d", $$)])[;
212 }
213 input line thing 'x' 'y'
214
215 %destructor
216 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
217 input
218
219 %destructor
220 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
221 line
222
223 %destructor
224 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
225 thing
226
227 %destructor
228 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
229 'x'
230
231 %destructor
232 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
233 'y'
234
235 %token END 0
236 %destructor
237 { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
238 END
239
240 %%
241 /*
242 This grammar is made to exercise error recovery.
243 "Lines" starting with `(' support error recovery, with
244 ')' as synchronizing token. Lines starting with 'x' can never
245 be recovered from if in error.
246 */
247
248 input:
249 /* Nothing. */
250 {
251 $$ = 0;
252 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
253 }
254 | line input /* Right recursive to load the stack so that popping at
255 END can be exercised. */
256 {
257 $$ = 2;
258 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
259 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
260 }
261 ;
262
263 line:
264 thing thing thing ';'
265 {
266 $$ = $1;
267 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
268 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
269 $3, RANGE (@3), $4, RANGE (@4));
270 }
271 | '(' thing thing ')'
272 {
273 $$ = $1;
274 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
275 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
276 $3, RANGE (@3), $4, RANGE (@4));
277 }
278 | '(' thing ')'
279 {
280 $$ = $1;
281 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
282 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
283 }
284 | '(' error ')'
285 {
286 $$ = -1;
287 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
288 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
289 }
290 ;
291
292 thing:
293 'x'
294 {
295 $$ = $1;
296 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
297 $$, RANGE (@$), $1, RANGE (@1));
298 }
299 ;
300 %%
301 /* Alias to ARGV[1]. */
302 const char *source = 0;
303
304 static int
305 yylex (]AT_LEX_FORMALS[)
306 {
307 static unsigned int counter = 0;
308
309 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
310 /* As in BASIC, line numbers go from 10 to 10. */
311 ]AT_LALR1_CC_IF(
312 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
313 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
314 ],
315 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
316 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
317 ])[
318
319 if (! (0 <= c && c <= strlen (source)))
320 abort ();
321 if (source[c])
322 printf ("sending: '%c'", source[c]);
323 else
324 printf ("sending: END");
325 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
326 return source[c];
327 }
328
329 ]AT_LALR1_CC_IF(
330 [/* A C++ error reporting function. */
331 void
332 yy::parser::error (const location& l, const std::string& m)
333 {
334 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
335 }
336
337 static bool yydebug;
338 int
339 yyparse ()
340 {
341 yy::parser parser;
342 parser.set_debug_level (yydebug);
343 return parser.parse ();
344 }
345 ],
346 [static void
347 yyerror (const char *msg)
348 {
349 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
350 }])[
351
352 int
353 main (int argc, const char *argv[])
354 {
355 int status;
356 yydebug = !!getenv ("YYDEBUG");
357 assert (argc == 2);
358 source = argv[1];
359 status = yyparse ();
360 switch (status)
361 {
362 case 0: printf ("Successful parse.\n"); break;
363 case 1: printf ("Parsing FAILED.\n"); break;
364 default: printf ("Parsing FAILED (status %d).\n", status); break;
365 }
366 return status;
367 }
368 ]])
369
370 AT_LALR1_CC_IF(
371 [AT_BISON_CHECK([-o input.cc input.y])
372 AT_COMPILE_CXX([input])],
373 [AT_BISON_CHECK([-o input.c input.y])
374 AT_COMPILE([input])])
375
376
377 # Check the location of "empty"
378 # -----------------------------
379 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
380 # an empty "line" nterm.
381 # FIXME: This location is not satisfying. Depend on the lookahead?
382 AT_PARSER_CHECK([./input '(x)'], 0,
383 [[sending: '(' (0@0-9)
384 sending: 'x' (1@10-19)
385 thing (1@10-19): 'x' (1@10-19)
386 sending: ')' (2@20-29)
387 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
388 sending: END (3@30-39)
389 input (0@29-29): /* Nothing */
390 input (2@0-29): line (0@0-29) input (0@29-29)
391 Freeing token END (3@30-39)
392 Freeing nterm input (2@0-29)
393 Successful parse.
394 ]])
395
396
397 # Check locations in error recovery
398 # ---------------------------------
399 # '(y)' is an error, but can be recovered from. But what's the location
400 # of the error itself ('y'), and of the resulting reduction ('(error)').
401 AT_PARSER_CHECK([./input '(y)'], 0,
402 [[sending: '(' (0@0-9)
403 sending: 'y' (1@10-19)
404 10-19: syntax error, unexpected 'y', expecting 'x'
405 Freeing token 'y' (1@10-19)
406 sending: ')' (2@20-29)
407 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
408 sending: END (3@30-39)
409 input (0@29-29): /* Nothing */
410 input (2@0-29): line (-1@0-29) input (0@29-29)
411 Freeing token END (3@30-39)
412 Freeing nterm input (2@0-29)
413 Successful parse.
414 ]])
415
416
417 # Syntax errors caught by the parser
418 # ----------------------------------
419 # Exercise the discarding of stack top and input until `error'
420 # can be reduced.
421 #
422 # '(', 'x', 'x', 'x', 'x', 'x', ')',
423 #
424 # Load the stack and provoke an error that cannot be caught by the
425 # grammar, to check that the stack is cleared. And make sure the
426 # lookahead is freed.
427 #
428 # '(', 'x', ')',
429 # '(', 'x', ')',
430 # 'y'
431 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
432 [[sending: '(' (0@0-9)
433 sending: 'x' (1@10-19)
434 thing (1@10-19): 'x' (1@10-19)
435 sending: 'x' (2@20-29)
436 thing (2@20-29): 'x' (2@20-29)
437 sending: 'x' (3@30-39)
438 30-39: syntax error, unexpected 'x', expecting ')'
439 Freeing nterm thing (2@20-29)
440 Freeing nterm thing (1@10-19)
441 Freeing token 'x' (3@30-39)
442 sending: 'x' (4@40-49)
443 Freeing token 'x' (4@40-49)
444 sending: 'x' (5@50-59)
445 Freeing token 'x' (5@50-59)
446 sending: ')' (6@60-69)
447 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
448 sending: '(' (7@70-79)
449 sending: 'x' (8@80-89)
450 thing (8@80-89): 'x' (8@80-89)
451 sending: ')' (9@90-99)
452 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
453 sending: '(' (10@100-109)
454 sending: 'x' (11@110-119)
455 thing (11@110-119): 'x' (11@110-119)
456 sending: ')' (12@120-129)
457 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
458 sending: 'y' (13@130-139)
459 input (0@129-129): /* Nothing */
460 input (2@100-129): line (10@100-129) input (0@129-129)
461 input (2@70-129): line (7@70-99) input (2@100-129)
462 input (2@0-129): line (-1@0-69) input (2@70-129)
463 130-139: syntax error, unexpected 'y', expecting END
464 Freeing nterm input (2@0-129)
465 Freeing token 'y' (13@130-139)
466 Parsing FAILED.
467 ]])
468
469
470 # Syntax error caught by the parser where lookahead = END
471 # --------------------------------------------------------
472 # Load the stack and provoke an error that cannot be caught by the
473 # grammar, to check that the stack is cleared. And make sure the
474 # lookahead is freed.
475 #
476 # '(', 'x', ')',
477 # '(', 'x', ')',
478 # 'x'
479 AT_PARSER_CHECK([./input '(x)(x)x'], 1,
480 [[sending: '(' (0@0-9)
481 sending: 'x' (1@10-19)
482 thing (1@10-19): 'x' (1@10-19)
483 sending: ')' (2@20-29)
484 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
485 sending: '(' (3@30-39)
486 sending: 'x' (4@40-49)
487 thing (4@40-49): 'x' (4@40-49)
488 sending: ')' (5@50-59)
489 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
490 sending: 'x' (6@60-69)
491 thing (6@60-69): 'x' (6@60-69)
492 sending: END (7@70-79)
493 70-79: syntax error, unexpected END, expecting 'x'
494 Freeing nterm thing (6@60-69)
495 Freeing nterm line (3@30-59)
496 Freeing nterm line (0@0-29)
497 Freeing token END (7@70-79)
498 Parsing FAILED.
499 ]])
500
501
502 # Check destruction upon stack overflow
503 # -------------------------------------
504 # Upon stack overflow, all symbols on the stack should be destroyed.
505 # Only check for yacc.c.
506 AT_YACC_IF([
507 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
508 [[sending: '(' (0@0-9)
509 sending: 'x' (1@10-19)
510 thing (1@10-19): 'x' (1@10-19)
511 sending: ')' (2@20-29)
512 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
513 sending: '(' (3@30-39)
514 sending: 'x' (4@40-49)
515 thing (4@40-49): 'x' (4@40-49)
516 sending: ')' (5@50-59)
517 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
518 sending: '(' (6@60-69)
519 sending: 'x' (7@70-79)
520 thing (7@70-79): 'x' (7@70-79)
521 sending: ')' (8@80-89)
522 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
523 sending: '(' (9@90-99)
524 sending: 'x' (10@100-109)
525 thing (10@100-109): 'x' (10@100-109)
526 sending: ')' (11@110-119)
527 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
528 sending: '(' (12@120-129)
529 sending: 'x' (13@130-139)
530 thing (13@130-139): 'x' (13@130-139)
531 sending: ')' (14@140-149)
532 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
533 sending: '(' (15@150-159)
534 sending: 'x' (16@160-169)
535 thing (16@160-169): 'x' (16@160-169)
536 sending: ')' (17@170-179)
537 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
538 sending: '(' (18@180-189)
539 sending: 'x' (19@190-199)
540 thing (19@190-199): 'x' (19@190-199)
541 sending: ')' (20@200-209)
542 200-209: memory exhausted
543 Freeing nterm thing (19@190-199)
544 Freeing nterm line (15@150-179)
545 Freeing nterm line (12@120-149)
546 Freeing nterm line (9@90-119)
547 Freeing nterm line (6@60-89)
548 Freeing nterm line (3@30-59)
549 Freeing nterm line (0@0-29)
550 Parsing FAILED (status 2).
551 ]])
552 ])
553
554 ])
555
556
557 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
558 # ---------------------------------------------------------------------------
559 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
560 [AT_SETUP([Printers and Destructors $2: $1])
561
562 $3
563 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
564 [%error-verbose
565 %debug
566 %verbose
567 %locations
568 $1], [$2])
569
570 AT_CLEANUP
571 ])
572
573
574 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
575 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
576
577 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
578 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
579
580 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
581 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
582
583
584
585 ## ----------------------------------------- ##
586 ## Default tagless %printer and %destructor. ##
587 ## ----------------------------------------- ##
588
589 # Check that the right %printer and %destructor are called, that they're not
590 # called for $end, and that $$ and @$ work correctly.
591
592 AT_SETUP([Default tagless %printer and %destructor])
593
594 AT_DATA_GRAMMAR([[input.y]],
595 [[%error-verbose
596 %debug
597 %locations
598 %initial-action {
599 @$.first_line = @$.last_line = 1;
600 @$.first_column = @$.last_column = 1;
601 }
602
603 %{
604 # include <stdio.h>
605 # include <stdlib.h>
606 static void yyerror (const char *msg);
607 static int yylex (void);
608 # define USE(SYM)
609 %}
610
611 %printer {
612 fprintf (yyoutput, "<*> printer should not be called.\n");
613 } <*>
614
615 %printer {
616 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
617 } <>
618 %destructor {
619 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
620 } <>
621
622 %printer {
623 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
624 } 'b' 'c'
625 %destructor {
626 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
627 } 'b' 'c'
628
629 %destructor {
630 fprintf (yyoutput, "<*> destructor should not be called.\n");
631 } <*>
632
633 %%
634
635 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
636
637 %%
638
639 static int
640 yylex (void)
641 {
642 static char const input[] = "abcd";
643 static size_t toknum;
644 if (! (toknum < sizeof input))
645 abort ();
646 yylval = input[toknum++];
647 yylloc.first_line = yylloc.last_line = 1;
648 yylloc.first_column = yylloc.last_column = toknum;
649 return yylval;
650 }
651
652 static void
653 yyerror (const char *msg)
654 {
655 fprintf (stderr, "%s\n", msg);
656 }
657
658 int
659 main (void)
660 {
661 yydebug = 1;
662 return yyparse ();
663 }
664 ]])
665
666 AT_BISON_CHECK([-o input.c input.y])
667 AT_COMPILE([input])
668 AT_PARSER_CHECK([./input], 1,
669 [[<> destructor for 'd' @ 4.
670 'b'/'c' destructor for 'c' @ 3.
671 'b'/'c' destructor for 'b' @ 2.
672 <> destructor for 'a' @ 1.
673 ]],
674 [[Starting parse
675 Entering state 0
676 Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1)
677 Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1)
678 Entering state 1
679 Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
680 Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
681 Entering state 3
682 Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
683 Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
684 Entering state 5
685 Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4)
686 Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4)
687 Entering state 6
688 Reading a token: Now at end of input.
689 syntax error, unexpected $end, expecting 'e'
690 Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4)
691 Stack now 0 1 3 5
692 Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
693 Stack now 0 1 3
694 Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
695 Stack now 0 1
696 Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1)
697 Stack now 0
698 Cleanup: discarding lookahead token $end (1.5-1.5: )
699 Stack now 0
700 ]])
701
702 AT_CLEANUP
703
704
705
706 ## ------------------------------------------------------ ##
707 ## Default tagged and per-type %printer and %destructor. ##
708 ## ------------------------------------------------------ ##
709
710 AT_SETUP([Default tagged and per-type %printer and %destructor])
711
712 AT_DATA_GRAMMAR([[input.y]],
713 [[%error-verbose
714 %debug
715
716 %{
717 # include <stdio.h>
718 # include <stdlib.h>
719 static void yyerror (const char *msg);
720 static int yylex (void);
721 # define USE(SYM)
722 %}
723
724 %printer {
725 fprintf (yyoutput, "<> printer should not be called.\n");
726 } <>
727
728 %union { int field0; int field1; int field2; }
729 %type <field0> start 'a' 'g'
730 %type <field1> 'e'
731 %type <field2> 'f'
732 %printer {
733 fprintf (yyoutput, "<*>/<field2>/e printer");
734 } <*> 'e' <field2>
735 %destructor {
736 fprintf (stdout, "<*>/<field2>/e destructor.\n");
737 } <*> 'e' <field2>
738
739 %type <field1> 'b'
740 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
741 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
742
743 %type <field0> 'c'
744 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
745 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
746
747 %type <field1> 'd'
748 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
749 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
750
751 %destructor {
752 fprintf (yyoutput, "<> destructor should not be called.\n");
753 } <>
754
755 %%
756
757 start:
758 'a' 'b' 'c' 'd' 'e' 'f' 'g'
759 {
760 USE(($1, $2, $3, $4, $5, $6, $7));
761 $$ = 'S';
762 }
763 ;
764
765 %%
766
767 static int
768 yylex (void)
769 {
770 static char const input[] = "abcdef";
771 static size_t toknum;
772 if (! (toknum < sizeof input))
773 abort ();
774 return input[toknum++];
775 }
776
777 static void
778 yyerror (const char *msg)
779 {
780 fprintf (stderr, "%s\n", msg);
781 }
782
783 int
784 main (void)
785 {
786 yydebug = 1;
787 return yyparse ();
788 }
789 ]])
790
791 AT_BISON_CHECK([-o input.c input.y])
792 AT_COMPILE([input])
793 AT_PARSER_CHECK([./input], 1,
794 [[<*>/<field2>/e destructor.
795 <*>/<field2>/e destructor.
796 'd' destructor.
797 'c' destructor.
798 <field1> destructor.
799 <*>/<field2>/e destructor.
800 ]],
801 [[Starting parse
802 Entering state 0
803 Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
804 Shifting token 'a' (<*>/<field2>/e printer)
805 Entering state 1
806 Reading a token: Next token is token 'b' (<field1> printer)
807 Shifting token 'b' (<field1> printer)
808 Entering state 3
809 Reading a token: Next token is token 'c' ('c' printer)
810 Shifting token 'c' ('c' printer)
811 Entering state 5
812 Reading a token: Next token is token 'd' ('d' printer)
813 Shifting token 'd' ('d' printer)
814 Entering state 6
815 Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
816 Shifting token 'e' (<*>/<field2>/e printer)
817 Entering state 7
818 Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
819 Shifting token 'f' (<*>/<field2>/e printer)
820 Entering state 8
821 Reading a token: Now at end of input.
822 syntax error, unexpected $end, expecting 'g'
823 Error: popping token 'f' (<*>/<field2>/e printer)
824 Stack now 0 1 3 5 6 7
825 Error: popping token 'e' (<*>/<field2>/e printer)
826 Stack now 0 1 3 5 6
827 Error: popping token 'd' ('d' printer)
828 Stack now 0 1 3 5
829 Error: popping token 'c' ('c' printer)
830 Stack now 0 1 3
831 Error: popping token 'b' (<field1> printer)
832 Stack now 0 1
833 Error: popping token 'a' (<*>/<field2>/e printer)
834 Stack now 0
835 Cleanup: discarding lookahead token $end ()
836 Stack now 0
837 ]])
838
839 AT_CLEANUP
840
841
842
843 ## ------------------------------------------------------------- ##
844 ## Default %printer and %destructor for user-defined end token. ##
845 ## ------------------------------------------------------------- ##
846
847 AT_SETUP([Default %printer and %destructor for user-defined end token])
848
849 # _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED)
850 # -----------------------------------------------------------------------------
851 m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN],
852 [m4_if($1, 0,
853 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
854 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
855
856 AT_DATA_GRAMMAR([[input]]$1[[.y]],
857 [[%error-verbose
858 %debug
859 %locations
860 %initial-action {
861 @$.first_line = @$.last_line = 1;
862 @$.first_column = @$.last_column = 1;
863 }
864
865 %{
866 # include <stdio.h>
867 # include <stdlib.h>
868 static void yyerror (const char *msg);
869 static int yylex (void);
870 # define USE(SYM)
871 %}
872
873 %destructor {
874 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
875 } <]]not_kind[[>
876
877 %token END 0
878 %printer {
879 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
880 } <]]kind[[>
881 %destructor {
882 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
883 } <]]kind[[>
884
885 %printer {
886 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
887 } <]]not_kind[[>
888
889 ]]m4_if($1, 0, [[[
890 ]]],
891 [[[%union { char tag; }
892 %type <tag> start END]]])[[
893
894 %%
895
896 start: { $$ = 'S'; } ;
897
898 %%
899
900 static int
901 yylex (void)
902 {
903 static int called;
904 if (called++)
905 abort ();
906 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
907 yylloc.first_line = yylloc.last_line = 1;
908 yylloc.first_column = yylloc.last_column = 1;
909 return 0;
910 }
911
912 static void
913 yyerror (const char *msg)
914 {
915 fprintf (stderr, "%s\n", msg);
916 }
917
918 int
919 main (void)
920 {
921 yydebug = 1;
922 return yyparse ();
923 }
924 ]])
925
926 AT_BISON_CHECK([-o input$1.c input$1.y])
927 AT_COMPILE([input$1])
928 AT_PARSER_CHECK([./input$1], 0,
929 [[<]]kind[[> for 'E' @ 1.
930 <]]kind[[> for 'S' @ 1.
931 ]],
932 [[Starting parse
933 Entering state 0
934 Reducing stack by rule 1 (line 46):
935 -> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
936 Stack now 0
937 Entering state 1
938 Reading a token: Now at end of input.
939 Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
940 Entering state 2
941 Stack now 0 1 2
942 Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
943 Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
944 ]])
945
946 m4_popdef([kind])
947 m4_popdef([not_kind])
948 ])
949
950 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0)
951 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1)
952
953 AT_CLEANUP
954
955
956
957 ## ------------------------------------------------------------------ ##
958 ## Default %printer and %destructor are not for error or $undefined. ##
959 ## ------------------------------------------------------------------ ##
960
961 AT_SETUP([Default %printer and %destructor are not for error or $undefined])
962
963 # If Bison were to apply the default %printer and %destructor to the error
964 # token or to $undefined:
965 # - For the error token:
966 # - It would generate warnings for unused $n.
967 # - It would invoke the %printer and %destructor on the error token's
968 # semantic value, which would be initialized from the lookahead, which
969 # would be destroyed separately.
970 # - For $undefined, who knows what the semantic value would be.
971
972 AT_DATA_GRAMMAR([[input.y]],
973 [[%debug
974
975 %{
976 # include <stdio.h>
977 # include <stdlib.h>
978 static void yyerror (const char *msg);
979 static int yylex (void);
980 # define USE(SYM)
981 %}
982
983 %printer {
984 fprintf (yyoutput, "'%c'", $$);
985 } <> <*>
986 %destructor {
987 fprintf (stderr, "DESTROY '%c'\n", $$);
988 } <> <*>
989
990 %%
991
992 start:
993 { $$ = 'S'; }
994 /* In order to reveal the problems that this bug caused during parsing, add
995 * $2 to USE. */
996 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
997 ;
998
999 %%
1000
1001 static int
1002 yylex (void)
1003 {
1004 static char const input[] = "abd";
1005 static size_t toknum;
1006 if (! (toknum < sizeof input))
1007 abort ();
1008 yylval = input[toknum++];
1009 return yylval;
1010 }
1011
1012 static void
1013 yyerror (const char *msg)
1014 {
1015 fprintf (stderr, "%s\n", msg);
1016 }
1017
1018 int
1019 main (void)
1020 {
1021 yydebug = 1;
1022 return yyparse ();
1023 }
1024 ]])
1025
1026 AT_BISON_CHECK([-o input.c input.y])
1027 AT_COMPILE([input])
1028 AT_PARSER_CHECK([./input], [1], [],
1029 [[Starting parse
1030 Entering state 0
1031 Reading a token: Next token is token 'a' ('a')
1032 Shifting token 'a' ('a')
1033 Entering state 1
1034 Reading a token: Next token is token 'b' ('b')
1035 syntax error
1036 Shifting token error ()
1037 Entering state 3
1038 Next token is token 'b' ('b')
1039 Shifting token 'b' ('b')
1040 Entering state 5
1041 Reading a token: Next token is token $undefined ()
1042 Error: popping token 'b' ('b')
1043 DESTROY 'b'
1044 Stack now 0 1 3
1045 Error: popping token error ()
1046 Stack now 0 1
1047 Shifting token error ()
1048 Entering state 3
1049 Next token is token $undefined ()
1050 Error: discarding token $undefined ()
1051 Error: popping token error ()
1052 Stack now 0 1
1053 Shifting token error ()
1054 Entering state 3
1055 Reading a token: Now at end of input.
1056 Cleanup: discarding lookahead token $end ()
1057 Stack now 0 1 3
1058 Cleanup: popping token error ()
1059 Cleanup: popping token 'a' ('a')
1060 DESTROY 'a'
1061 ]])
1062
1063 AT_CLEANUP
1064
1065
1066
1067 ## ------------------------------------------------------ ##
1068 ## Default %printer and %destructor are not for $accept. ##
1069 ## ------------------------------------------------------ ##
1070
1071 AT_SETUP([Default %printer and %destructor are not for $accept])
1072
1073 # If YYSTYPE is a union and Bison were to apply the default %printer and
1074 # %destructor to $accept:
1075 # - The %printer and %destructor code generated for $accept would always be
1076 # dead code because $accept is currently never shifted onto the stack.
1077 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1078 # to declare `%type <field> $accept'. (Also true for $undefined.)
1079 # - Thus, the compiler might complain that the user code assumes the wrong
1080 # type for $$ since the code might assume the type associated with a
1081 # specific union field, which is especially reasonable in C++ since that
1082 # type may be a base type. This test case checks for this problem. (Also
1083 # true for $undefined and the error token, so there are three warnings for
1084 # %printer and three for %destructor.)
1085
1086 AT_DATA_GRAMMAR([[input.y]],
1087 [[%debug /* So that %printer is actually compiled. */
1088
1089 %{
1090 # include <stdio.h>
1091 # include <stdlib.h>
1092 static void yyerror (const char *msg);
1093 static int yylex (void);
1094 # define USE(SYM)
1095 %}
1096
1097 %printer {
1098 char chr = $$;
1099 fprintf (yyoutput, "'%c'", chr);
1100 } <> <*>
1101 %destructor {
1102 char chr = $$;
1103 fprintf (stderr, "DESTROY '%c'\n", chr);
1104 } <> <*>
1105
1106 %union { char chr; }
1107 %type <chr> start
1108
1109 %%
1110
1111 start: { USE($$); } ;
1112
1113 %%
1114
1115 static int
1116 yylex (void)
1117 {
1118 static int called;
1119 if (called++)
1120 abort ();
1121 return 0;
1122 }
1123
1124 static void
1125 yyerror (const char *msg)
1126 {
1127 fprintf (stderr, "%s\n", msg);
1128 }
1129
1130 int
1131 main (void)
1132 {
1133 return yyparse ();
1134 }
1135 ]])
1136
1137 AT_BISON_CHECK([-o input.c input.y])
1138 AT_COMPILE([input])
1139
1140 AT_CLEANUP
1141
1142
1143
1144 ## ------------------------------------------------------ ##
1145 ## Default %printer and %destructor for mid-rule values. ##
1146 ## ------------------------------------------------------ ##
1147
1148 AT_SETUP([Default %printer and %destructor for mid-rule values])
1149
1150 AT_DATA_GRAMMAR([[input.y]],
1151 [[%debug /* So that %printer is actually compiled. */
1152
1153 %{
1154 # include <stdio.h>
1155 # include <stdlib.h>
1156 static void yyerror (const char *msg);
1157 static int yylex (void);
1158 # define USE(SYM)
1159 # define YYLTYPE int
1160 # define YYLLOC_DEFAULT(Current, Rhs, N)
1161 # define YY_LOCATION_PRINT(File, Loc)
1162 %}
1163
1164 %printer { fprintf (yyoutput, "%d", @$); } <>
1165 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1166 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1167 %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1168
1169 %%
1170
1171 start:
1172 { @$ = 1; } // Not set or used.
1173 { USE ($$); @$ = 2; } // Both set and used.
1174 { USE ($$); @$ = 3; } // Only set.
1175 { @$ = 4; } // Only used.
1176 'c'
1177 { USE (($$, $2, $4, $5)); @$ = 0; }
1178 ;
1179
1180 %%
1181
1182 static int
1183 yylex (void)
1184 {
1185 static int called;
1186 if (called++)
1187 abort ();
1188 return 0;
1189 }
1190
1191 static void
1192 yyerror (const char *msg)
1193 {
1194 fprintf (stderr, "%s\n", msg);
1195 }
1196
1197 int
1198 main (void)
1199 {
1200 yydebug = 1;
1201 return yyparse ();
1202 }
1203 ]])
1204
1205 AT_BISON_CHECK([-o input.c input.y], 0,,
1206 [[input.y:33.3-23: warning: unset value: $$
1207 input.y:30.3-35.37: warning: unused value: $3
1208 ]])
1209
1210 AT_COMPILE([input])
1211 AT_PARSER_CHECK([./input], 1,,
1212 [[Starting parse
1213 Entering state 0
1214 Reducing stack by rule 1 (line 30):
1215 -> $$ = nterm $@1 (: )
1216 Stack now 0
1217 Entering state 2
1218 Reducing stack by rule 2 (line 31):
1219 -> $$ = nterm @2 (: 2)
1220 Stack now 0 2
1221 Entering state 4
1222 Reducing stack by rule 3 (line 32):
1223 -> $$ = nterm @3 (: 3)
1224 Stack now 0 2 4
1225 Entering state 5
1226 Reducing stack by rule 4 (line 33):
1227 -> $$ = nterm @4 (: 4)
1228 Stack now 0 2 4 5
1229 Entering state 6
1230 Reading a token: Now at end of input.
1231 syntax error
1232 Error: popping nterm @4 (: 4)
1233 DESTROY 4
1234 Stack now 0 2 4 5
1235 Error: popping nterm @3 (: 3)
1236 DESTROY 3
1237 Stack now 0 2 4
1238 Error: popping nterm @2 (: 2)
1239 DESTROY 2
1240 Stack now 0 2
1241 Error: popping nterm $@1 (: )
1242 Stack now 0
1243 Cleanup: discarding lookahead token $end (: )
1244 Stack now 0
1245 ]])
1246
1247 AT_CLEANUP
1248
1249
1250 ## ----------------------- ##
1251 ## @$ implies %locations. ##
1252 ## ----------------------- ##
1253
1254 # Bison once forgot to check for @$ in actions other than semantic actions.
1255
1256 # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1257 # -------------------------------------------------------
1258 m4_define([AT_CHECK_ACTION_LOCATIONS],
1259 [AT_SETUP([[@$ in ]$1[ implies %locations]])
1260
1261 AT_DATA_GRAMMAR([[input.y]],
1262 [[%code {
1263 #include <stdio.h>
1264 static int yylex (void);
1265 static void yyerror (char const *msg);
1266 }
1267
1268 %debug
1269
1270 ]$1[ {
1271 printf ("%d\n", @$.first_line);
1272 } ]m4_if($1, [%initial-action], [], [[start]])[
1273
1274 %%
1275
1276 start: ;
1277
1278 %%
1279
1280 static int
1281 yylex (void)
1282 {
1283 return 0;
1284 }
1285
1286 static void
1287 yyerror (char const *msg)
1288 {
1289 fprintf (stderr, "%s\n", msg);
1290 }
1291
1292 int
1293 main (void)
1294 {
1295 return yyparse ();
1296 }
1297 ]])
1298
1299 AT_BISON_CHECK([[-o input.c input.y]])
1300 AT_COMPILE([[input]])
1301
1302 AT_CLEANUP])
1303
1304 AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1305 AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1306 AT_CHECK_ACTION_LOCATIONS([[%printer]])