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