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