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