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