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