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