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