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