]> git.saurik.com Git - bison.git/blame - tests/actions.at
* bootstrap.conf (gnulib_modules): Add config-h.
[bison.git] / tests / actions.at
CommitLineData
82c035a8 1# Executing Actions. -*- Autotest -*-
378f4bd8 2# Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
82c035a8
AD
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2, or (at your option)
7# any later version.
8
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
0fb669f9
PE
16# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17# 02110-1301, USA.
82c035a8
AD
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
9501dc6e 32AT_DATA_GRAMMAR([[input.y]],
8f3596a6
AD
33[[%error-verbose
34%debug
35%{
d99361e6
AD
36# include <stdio.h>
37# include <stdlib.h>
38 static void yyerror (const char *msg);
39 static int yylex (void);
82c035a8
AD
40%}
41%%
931394cb
AD
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'); }
82c035a8
AD
53 ;
54%%
55static int
56yylex (void)
57{
cf806753
PE
58 static char const input[] = "123456789";
59 static size_t toknum;
60 if (! (toknum < sizeof input))
61 abort ();
62 return input[toknum++];
82c035a8
AD
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
b56471a6 78AT_CHECK([bison -d -v -o input.c input.y])
1154cced
AD
79AT_COMPILE([input])
80AT_PARSER_CHECK([./input], 0,
931394cb 81[[0123456789
82c035a8
AD
82]])
83
84AT_CLEANUP
75d1fe16
AD
85
86
87
5dac0025
PE
88
89
75d1fe16
AD
90## ---------------- ##
91## Exotic Dollars. ##
92## ---------------- ##
93
94AT_SETUP([Exotic Dollars])
95
9501dc6e 96AT_DATA_GRAMMAR([[input.y]],
8f3596a6
AD
97[[%error-verbose
98%debug
99%{
75d1fe16
AD
100# include <stdio.h>
101# include <stdlib.h>
102 static void yyerror (const char *msg);
103 static int yylex (void);
affac613 104# define USE(Var)
75d1fe16
AD
105%}
106
107%union
108{
109 int val;
110};
111
0ff67d71 112%type <val> a_1 a_2 a_5
378f4bd8 113 sum_of_the_five_previous_values
75d1fe16
AD
114
115%%
0ff67d71
PE
116exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
117 sum_of_the_five_previous_values
75d1fe16 118 {
84866159 119 USE (($1, $2, $<foo>3, $<foo>4, $5));
75d1fe16
AD
120 printf ("%d\n", $6);
121 }
122;
123a_1: { $$ = 1; };
124a_2: { $$ = 2; };
75d1fe16
AD
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{
cf806753
PE
137 static int called;
138 if (called++)
139 abort ();
75d1fe16
AD
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
84866159 156AT_CHECK([bison -d -v -o input.c input.y], 0)
1154cced
AD
157AT_COMPILE([input])
158AT_PARSER_CHECK([./input], 0,
75d1fe16
AD
159[[15
160]])
161
162AT_CLEANUP
9280d3ef
AD
163
164
165
e776192e
AD
166## -------------------------- ##
167## Printers and Destructors. ##
168## -------------------------- ##
9280d3ef 169
ac700aa6
PE
170# _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
171# -----------------------------------------------------------------------------
3df37415 172m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
9c66f418
AD
173[# Make sure complex $n work.
174m4_if([$1$2$3], $[1]$[2]$[3], [],
3df37415
AD
175 [m4_fatal([$0: Invalid arguments: $@])])dnl
176
9c66f418
AD
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.
c2729758 179AT_BISON_OPTION_PUSHDEFS([$5])
9501dc6e 180AT_DATA_GRAMMAR([[input.y]],
34f98f46 181[[%start-header {
9280d3ef
AD
182#include <stdio.h>
183#include <stdlib.h>
cf806753 184#include <string.h>
9c66f418 185#include <assert.h>
80ce3401
PE
186
187#define YYINITDEPTH 10
188#define YYMAXDEPTH 10
c2729758
ADL
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])
9bc0dd67 192[}
9c66f418
AD
193
194$5]
195m4_ifval([$6], [%union
9280d3ef
AD
196{
197 int ival;
ac700aa6 198}])
fb9712a9 199AT_LALR1_CC_IF([%define "global_tokens_and_yystype"])
aef3da86 200m4_ifval([$6], [[%end-header {]], [[%after-header {]])
e8ec4d9b 201AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
c2729758 202[static int yylex (]AT_LEX_FORMALS[);
ca6f187f 203]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
9bc0dd67 204[}
c2729758 205
868d2d96 206]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
e3170060 207
868d2d96 208/* FIXME: This %printer isn't actually tested. */
a5eb1ed2
AD
209%printer
210 {
9a1e9989 211 ]AT_LALR1_CC_IF([debug_stream () << $$;],
3fc16193 212 [fprintf (yyoutput, "%d", $$)])[;
a5eb1ed2 213 }
9c66f418 214 input line thing 'x' 'y'
e3170060
AD
215
216%destructor
4c6cc1db 217 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
7bd6c77e 218 input
5719c109 219
7bd6c77e 220%destructor
4c6cc1db 221 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
7bd6c77e
AD
222 line
223
224%destructor
4c6cc1db 225 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
7bd6c77e
AD
226 thing
227
228%destructor
4c6cc1db 229 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
7bd6c77e 230 'x'
5719c109 231
9c66f418
AD
232%destructor
233 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
234 'y'
235
868d2d96
JD
236%token END 0
237%destructor
238 { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
239 END
240
9280d3ef 241%%
9c66f418
AD
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
9280d3ef
AD
249input:
250 /* Nothing. */
5719c109
AD
251 {
252 $$ = 0;
16f37b35 253 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
5719c109
AD
254 }
255| line input /* Right recursive to load the stack so that popping at
868d2d96 256 END can be exercised. */
5719c109
AD
257 {
258 $$ = 2;
16f37b35 259 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
4c6cc1db 260 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
5719c109 261 }
9280d3ef
AD
262;
263
264line:
265 thing thing thing ';'
5719c109
AD
266 {
267 $$ = $1;
16f37b35 268 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
4c6cc1db 269 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
16f37b35 270 $3, RANGE (@3), $4, RANGE (@4));
5719c109 271 }
9c66f418 272| '(' thing thing ')'
5719c109
AD
273 {
274 $$ = $1;
9c66f418
AD
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));
5719c109 278 }
9c66f418 279| '(' thing ')'
5719c109
AD
280 {
281 $$ = $1;
9c66f418
AD
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));
5719c109 284 }
9c66f418 285| '(' error ')'
5719c109
AD
286 {
287 $$ = -1;
9c66f418
AD
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));
5719c109 290 }
9280d3ef
AD
291;
292
293thing:
5719c109
AD
294 'x'
295 {
296 $$ = $1;
4c6cc1db
AD
297 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
298 $$, RANGE (@$), $1, RANGE (@1));
5719c109 299 }
9280d3ef
AD
300;
301%%
9c66f418 302/* Alias to ARGV[1]. */
a9739e7c 303const char *source = 0;
9c66f418 304
9280d3ef 305static int
c2729758 306yylex (]AT_LEX_FORMALS[)
9280d3ef 307{
5a08f1ce 308 static unsigned int counter = 0;
9280d3ef 309
9c66f418
AD
310 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
311 /* As in BASIC, line numbers go from 10 to 10. */
c2729758 312]AT_LALR1_CC_IF(
9c66f418
AD
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;
c2729758 315],
9c66f418
AD
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;
c2729758 318])[
9c66f418 319
cf806753
PE
320 if (! (0 <= c && c <= strlen (source)))
321 abort ();
a9739e7c
PE
322 if (source[c])
323 printf ("sending: '%c'", source[c]);
9280d3ef 324 else
868d2d96 325 printf ("sending: END");
9c66f418 326 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
a9739e7c 327 return source[c];
9280d3ef
AD
328}
329
c2729758 330]AT_LALR1_CC_IF(
68e11668 331[/* A C++ error reporting function. */
c2729758 332void
99880de5 333yy::parser::error (const location& l, const std::string& m)
c2729758 334{
efeed023 335 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
c2729758
ADL
336}
337
338static bool yydebug;
339int
340yyparse ()
341{
99880de5 342 yy::parser parser;
a3cb6248 343 parser.set_debug_level (yydebug);
c2729758
ADL
344 return parser.parse ();
345}
346],
347[static void
9280d3ef
AD
348yyerror (const char *msg)
349{
4c6cc1db 350 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
c2729758 351}])[
9280d3ef 352
9280d3ef 353int
9c66f418 354main (int argc, const char *argv[])
9280d3ef 355{
6100a9aa 356 int status;
9280d3ef 357 yydebug = !!getenv ("YYDEBUG");
9c66f418 358 assert (argc == 2);
a9739e7c 359 source = argv[1];
6100a9aa
PE
360 status = yyparse ();
361 switch (status)
9280d3ef 362 {
6100a9aa
PE
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;
9280d3ef 366 }
6100a9aa 367 return status;
9280d3ef
AD
368}
369]])
370
07971983
PE
371AT_LALR1_CC_IF(
372 [AT_CHECK([bison -o input.cc input.y])
373 AT_COMPILE_CXX([input])],
374 [AT_CHECK([bison -o input.c input.y])
375 AT_COMPILE([input])])
9c66f418
AD
376
377
378# Check the location of "empty"
379# -----------------------------
380# I.e., epsilon-reductions, as in "(x)" which ends by reducing
381# an empty "line" nterm.
382# FIXME: This location is not satisfying. Depend on the lookahead?
383AT_PARSER_CHECK([./input '(x)'], 0,
384[[sending: '(' (0@0-9)
385sending: 'x' (1@10-19)
386thing (1@10-19): 'x' (1@10-19)
387sending: ')' (2@20-29)
388line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
868d2d96 389sending: END (3@30-39)
b4a20338
AD
390input (0@29-29): /* Nothing */
391input (2@0-29): line (0@0-29) input (0@29-29)
868d2d96 392Freeing token END (3@30-39)
258b75ca 393Freeing nterm input (2@0-29)
9c66f418
AD
394Successful parse.
395]])
396
397
398# Check locations in error recovery
399# ---------------------------------
400# '(y)' is an error, but can be recovered from. But what's the location
401# of the error itself ('y'), and of the resulting reduction ('(error)').
402AT_PARSER_CHECK([./input '(y)'], 0,
403[[sending: '(' (0@0-9)
404sending: 'y' (1@10-19)
40510-19: syntax error, unexpected 'y', expecting 'x'
406Freeing token 'y' (1@10-19)
407sending: ')' (2@20-29)
408line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
868d2d96 409sending: END (3@30-39)
b4a20338
AD
410input (0@29-29): /* Nothing */
411input (2@0-29): line (-1@0-29) input (0@29-29)
868d2d96 412Freeing token END (3@30-39)
258b75ca 413Freeing nterm input (2@0-29)
9c66f418
AD
414Successful parse.
415]])
416
417
418# Syntax errors caught by the parser
419# ----------------------------------
420# Exercise the discarding of stack top and input until `error'
421# can be reduced.
422#
423# '(', 'x', 'x', 'x', 'x', 'x', ')',
424#
425# Load the stack and provoke an error that cannot be caught by the
426# grammar, to check that the stack is cleared. And make sure the
427# lookahead is freed.
428#
429# '(', 'x', ')',
430# '(', 'x', ')',
431# 'y'
432AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
433[[sending: '(' (0@0-9)
4c6cc1db
AD
434sending: 'x' (1@10-19)
435thing (1@10-19): 'x' (1@10-19)
436sending: 'x' (2@20-29)
437thing (2@20-29): 'x' (2@20-29)
438sending: 'x' (3@30-39)
9c66f418 43930-39: syntax error, unexpected 'x', expecting ')'
4c6cc1db
AD
440Freeing nterm thing (2@20-29)
441Freeing nterm thing (1@10-19)
4c6cc1db
AD
442Freeing token 'x' (3@30-39)
443sending: 'x' (4@40-49)
444Freeing token 'x' (4@40-49)
445sending: 'x' (5@50-59)
446Freeing token 'x' (5@50-59)
9c66f418
AD
447sending: ')' (6@60-69)
448line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
449sending: '(' (7@70-79)
4c6cc1db
AD
450sending: 'x' (8@80-89)
451thing (8@80-89): 'x' (8@80-89)
9c66f418
AD
452sending: ')' (9@90-99)
453line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
454sending: '(' (10@100-109)
455sending: 'x' (11@110-119)
456thing (11@110-119): 'x' (11@110-119)
457sending: ')' (12@120-129)
458line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
459sending: 'y' (13@130-139)
b4a20338
AD
460input (0@129-129): /* Nothing */
461input (2@100-129): line (10@100-129) input (0@129-129)
9c66f418
AD
462input (2@70-129): line (7@70-99) input (2@100-129)
463input (2@0-129): line (-1@0-69) input (2@70-129)
868d2d96 464130-139: syntax error, unexpected 'y', expecting END
9c66f418
AD
465Freeing nterm input (2@0-129)
466Freeing token 'y' (13@130-139)
5719c109 467Parsing FAILED.
9280d3ef
AD
468]])
469
868d2d96
JD
470
471# Syntax error caught by the parser where lookahead = END
472# --------------------------------------------------------
473# Load the stack and provoke an error that cannot be caught by the
474# grammar, to check that the stack is cleared. And make sure the
475# lookahead is freed.
476#
477# '(', 'x', ')',
478# '(', 'x', ')',
479# 'x'
480AT_PARSER_CHECK([./input '(x)(x)x'], 1,
481[[sending: '(' (0@0-9)
482sending: 'x' (1@10-19)
483thing (1@10-19): 'x' (1@10-19)
484sending: ')' (2@20-29)
485line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
486sending: '(' (3@30-39)
487sending: 'x' (4@40-49)
488thing (4@40-49): 'x' (4@40-49)
489sending: ')' (5@50-59)
490line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
491sending: 'x' (6@60-69)
492thing (6@60-69): 'x' (6@60-69)
493sending: END (7@70-79)
49470-79: syntax error, unexpected END, expecting 'x'
495Freeing nterm thing (6@60-69)
496Freeing nterm line (3@30-59)
497Freeing nterm line (0@0-29)
498Freeing token END (7@70-79)
499Parsing FAILED.
500]])
501
502
80ce3401
PE
503# Check destruction upon stack overflow
504# -------------------------------------
505# Upon stack overflow, all symbols on the stack should be destroyed.
506# Only check for yacc.c.
507AT_YACC_IF([
6100a9aa 508AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
80ce3401
PE
509[[sending: '(' (0@0-9)
510sending: 'x' (1@10-19)
511thing (1@10-19): 'x' (1@10-19)
512sending: ')' (2@20-29)
513line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
514sending: '(' (3@30-39)
515sending: 'x' (4@40-49)
516thing (4@40-49): 'x' (4@40-49)
517sending: ')' (5@50-59)
518line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
519sending: '(' (6@60-69)
520sending: 'x' (7@70-79)
521thing (7@70-79): 'x' (7@70-79)
522sending: ')' (8@80-89)
523line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
524sending: '(' (9@90-99)
525sending: 'x' (10@100-109)
526thing (10@100-109): 'x' (10@100-109)
527sending: ')' (11@110-119)
528line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
529sending: '(' (12@120-129)
530sending: 'x' (13@130-139)
531thing (13@130-139): 'x' (13@130-139)
532sending: ')' (14@140-149)
533line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
534sending: '(' (15@150-159)
535sending: 'x' (16@160-169)
536thing (16@160-169): 'x' (16@160-169)
537sending: ')' (17@170-179)
538line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
539sending: '(' (18@180-189)
540sending: 'x' (19@190-199)
541thing (19@190-199): 'x' (19@190-199)
542sending: ')' (20@200-209)
1a059451 543200-209: memory exhausted
80ce3401
PE
544Freeing nterm thing (19@190-199)
545Freeing nterm line (15@150-179)
546Freeing nterm line (12@120-149)
547Freeing nterm line (9@90-119)
548Freeing nterm line (6@60-89)
549Freeing nterm line (3@30-59)
550Freeing nterm line (0@0-29)
6100a9aa 551Parsing FAILED (status 2).
80ce3401
PE
552]])
553])
554
3df37415
AD
555])
556
557
a14a26fa 558# AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
046ac74e 559# ---------------------------------------------------------------------------
3df37415 560m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
9c66f418
AD
561[AT_SETUP([Printers and Destructors $2: $1])
562
a14a26fa 563$3
9c66f418
AD
564_AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
565[%error-verbose
566%debug
567%verbose
568%locations
569$1], [$2])
570
571AT_CLEANUP
3df37415
AD
572])
573
574
ac700aa6
PE
575AT_CHECK_PRINTER_AND_DESTRUCTOR([])
576AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
046ac74e 577
fd19f271
AD
578AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
579AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
046ac74e 580
1576d44d
AD
581AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
582AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
ec5479ce
JD
583
584
585
ec5479ce
JD
586## --------------------------------- ##
587## Default %printer and %destructor. ##
588## --------------------------------- ##
589
590# Check that the right %printer and %destructor are called, that they're not
591# called for $end, and that $$ and @$ work correctly.
592
593AT_SETUP([Default %printer and %destructor])
594
595AT_DATA_GRAMMAR([[input.y]],
596[[%error-verbose
597%debug
598%locations
599%initial-action {
600 @$.first_line = @$.last_line = 1;
601 @$.first_column = @$.last_column = 1;
602}
603
604%{
605# include <stdio.h>
606# include <stdlib.h>
607 static void yyerror (const char *msg);
608 static int yylex (void);
609# define USE(SYM)
610%}
611
612%printer {
613 fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column);
3be03b13 614} %symbol-default
ec5479ce
JD
615%destructor {
616 fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column);
3be03b13 617} %symbol-default
ec5479ce
JD
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%%
627
628start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
629
630%%
631
632static int
633yylex (void)
634{
cf806753
PE
635 static char const input[] = "abcd";
636 static size_t toknum;
637 if (! (toknum < sizeof input))
638 abort ();
639 yylval = input[toknum++];
ec5479ce 640 yylloc.first_line = yylloc.last_line = 1;
cf806753 641 yylloc.first_column = yylloc.last_column = toknum;
ec5479ce
JD
642 return yylval;
643}
644
645static void
646yyerror (const char *msg)
647{
648 fprintf (stderr, "%s\n", msg);
649}
650
651int
652main (void)
653{
654 yydebug = 1;
655 return yyparse ();
656}
657]])
658
659AT_CHECK([bison -o input.c input.y])
660AT_COMPILE([input])
661AT_PARSER_CHECK([./input], 1,
662[[Default destructor for 'd' @ 4.
663'b'/'c' destructor for 'c' @ 3.
664'b'/'c' destructor for 'b' @ 2.
665Default destructor for 'a' @ 1.
666]],
667[[Starting parse
668Entering state 0
669Reading a token: Next token is token 'a' (1.1-1.1: Default printer for 'a' @ 1)
670Shifting token 'a' (1.1-1.1: Default printer for 'a' @ 1)
671Entering state 1
672Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
673Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
674Entering state 3
675Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
676Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
677Entering state 5
678Reading a token: Next token is token 'd' (1.4-1.4: Default printer for 'd' @ 4)
679Shifting token 'd' (1.4-1.4: Default printer for 'd' @ 4)
680Entering state 6
681Reading a token: Now at end of input.
682syntax error, unexpected $end, expecting 'e'
683Error: popping token 'd' (1.4-1.4: Default printer for 'd' @ 4)
684Stack now 0 1 3 5
685Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
686Stack now 0 1 3
687Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
688Stack now 0 1
689Error: popping token 'a' (1.1-1.1: Default printer for 'a' @ 1)
690Stack now 0
691Cleanup: discarding lookahead token $end (1.5-1.5: )
692Stack now 0
693]])
694
695AT_CLEANUP
696
697
698
b2a0b7ca
JD
699## ----------------------------------- ##
700## Per-type %printer and %destructor. ##
701## ----------------------------------- ##
702
703AT_SETUP([Per-type %printer and %destructor])
704
705AT_DATA_GRAMMAR([[input.y]],
706[[%error-verbose
707%debug
708
709%{
710# include <stdio.h>
711# include <stdlib.h>
712 static void yyerror (const char *msg);
713 static int yylex (void);
714# define USE(SYM)
715%}
716
717%union { int field0; int field1; int field2; }
718%type <field0> start 'a' 'g'
719%type <field1> 'e'
720%type <field2> 'f'
721%printer {
722 fprintf (yyoutput, "%%symbol-default/<field2>/e printer");
723} %symbol-default 'e' <field2>
724%destructor {
725 fprintf (stdout, "%%symbol-default/<field2>/e destructor.\n");
726} %symbol-default 'e' <field2>
727
728%type <field1> 'b'
729%printer { fprintf (yyoutput, "<field1> printer"); } <field1>
730%destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
731
732%type <field0> 'c'
733%printer { fprintf (yyoutput, "'c' printer"); } 'c'
734%destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
735
736%type <field1> 'd'
737%printer { fprintf (yyoutput, "'d' printer"); } 'd'
738%destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
739
740%%
741
742start:
743 'a' 'b' 'c' 'd' 'e' 'f' 'g'
744 {
745 USE(($1, $2, $3, $4, $5, $6, $7));
746 $$ = 'S';
747 }
748 ;
749
750%%
751
752static int
753yylex (void)
754{
cf806753
PE
755 static char const input[] = "abcdef";
756 static size_t toknum;
757 if (! (toknum < sizeof input))
758 abort ();
759 return input[toknum++];
b2a0b7ca
JD
760}
761
762static void
763yyerror (const char *msg)
764{
765 fprintf (stderr, "%s\n", msg);
766}
767
768int
769main (void)
770{
771 yydebug = 1;
772 return yyparse ();
773}
774]])
775
776AT_CHECK([bison -o input.c input.y])
777AT_COMPILE([input])
778AT_PARSER_CHECK([./input], 1,
779[[%symbol-default/<field2>/e destructor.
780%symbol-default/<field2>/e destructor.
781'd' destructor.
782'c' destructor.
783<field1> destructor.
784%symbol-default/<field2>/e destructor.
785]],
786[[Starting parse
787Entering state 0
788Reading a token: Next token is token 'a' (%symbol-default/<field2>/e printer)
789Shifting token 'a' (%symbol-default/<field2>/e printer)
790Entering state 1
791Reading a token: Next token is token 'b' (<field1> printer)
792Shifting token 'b' (<field1> printer)
793Entering state 3
794Reading a token: Next token is token 'c' ('c' printer)
795Shifting token 'c' ('c' printer)
796Entering state 5
797Reading a token: Next token is token 'd' ('d' printer)
798Shifting token 'd' ('d' printer)
799Entering state 6
800Reading a token: Next token is token 'e' (%symbol-default/<field2>/e printer)
801Shifting token 'e' (%symbol-default/<field2>/e printer)
802Entering state 7
803Reading a token: Next token is token 'f' (%symbol-default/<field2>/e printer)
804Shifting token 'f' (%symbol-default/<field2>/e printer)
805Entering state 8
806Reading a token: Now at end of input.
807syntax error, unexpected $end, expecting 'g'
808Error: popping token 'f' (%symbol-default/<field2>/e printer)
809Stack now 0 1 3 5 6 7
810Error: popping token 'e' (%symbol-default/<field2>/e printer)
811Stack now 0 1 3 5 6
812Error: popping token 'd' ('d' printer)
813Stack now 0 1 3 5
814Error: popping token 'c' ('c' printer)
815Stack now 0 1 3
816Error: popping token 'b' (<field1> printer)
817Stack now 0 1
818Error: popping token 'a' (%symbol-default/<field2>/e printer)
819Stack now 0
820Cleanup: discarding lookahead token $end ()
821Stack now 0
822]])
823
824AT_CLEANUP
825
826
827
ec5479ce 828## ------------------------------------------------------------- ##
3508ce36 829## Default %printer and %destructor for user-defined end token. ##
ec5479ce
JD
830## ------------------------------------------------------------- ##
831
3508ce36 832AT_SETUP([Default %printer and %destructor for user-defined end token])
ec5479ce
JD
833
834AT_DATA_GRAMMAR([[input.y]],
835[[%error-verbose
836%debug
837%locations
838%initial-action {
839 @$.first_line = @$.last_line = 1;
840 @$.first_column = @$.last_column = 1;
841}
842
843%{
844# include <stdio.h>
845# include <stdlib.h>
846 static void yyerror (const char *msg);
847 static int yylex (void);
848# define USE(SYM)
849%}
850
851%token END 0
852%printer {
853 fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column);
3be03b13 854} %symbol-default
ec5479ce
JD
855%destructor {
856 fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column);
3be03b13 857} %symbol-default
ec5479ce
JD
858
859%%
860
861start: { $$ = 'S'; } ;
862
863%%
864
865static int
866yylex (void)
867{
cf806753
PE
868 static int called;
869 if (called++)
870 abort ();
ec5479ce
JD
871 yylval = 'E';
872 yylloc.first_line = yylloc.last_line = 1;
873 yylloc.first_column = yylloc.last_column = 1;
874 return 0;
875}
876
877static void
878yyerror (const char *msg)
879{
880 fprintf (stderr, "%s\n", msg);
881}
882
883int
884main (void)
885{
886 yydebug = 1;
887 return yyparse ();
888}
889]])
890
891AT_CHECK([bison -o input.c input.y])
892AT_COMPILE([input])
893AT_PARSER_CHECK([./input], 0,
894[[Default destructor for 'E' @ 1.
895Default destructor for 'S' @ 1.
896]],
897[[Starting parse
898Entering state 0
231ed89a 899Reducing stack by rule 1 (line 35):
ec5479ce
JD
900-> $$ = nterm start (1.1-1.1: Default printer for 'S' @ 1)
901Stack now 0
902Entering state 1
903Reading a token: Now at end of input.
904Shifting token END (1.1-1.1: Default printer for 'E' @ 1)
905Entering state 2
906Stack now 0 1 2
907Cleanup: popping token END (1.1-1.1: Default printer for 'E' @ 1)
908Cleanup: popping nterm start (1.1-1.1: Default printer for 'S' @ 1)
909]])
910
911AT_CLEANUP
9350499c
JD
912
913
914
915## ------------------------------------------------------------------ ##
916## Default %printer and %destructor are not for error or $undefined. ##
917## ------------------------------------------------------------------ ##
918
919AT_SETUP([Default %printer and %destructor are not for error or \$undefined])
920
921# If Bison were to apply the default %printer and %destructor to the error
922# token or to $undefined:
923# - For the error token:
924# - It would generate warnings for unused $n.
925# - It would invoke the %printer and %destructor on the error token's
926# semantic value, which would be initialized from the lookahead, which
927# would be destroyed separately.
928# - For $undefined, who knows what the semantic value would be.
929
930AT_DATA_GRAMMAR([[input.y]],
931[[%debug
932
933%{
934# include <stdio.h>
cf806753 935# include <stdlib.h>
9350499c
JD
936 static void yyerror (const char *msg);
937 static int yylex (void);
938# define USE(SYM)
939%}
940
941%printer {
942 fprintf (yyoutput, "'%c'", $$);
3be03b13 943} %symbol-default
9350499c
JD
944%destructor {
945 fprintf (stderr, "DESTROY '%c'\n", $$);
3be03b13 946} %symbol-default
9350499c
JD
947
948%%
949
950start:
951 { $$ = 'S'; }
952 /* In order to reveal the problems that this bug caused during parsing, add
953 * $2 to USE. */
954 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
955 ;
956
957%%
958
959static int
960yylex (void)
961{
cf806753
PE
962 static char const input[] = "abd";
963 static size_t toknum;
964 if (! (toknum < sizeof input))
965 abort ();
966 yylval = input[toknum++];
9350499c
JD
967 return yylval;
968}
969
970static void
971yyerror (const char *msg)
972{
973 fprintf (stderr, "%s\n", msg);
974}
975
976int
977main (void)
978{
979 yydebug = 1;
980 return yyparse ();
981}
982]])
983
984AT_CHECK([bison -o input.c input.y])
985AT_COMPILE([input])
986AT_PARSER_CHECK([./input], [1], [],
987[[Starting parse
988Entering state 0
989Reading a token: Next token is token 'a' ('a')
990Shifting token 'a' ('a')
991Entering state 1
992Reading a token: Next token is token 'b' ('b')
993syntax error
994Shifting token error ()
995Entering state 3
996Next token is token 'b' ('b')
997Shifting token 'b' ('b')
998Entering state 5
999Reading a token: Next token is token $undefined ()
1000Error: popping token 'b' ('b')
1001DESTROY 'b'
1002Stack now 0 1 3
1003Error: popping token error ()
1004Stack now 0 1
1005Shifting token error ()
1006Entering state 3
1007Next token is token $undefined ()
1008Error: discarding token $undefined ()
1009Error: popping token error ()
1010Stack now 0 1
1011Shifting token error ()
1012Entering state 3
1013Reading a token: Now at end of input.
1014Cleanup: discarding lookahead token $end ()
1015Stack now 0 1 3
1016Cleanup: popping token error ()
1017Cleanup: popping token 'a' ('a')
1018DESTROY 'a'
1019]])
1020
1021AT_CLEANUP
1022
1023
1024
1025## ------------------------------------------------------ ##
1026## Default %printer and %destructor are not for $accept. ##
1027## ------------------------------------------------------ ##
1028
1029AT_SETUP([Default %printer and %destructor are not for \$accept])
1030
1031# If YYSTYPE is a union and Bison were to apply the default %printer and
1032# %destructor to $accept:
1033# - The %printer and %destructor code generated for $accept would always be
1034# dead code because $accept is currently never shifted onto the stack.
1035# - $$ for $accept would always be of type YYSTYPE because it's not possible
1036# to declare `%type <field> $accept'. (Also true for $undefined.)
1037# - Thus, the compiler might complain that the user code assumes the wrong
1038# type for $$ since the code might assume the type associated with a
1039# specific union field, which is especially reasonable in C++ since that
1040# type may be a base type. This test case checks for this problem. (Also
1041# true for $undefined and the error token, so there are three warnings for
1042# %printer and three for %destructor.)
1043
1044AT_DATA_GRAMMAR([[input.y]],
1045[[%debug /* So that %printer is actually compiled. */
1046
1047%{
1048# include <stdio.h>
cf806753 1049# include <stdlib.h>
9350499c
JD
1050 static void yyerror (const char *msg);
1051 static int yylex (void);
1052# define USE(SYM)
1053%}
1054
1055%printer {
1056 char chr = $$;
1057 fprintf (yyoutput, "'%c'", chr);
3be03b13 1058} %symbol-default
9350499c
JD
1059%destructor {
1060 char chr = $$;
1061 fprintf (stderr, "DESTROY '%c'\n", chr);
3be03b13 1062} %symbol-default
9350499c
JD
1063
1064%union { char chr; }
1065%type <chr> start
1066
1067%%
1068
1069start: { USE($$); } ;
1070
1071%%
1072
1073static int
1074yylex (void)
1075{
cf806753
PE
1076 static int called;
1077 if (called++)
1078 abort ();
9350499c
JD
1079 return 0;
1080}
1081
1082static void
1083yyerror (const char *msg)
1084{
1085 fprintf (stderr, "%s\n", msg);
1086}
1087
1088int
1089main (void)
1090{
1091 return yyparse ();
1092}
1093]])
1094
1095AT_CHECK([bison -o input.c input.y])
1096AT_COMPILE([input])
1097
1098AT_CLEANUP