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