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