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