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