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