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