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