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