]> git.saurik.com Git - bison.git/blame - tests/actions.at
tests: when using the C++ compiler, use its flags too.
[bison.git] / tests / actions.at
CommitLineData
82c035a8 1# Executing Actions. -*- Autotest -*-
6e30ede8 2
c932d613 3# Copyright (C) 2001-2012 Free Software 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
eb8c66bb
JD
161# Make sure that fields after $n or $-n are parsed correctly. At one
162# point while implementing dashes in symbol names, we were dropping
163# fields after $-n.
164AT_DATA_GRAMMAR([[input.y]],
165[[
166%{
167# include <stdio.h>
168 static int yylex (void);
169 static void yyerror (char const *msg);
170 typedef struct { int val; } stype;
171# define YYSTYPE stype
172%}
173
174%%
175start: one two { $$.val = $1.val + $2.val; } sum ;
176one: { $$.val = 1; } ;
177two: { $$.val = 2; } ;
178sum: { printf ("%d\n", $0.val + $-1.val + $-2.val); } ;
179
180%%
181
182static int
183yylex (void)
184{
185 return 0;
186}
187
188static void
189yyerror (char const *msg)
190{
191 fprintf (stderr, "%s\n", msg);
192}
193
194int
195main (void)
196{
197 return yyparse ();
198}
199]])
200
201AT_BISON_CHECK([[-o input.c input.y]])
202AT_COMPILE([[input]])
203AT_PARSER_CHECK([[./input]], [[0]],
204[[6
205]])
206
75d1fe16 207AT_CLEANUP
9280d3ef
AD
208
209
210
e776192e
AD
211## -------------------------- ##
212## Printers and Destructors. ##
213## -------------------------- ##
9280d3ef 214
ac700aa6
PE
215# _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
216# -----------------------------------------------------------------------------
3df37415 217m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
9c66f418
AD
218[# Make sure complex $n work.
219m4_if([$1$2$3], $[1]$[2]$[3], [],
3df37415
AD
220 [m4_fatal([$0: Invalid arguments: $@])])dnl
221
9c66f418
AD
222# Be sure to pass all the %directives to this macro to have correct
223# helping macros. So don't put any directly in the Bison file.
c2729758 224AT_BISON_OPTION_PUSHDEFS([$5])
9501dc6e 225AT_DATA_GRAMMAR([[input.y]],
16dc6a9e 226[[%code requires {
9280d3ef
AD
227#include <stdio.h>
228#include <stdlib.h>
cf806753 229#include <string.h>
9c66f418 230#include <assert.h>
80ce3401
PE
231
232#define YYINITDEPTH 10
233#define YYMAXDEPTH 10
c2729758
ADL
234]AT_LALR1_CC_IF(
235 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
236 [#define RANGE(Location) (Location).first_line, (Location).last_line])
9bc0dd67 237[}
9c66f418
AD
238
239$5]
240m4_ifval([$6], [%union
9280d3ef
AD
241{
242 int ival;
ac700aa6 243}])
16dc6a9e
JD
244AT_LALR1_CC_IF([%define global_tokens_and_yystype])
245m4_ifval([$6], [[%code provides {]], [[%code {]])
e8ec4d9b 246AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
c2729758 247[static int yylex (]AT_LEX_FORMALS[);
ca6f187f 248]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
9bc0dd67 249[}
c2729758 250
868d2d96 251]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
e3170060 252
868d2d96 253/* FIXME: This %printer isn't actually tested. */
a5eb1ed2
AD
254%printer
255 {
9a1e9989 256 ]AT_LALR1_CC_IF([debug_stream () << $$;],
3fc16193 257 [fprintf (yyoutput, "%d", $$)])[;
a5eb1ed2 258 }
9c66f418 259 input line thing 'x' 'y'
e3170060
AD
260
261%destructor
4c6cc1db 262 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
7bd6c77e 263 input
5719c109 264
7bd6c77e 265%destructor
4c6cc1db 266 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
7bd6c77e
AD
267 line
268
269%destructor
4c6cc1db 270 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
7bd6c77e
AD
271 thing
272
273%destructor
4c6cc1db 274 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
7bd6c77e 275 'x'
5719c109 276
9c66f418
AD
277%destructor
278 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
279 'y'
280
868d2d96
JD
281%token END 0
282%destructor
283 { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
284 END
285
9280d3ef 286%%
9c66f418
AD
287/*
288 This grammar is made to exercise error recovery.
289 "Lines" starting with `(' support error recovery, with
290 ')' as synchronizing token. Lines starting with 'x' can never
291 be recovered from if in error.
292*/
293
9280d3ef
AD
294input:
295 /* Nothing. */
5719c109
AD
296 {
297 $$ = 0;
16f37b35 298 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
5719c109
AD
299 }
300| line input /* Right recursive to load the stack so that popping at
868d2d96 301 END can be exercised. */
5719c109
AD
302 {
303 $$ = 2;
16f37b35 304 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
4c6cc1db 305 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
5719c109 306 }
9280d3ef
AD
307;
308
309line:
310 thing thing thing ';'
5719c109
AD
311 {
312 $$ = $1;
16f37b35 313 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
4c6cc1db 314 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
16f37b35 315 $3, RANGE (@3), $4, RANGE (@4));
5719c109 316 }
9c66f418 317| '(' thing thing ')'
5719c109
AD
318 {
319 $$ = $1;
9c66f418
AD
320 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
321 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
322 $3, RANGE (@3), $4, RANGE (@4));
5719c109 323 }
9c66f418 324| '(' thing ')'
5719c109
AD
325 {
326 $$ = $1;
9c66f418
AD
327 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
328 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
5719c109 329 }
9c66f418 330| '(' error ')'
5719c109
AD
331 {
332 $$ = -1;
9c66f418
AD
333 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
334 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
5719c109 335 }
9280d3ef
AD
336;
337
338thing:
5719c109
AD
339 'x'
340 {
341 $$ = $1;
4c6cc1db
AD
342 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
343 $$, RANGE (@$), $1, RANGE (@1));
5719c109 344 }
9280d3ef
AD
345;
346%%
9c66f418 347/* Alias to ARGV[1]. */
a9739e7c 348const char *source = 0;
9c66f418 349
9280d3ef 350static int
c2729758 351yylex (]AT_LEX_FORMALS[)
9280d3ef 352{
5a08f1ce 353 static unsigned int counter = 0;
9280d3ef 354
9c66f418
AD
355 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
356 /* As in BASIC, line numbers go from 10 to 10. */
c2729758 357]AT_LALR1_CC_IF(
9c66f418
AD
358[ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
359 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
c2729758 360],
9c66f418
AD
361[ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
362 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
c2729758 363])[
9c66f418 364
cf806753
PE
365 if (! (0 <= c && c <= strlen (source)))
366 abort ();
a9739e7c
PE
367 if (source[c])
368 printf ("sending: '%c'", source[c]);
9280d3ef 369 else
868d2d96 370 printf ("sending: END");
9c66f418 371 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
a9739e7c 372 return source[c];
9280d3ef
AD
373}
374
c2729758 375]AT_LALR1_CC_IF(
68e11668 376[/* A C++ error reporting function. */
c2729758 377void
99880de5 378yy::parser::error (const location& l, const std::string& m)
c2729758 379{
efeed023 380 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
c2729758
ADL
381}
382
383static bool yydebug;
384int
385yyparse ()
386{
99880de5 387 yy::parser parser;
a3cb6248 388 parser.set_debug_level (yydebug);
c2729758
ADL
389 return parser.parse ();
390}
391],
392[static void
9280d3ef
AD
393yyerror (const char *msg)
394{
4c6cc1db 395 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
c2729758 396}])[
9280d3ef 397
9280d3ef 398int
9c66f418 399main (int argc, const char *argv[])
9280d3ef 400{
6100a9aa 401 int status;
9280d3ef 402 yydebug = !!getenv ("YYDEBUG");
9c66f418 403 assert (argc == 2);
a9739e7c 404 source = argv[1];
6100a9aa
PE
405 status = yyparse ();
406 switch (status)
9280d3ef 407 {
6100a9aa
PE
408 case 0: printf ("Successful parse.\n"); break;
409 case 1: printf ("Parsing FAILED.\n"); break;
410 default: printf ("Parsing FAILED (status %d).\n", status); break;
9280d3ef 411 }
6100a9aa 412 return status;
9280d3ef
AD
413}
414]])
415
11c4e57d 416AT_FULL_COMPILE([input])
9c66f418
AD
417
418
419# Check the location of "empty"
420# -----------------------------
421# I.e., epsilon-reductions, as in "(x)" which ends by reducing
422# an empty "line" nterm.
423# FIXME: This location is not satisfying. Depend on the lookahead?
424AT_PARSER_CHECK([./input '(x)'], 0,
425[[sending: '(' (0@0-9)
426sending: 'x' (1@10-19)
427thing (1@10-19): 'x' (1@10-19)
428sending: ')' (2@20-29)
429line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
868d2d96 430sending: END (3@30-39)
b4a20338
AD
431input (0@29-29): /* Nothing */
432input (2@0-29): line (0@0-29) input (0@29-29)
868d2d96 433Freeing token END (3@30-39)
258b75ca 434Freeing nterm input (2@0-29)
9c66f418
AD
435Successful parse.
436]])
437
438
439# Check locations in error recovery
440# ---------------------------------
441# '(y)' is an error, but can be recovered from. But what's the location
442# of the error itself ('y'), and of the resulting reduction ('(error)').
443AT_PARSER_CHECK([./input '(y)'], 0,
444[[sending: '(' (0@0-9)
445sending: 'y' (1@10-19)
44610-19: syntax error, unexpected 'y', expecting 'x'
447Freeing token 'y' (1@10-19)
448sending: ')' (2@20-29)
449line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
868d2d96 450sending: END (3@30-39)
b4a20338
AD
451input (0@29-29): /* Nothing */
452input (2@0-29): line (-1@0-29) input (0@29-29)
868d2d96 453Freeing token END (3@30-39)
258b75ca 454Freeing nterm input (2@0-29)
9c66f418
AD
455Successful parse.
456]])
457
458
459# Syntax errors caught by the parser
460# ----------------------------------
461# Exercise the discarding of stack top and input until `error'
462# can be reduced.
463#
464# '(', 'x', 'x', 'x', 'x', 'x', ')',
465#
466# Load the stack and provoke an error that cannot be caught by the
467# grammar, to check that the stack is cleared. And make sure the
468# lookahead is freed.
469#
470# '(', 'x', ')',
471# '(', 'x', ')',
472# 'y'
473AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
474[[sending: '(' (0@0-9)
4c6cc1db
AD
475sending: 'x' (1@10-19)
476thing (1@10-19): 'x' (1@10-19)
477sending: 'x' (2@20-29)
478thing (2@20-29): 'x' (2@20-29)
479sending: 'x' (3@30-39)
9c66f418 48030-39: syntax error, unexpected 'x', expecting ')'
4c6cc1db
AD
481Freeing nterm thing (2@20-29)
482Freeing nterm thing (1@10-19)
4c6cc1db
AD
483Freeing token 'x' (3@30-39)
484sending: 'x' (4@40-49)
485Freeing token 'x' (4@40-49)
486sending: 'x' (5@50-59)
487Freeing token 'x' (5@50-59)
9c66f418
AD
488sending: ')' (6@60-69)
489line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
490sending: '(' (7@70-79)
4c6cc1db
AD
491sending: 'x' (8@80-89)
492thing (8@80-89): 'x' (8@80-89)
9c66f418
AD
493sending: ')' (9@90-99)
494line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
495sending: '(' (10@100-109)
496sending: 'x' (11@110-119)
497thing (11@110-119): 'x' (11@110-119)
498sending: ')' (12@120-129)
499line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
500sending: 'y' (13@130-139)
b4a20338
AD
501input (0@129-129): /* Nothing */
502input (2@100-129): line (10@100-129) input (0@129-129)
9c66f418
AD
503input (2@70-129): line (7@70-99) input (2@100-129)
504input (2@0-129): line (-1@0-69) input (2@70-129)
868d2d96 505130-139: syntax error, unexpected 'y', expecting END
9c66f418
AD
506Freeing nterm input (2@0-129)
507Freeing token 'y' (13@130-139)
5719c109 508Parsing FAILED.
9280d3ef
AD
509]])
510
868d2d96
JD
511
512# Syntax error caught by the parser where lookahead = END
513# --------------------------------------------------------
514# Load the stack and provoke an error that cannot be caught by the
515# grammar, to check that the stack is cleared. And make sure the
516# lookahead is freed.
517#
518# '(', 'x', ')',
519# '(', 'x', ')',
520# 'x'
521AT_PARSER_CHECK([./input '(x)(x)x'], 1,
522[[sending: '(' (0@0-9)
523sending: 'x' (1@10-19)
524thing (1@10-19): 'x' (1@10-19)
525sending: ')' (2@20-29)
526line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
527sending: '(' (3@30-39)
528sending: 'x' (4@40-49)
529thing (4@40-49): 'x' (4@40-49)
530sending: ')' (5@50-59)
531line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
532sending: 'x' (6@60-69)
533thing (6@60-69): 'x' (6@60-69)
534sending: END (7@70-79)
53570-79: syntax error, unexpected END, expecting 'x'
536Freeing nterm thing (6@60-69)
537Freeing nterm line (3@30-59)
538Freeing nterm line (0@0-29)
539Freeing token END (7@70-79)
540Parsing FAILED.
541]])
542
543
80ce3401
PE
544# Check destruction upon stack overflow
545# -------------------------------------
546# Upon stack overflow, all symbols on the stack should be destroyed.
547# Only check for yacc.c.
548AT_YACC_IF([
6100a9aa 549AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
80ce3401
PE
550[[sending: '(' (0@0-9)
551sending: 'x' (1@10-19)
552thing (1@10-19): 'x' (1@10-19)
553sending: ')' (2@20-29)
554line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
555sending: '(' (3@30-39)
556sending: 'x' (4@40-49)
557thing (4@40-49): 'x' (4@40-49)
558sending: ')' (5@50-59)
559line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
560sending: '(' (6@60-69)
561sending: 'x' (7@70-79)
562thing (7@70-79): 'x' (7@70-79)
563sending: ')' (8@80-89)
564line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
565sending: '(' (9@90-99)
566sending: 'x' (10@100-109)
567thing (10@100-109): 'x' (10@100-109)
568sending: ')' (11@110-119)
569line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
570sending: '(' (12@120-129)
571sending: 'x' (13@130-139)
572thing (13@130-139): 'x' (13@130-139)
573sending: ')' (14@140-149)
574line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
575sending: '(' (15@150-159)
576sending: 'x' (16@160-169)
577thing (16@160-169): 'x' (16@160-169)
578sending: ')' (17@170-179)
579line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
580sending: '(' (18@180-189)
581sending: 'x' (19@190-199)
582thing (19@190-199): 'x' (19@190-199)
583sending: ')' (20@200-209)
1a059451 584200-209: memory exhausted
80ce3401
PE
585Freeing nterm thing (19@190-199)
586Freeing nterm line (15@150-179)
587Freeing nterm line (12@120-149)
588Freeing nterm line (9@90-119)
589Freeing nterm line (6@60-89)
590Freeing nterm line (3@30-59)
591Freeing nterm line (0@0-29)
6100a9aa 592Parsing FAILED (status 2).
80ce3401
PE
593]])
594])
595
3df37415
AD
596])
597
598
a14a26fa 599# AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
046ac74e 600# ---------------------------------------------------------------------------
3df37415 601m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
9c66f418
AD
602[AT_SETUP([Printers and Destructors $2: $1])
603
a14a26fa 604$3
9c66f418
AD
605_AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
606[%error-verbose
607%debug
608%verbose
609%locations
610$1], [$2])
611
612AT_CLEANUP
3df37415
AD
613])
614
615
ac700aa6
PE
616AT_CHECK_PRINTER_AND_DESTRUCTOR([])
617AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
046ac74e 618
fd19f271
AD
619AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
620AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
046ac74e 621
1576d44d
AD
622AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
623AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
ec5479ce
JD
624
625
626
12e35840
JD
627## ----------------------------------------- ##
628## Default tagless %printer and %destructor. ##
629## ----------------------------------------- ##
ec5479ce
JD
630
631# Check that the right %printer and %destructor are called, that they're not
632# called for $end, and that $$ and @$ work correctly.
633
12e35840 634AT_SETUP([Default tagless %printer and %destructor])
ec5479ce
JD
635
636AT_DATA_GRAMMAR([[input.y]],
637[[%error-verbose
638%debug
639%locations
640%initial-action {
641 @$.first_line = @$.last_line = 1;
642 @$.first_column = @$.last_column = 1;
643}
644
645%{
646# include <stdio.h>
647# include <stdlib.h>
648 static void yyerror (const char *msg);
649 static int yylex (void);
650# define USE(SYM)
651%}
652
653%printer {
12e35840
JD
654 fprintf (yyoutput, "<*> printer should not be called.\n");
655} <*>
656
657%printer {
3ebecc24
JD
658 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
659} <>
ec5479ce 660%destructor {
3ebecc24
JD
661 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
662} <>
ec5479ce
JD
663
664%printer {
665 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
666} 'b' 'c'
667%destructor {
668 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
669} 'b' 'c'
670
12e35840
JD
671%destructor {
672 fprintf (yyoutput, "<*> destructor should not be called.\n");
673} <*>
674
ec5479ce
JD
675%%
676
677start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
678
679%%
680
681static int
682yylex (void)
683{
cf806753
PE
684 static char const input[] = "abcd";
685 static size_t toknum;
686 if (! (toknum < sizeof input))
687 abort ();
688 yylval = input[toknum++];
ec5479ce 689 yylloc.first_line = yylloc.last_line = 1;
cf806753 690 yylloc.first_column = yylloc.last_column = toknum;
ec5479ce
JD
691 return yylval;
692}
693
694static void
695yyerror (const char *msg)
696{
697 fprintf (stderr, "%s\n", msg);
698}
699
700int
701main (void)
702{
703 yydebug = 1;
704 return yyparse ();
705}
706]])
707
da730230 708AT_BISON_CHECK([-o input.c input.y])
ec5479ce
JD
709AT_COMPILE([input])
710AT_PARSER_CHECK([./input], 1,
3ebecc24 711[[<> destructor for 'd' @ 4.
ec5479ce
JD
712'b'/'c' destructor for 'c' @ 3.
713'b'/'c' destructor for 'b' @ 2.
3ebecc24 714<> destructor for 'a' @ 1.
ec5479ce
JD
715]],
716[[Starting parse
717Entering state 0
3ebecc24
JD
718Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1)
719Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1)
ec5479ce
JD
720Entering state 1
721Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
722Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
723Entering state 3
724Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
725Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
726Entering state 5
3ebecc24
JD
727Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4)
728Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4)
ec5479ce
JD
729Entering state 6
730Reading a token: Now at end of input.
731syntax error, unexpected $end, expecting 'e'
3ebecc24 732Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4)
ec5479ce
JD
733Stack now 0 1 3 5
734Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
735Stack now 0 1 3
736Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
737Stack now 0 1
3ebecc24 738Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1)
ec5479ce
JD
739Stack now 0
740Cleanup: discarding lookahead token $end (1.5-1.5: )
741Stack now 0
742]])
743
744AT_CLEANUP
745
746
747
12e35840
JD
748## ------------------------------------------------------ ##
749## Default tagged and per-type %printer and %destructor. ##
750## ------------------------------------------------------ ##
b2a0b7ca 751
12e35840 752AT_SETUP([Default tagged and per-type %printer and %destructor])
b2a0b7ca
JD
753
754AT_DATA_GRAMMAR([[input.y]],
755[[%error-verbose
756%debug
757
758%{
759# include <stdio.h>
760# include <stdlib.h>
761 static void yyerror (const char *msg);
762 static int yylex (void);
763# define USE(SYM)
764%}
765
12e35840 766%printer {
3ebecc24
JD
767 fprintf (yyoutput, "<> printer should not be called.\n");
768} <>
12e35840 769
b2a0b7ca
JD
770%union { int field0; int field1; int field2; }
771%type <field0> start 'a' 'g'
772%type <field1> 'e'
773%type <field2> 'f'
774%printer {
12e35840
JD
775 fprintf (yyoutput, "<*>/<field2>/e printer");
776} <*> 'e' <field2>
b2a0b7ca 777%destructor {
12e35840
JD
778 fprintf (stdout, "<*>/<field2>/e destructor.\n");
779} <*> 'e' <field2>
b2a0b7ca
JD
780
781%type <field1> 'b'
782%printer { fprintf (yyoutput, "<field1> printer"); } <field1>
783%destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
784
785%type <field0> 'c'
786%printer { fprintf (yyoutput, "'c' printer"); } 'c'
787%destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
788
789%type <field1> 'd'
790%printer { fprintf (yyoutput, "'d' printer"); } 'd'
791%destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
792
12e35840 793%destructor {
3ebecc24
JD
794 fprintf (yyoutput, "<> destructor should not be called.\n");
795} <>
12e35840 796
b2a0b7ca
JD
797%%
798
799start:
800 'a' 'b' 'c' 'd' 'e' 'f' 'g'
801 {
802 USE(($1, $2, $3, $4, $5, $6, $7));
803 $$ = 'S';
804 }
805 ;
806
807%%
808
809static int
810yylex (void)
811{
cf806753
PE
812 static char const input[] = "abcdef";
813 static size_t toknum;
814 if (! (toknum < sizeof input))
815 abort ();
816 return input[toknum++];
b2a0b7ca
JD
817}
818
819static void
820yyerror (const char *msg)
821{
822 fprintf (stderr, "%s\n", msg);
823}
824
825int
826main (void)
827{
828 yydebug = 1;
829 return yyparse ();
830}
831]])
832
da730230 833AT_BISON_CHECK([-o input.c input.y])
b2a0b7ca
JD
834AT_COMPILE([input])
835AT_PARSER_CHECK([./input], 1,
12e35840
JD
836[[<*>/<field2>/e destructor.
837<*>/<field2>/e destructor.
b2a0b7ca
JD
838'd' destructor.
839'c' destructor.
840<field1> destructor.
12e35840 841<*>/<field2>/e destructor.
b2a0b7ca
JD
842]],
843[[Starting parse
844Entering state 0
12e35840
JD
845Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
846Shifting token 'a' (<*>/<field2>/e printer)
b2a0b7ca
JD
847Entering state 1
848Reading a token: Next token is token 'b' (<field1> printer)
849Shifting token 'b' (<field1> printer)
850Entering state 3
851Reading a token: Next token is token 'c' ('c' printer)
852Shifting token 'c' ('c' printer)
853Entering state 5
854Reading a token: Next token is token 'd' ('d' printer)
855Shifting token 'd' ('d' printer)
856Entering state 6
12e35840
JD
857Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
858Shifting token 'e' (<*>/<field2>/e printer)
b2a0b7ca 859Entering state 7
12e35840
JD
860Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
861Shifting token 'f' (<*>/<field2>/e printer)
b2a0b7ca
JD
862Entering state 8
863Reading a token: Now at end of input.
864syntax error, unexpected $end, expecting 'g'
12e35840 865Error: popping token 'f' (<*>/<field2>/e printer)
b2a0b7ca 866Stack now 0 1 3 5 6 7
12e35840 867Error: popping token 'e' (<*>/<field2>/e printer)
b2a0b7ca
JD
868Stack now 0 1 3 5 6
869Error: popping token 'd' ('d' printer)
870Stack now 0 1 3 5
871Error: popping token 'c' ('c' printer)
872Stack now 0 1 3
873Error: popping token 'b' (<field1> printer)
874Stack now 0 1
12e35840 875Error: popping token 'a' (<*>/<field2>/e printer)
b2a0b7ca
JD
876Stack now 0
877Cleanup: discarding lookahead token $end ()
878Stack now 0
879]])
880
881AT_CLEANUP
882
883
884
ec5479ce 885## ------------------------------------------------------------- ##
12e35840 886## Default %printer and %destructor for user-defined end token. ##
ec5479ce
JD
887## ------------------------------------------------------------- ##
888
3508ce36 889AT_SETUP([Default %printer and %destructor for user-defined end token])
ec5479ce 890
12e35840
JD
891# _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED)
892# -----------------------------------------------------------------------------
893m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN],
894[m4_if($1, 0,
3ebecc24
JD
895 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
896 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
12e35840
JD
897
898AT_DATA_GRAMMAR([[input]]$1[[.y]],
ec5479ce
JD
899[[%error-verbose
900%debug
901%locations
902%initial-action {
903 @$.first_line = @$.last_line = 1;
904 @$.first_column = @$.last_column = 1;
905}
906
907%{
908# include <stdio.h>
909# include <stdlib.h>
910 static void yyerror (const char *msg);
911 static int yylex (void);
912# define USE(SYM)
913%}
914
12e35840
JD
915%destructor {
916 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
917} <]]not_kind[[>
918
ec5479ce
JD
919%token END 0
920%printer {
12e35840
JD
921 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
922} <]]kind[[>
ec5479ce 923%destructor {
12e35840
JD
924 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
925} <]]kind[[>
926
927%printer {
928 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
929} <]]not_kind[[>
930
931]]m4_if($1, 0, [[[
932]]],
933[[[%union { char tag; }
934%type <tag> start END]]])[[
ec5479ce
JD
935
936%%
937
938start: { $$ = 'S'; } ;
939
940%%
941
942static int
943yylex (void)
944{
cf806753
PE
945 static int called;
946 if (called++)
947 abort ();
12e35840 948 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
ec5479ce
JD
949 yylloc.first_line = yylloc.last_line = 1;
950 yylloc.first_column = yylloc.last_column = 1;
951 return 0;
952}
953
954static void
955yyerror (const char *msg)
956{
957 fprintf (stderr, "%s\n", msg);
958}
959
960int
961main (void)
962{
963 yydebug = 1;
964 return yyparse ();
965}
966]])
967
da730230 968AT_BISON_CHECK([-o input$1.c input$1.y])
12e35840
JD
969AT_COMPILE([input$1])
970AT_PARSER_CHECK([./input$1], 0,
971[[<]]kind[[> for 'E' @ 1.
972<]]kind[[> for 'S' @ 1.
ec5479ce
JD
973]],
974[[Starting parse
975Entering state 0
12e35840
JD
976Reducing stack by rule 1 (line 46):
977-> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
ec5479ce
JD
978Stack now 0
979Entering state 1
980Reading a token: Now at end of input.
12e35840 981Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
ec5479ce
JD
982Entering state 2
983Stack now 0 1 2
12e35840
JD
984Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
985Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
ec5479ce
JD
986]])
987
12e35840
JD
988m4_popdef([kind])
989m4_popdef([not_kind])
990])
991
992_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0)
993_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1)
994
ec5479ce 995AT_CLEANUP
9350499c
JD
996
997
998
999## ------------------------------------------------------------------ ##
1000## Default %printer and %destructor are not for error or $undefined. ##
1001## ------------------------------------------------------------------ ##
1002
287b314e 1003AT_SETUP([Default %printer and %destructor are not for error or $undefined])
9350499c
JD
1004
1005# If Bison were to apply the default %printer and %destructor to the error
1006# token or to $undefined:
1007# - For the error token:
1008# - It would generate warnings for unused $n.
1009# - It would invoke the %printer and %destructor on the error token's
1010# semantic value, which would be initialized from the lookahead, which
1011# would be destroyed separately.
1012# - For $undefined, who knows what the semantic value would be.
1013
1014AT_DATA_GRAMMAR([[input.y]],
1015[[%debug
1016
1017%{
1018# include <stdio.h>
cf806753 1019# include <stdlib.h>
9350499c
JD
1020 static void yyerror (const char *msg);
1021 static int yylex (void);
1022# define USE(SYM)
1023%}
1024
1025%printer {
1026 fprintf (yyoutput, "'%c'", $$);
3ebecc24 1027} <> <*>
9350499c
JD
1028%destructor {
1029 fprintf (stderr, "DESTROY '%c'\n", $$);
3ebecc24 1030} <> <*>
9350499c
JD
1031
1032%%
1033
1034start:
1035 { $$ = 'S'; }
1036 /* In order to reveal the problems that this bug caused during parsing, add
1037 * $2 to USE. */
1038 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
1039 ;
1040
1041%%
1042
1043static int
1044yylex (void)
1045{
cf806753
PE
1046 static char const input[] = "abd";
1047 static size_t toknum;
1048 if (! (toknum < sizeof input))
1049 abort ();
1050 yylval = input[toknum++];
9350499c
JD
1051 return yylval;
1052}
1053
1054static void
1055yyerror (const char *msg)
1056{
1057 fprintf (stderr, "%s\n", msg);
1058}
1059
1060int
1061main (void)
1062{
1063 yydebug = 1;
1064 return yyparse ();
1065}
1066]])
1067
da730230 1068AT_BISON_CHECK([-o input.c input.y])
9350499c
JD
1069AT_COMPILE([input])
1070AT_PARSER_CHECK([./input], [1], [],
1071[[Starting parse
1072Entering state 0
1073Reading a token: Next token is token 'a' ('a')
1074Shifting token 'a' ('a')
1075Entering state 1
1076Reading a token: Next token is token 'b' ('b')
1077syntax error
1078Shifting token error ()
1079Entering state 3
1080Next token is token 'b' ('b')
1081Shifting token 'b' ('b')
1082Entering state 5
1083Reading a token: Next token is token $undefined ()
1084Error: popping token 'b' ('b')
1085DESTROY 'b'
1086Stack now 0 1 3
1087Error: popping token error ()
1088Stack now 0 1
1089Shifting token error ()
1090Entering state 3
1091Next token is token $undefined ()
1092Error: discarding token $undefined ()
1093Error: popping token error ()
1094Stack now 0 1
1095Shifting token error ()
1096Entering state 3
1097Reading a token: Now at end of input.
1098Cleanup: discarding lookahead token $end ()
1099Stack now 0 1 3
1100Cleanup: popping token error ()
1101Cleanup: popping token 'a' ('a')
1102DESTROY 'a'
1103]])
1104
1105AT_CLEANUP
1106
1107
1108
1109## ------------------------------------------------------ ##
1110## Default %printer and %destructor are not for $accept. ##
1111## ------------------------------------------------------ ##
1112
287b314e 1113AT_SETUP([Default %printer and %destructor are not for $accept])
9350499c
JD
1114
1115# If YYSTYPE is a union and Bison were to apply the default %printer and
1116# %destructor to $accept:
1117# - The %printer and %destructor code generated for $accept would always be
1118# dead code because $accept is currently never shifted onto the stack.
1119# - $$ for $accept would always be of type YYSTYPE because it's not possible
1120# to declare `%type <field> $accept'. (Also true for $undefined.)
1121# - Thus, the compiler might complain that the user code assumes the wrong
1122# type for $$ since the code might assume the type associated with a
1123# specific union field, which is especially reasonable in C++ since that
1124# type may be a base type. This test case checks for this problem. (Also
1125# true for $undefined and the error token, so there are three warnings for
1126# %printer and three for %destructor.)
1127
1128AT_DATA_GRAMMAR([[input.y]],
1129[[%debug /* So that %printer is actually compiled. */
1130
1131%{
1132# include <stdio.h>
cf806753 1133# include <stdlib.h>
9350499c
JD
1134 static void yyerror (const char *msg);
1135 static int yylex (void);
1136# define USE(SYM)
1137%}
1138
1139%printer {
1140 char chr = $$;
1141 fprintf (yyoutput, "'%c'", chr);
3ebecc24 1142} <> <*>
9350499c
JD
1143%destructor {
1144 char chr = $$;
1145 fprintf (stderr, "DESTROY '%c'\n", chr);
3ebecc24 1146} <> <*>
9350499c
JD
1147
1148%union { char chr; }
1149%type <chr> start
1150
1151%%
1152
1153start: { USE($$); } ;
1154
1155%%
1156
1157static int
1158yylex (void)
1159{
cf806753
PE
1160 static int called;
1161 if (called++)
1162 abort ();
9350499c
JD
1163 return 0;
1164}
1165
1166static void
1167yyerror (const char *msg)
1168{
1169 fprintf (stderr, "%s\n", msg);
1170}
1171
1172int
1173main (void)
1174{
1175 return yyparse ();
1176}
1177]])
1178
da730230 1179AT_BISON_CHECK([-o input.c input.y])
9350499c
JD
1180AT_COMPILE([input])
1181
1182AT_CLEANUP
f91b1629
JD
1183
1184
1185
1186## ------------------------------------------------------ ##
1187## Default %printer and %destructor for mid-rule values. ##
1188## ------------------------------------------------------ ##
1189
1190AT_SETUP([Default %printer and %destructor for mid-rule values])
1191
1192AT_DATA_GRAMMAR([[input.y]],
1193[[%debug /* So that %printer is actually compiled. */
1194
1195%{
1196# include <stdio.h>
1197# include <stdlib.h>
1198 static void yyerror (const char *msg);
1199 static int yylex (void);
1200# define USE(SYM)
1201# define YYLTYPE int
c9e2da4f 1202# define YYLLOC_DEFAULT(Current, Rhs, N) (void)(Rhs)
f91b1629
JD
1203# define YY_LOCATION_PRINT(File, Loc)
1204%}
1205
3ebecc24
JD
1206%printer { fprintf (yyoutput, "%d", @$); } <>
1207%destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
12e35840
JD
1208%printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1209%destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
f91b1629
JD
1210
1211%%
1212
1213start:
1214 { @$ = 1; } // Not set or used.
1215 { USE ($$); @$ = 2; } // Both set and used.
1216 { USE ($$); @$ = 3; } // Only set.
1217 { @$ = 4; } // Only used.
1218 'c'
1219 { USE (($$, $2, $4, $5)); @$ = 0; }
1220 ;
1221
1222%%
1223
1224static int
1225yylex (void)
1226{
1227 static int called;
1228 if (called++)
1229 abort ();
1230 return 0;
1231}
1232
1233static void
1234yyerror (const char *msg)
1235{
1236 fprintf (stderr, "%s\n", msg);
1237}
1238
1239int
1240main (void)
1241{
1242 yydebug = 1;
1243 return yyparse ();
1244}
1245]])
1246
da730230 1247AT_BISON_CHECK([-o input.c input.y], 0,,
12e35840
JD
1248[[input.y:33.3-23: warning: unset value: $$
1249input.y:30.3-35.37: warning: unused value: $3
f91b1629
JD
1250]])
1251
1252AT_COMPILE([input])
1253AT_PARSER_CHECK([./input], 1,,
1254[[Starting parse
1255Entering state 0
12e35840 1256Reducing stack by rule 1 (line 30):
f91b1629
JD
1257-> $$ = nterm $@1 (: )
1258Stack now 0
1259Entering state 2
12e35840 1260Reducing stack by rule 2 (line 31):
f91b1629
JD
1261-> $$ = nterm @2 (: 2)
1262Stack now 0 2
1263Entering state 4
12e35840 1264Reducing stack by rule 3 (line 32):
f91b1629
JD
1265-> $$ = nterm @3 (: 3)
1266Stack now 0 2 4
1267Entering state 5
12e35840 1268Reducing stack by rule 4 (line 33):
f91b1629
JD
1269-> $$ = nterm @4 (: 4)
1270Stack now 0 2 4 5
1271Entering state 6
1272Reading a token: Now at end of input.
1273syntax error
1274Error: popping nterm @4 (: 4)
1275DESTROY 4
1276Stack now 0 2 4 5
1277Error: popping nterm @3 (: 3)
1278DESTROY 3
1279Stack now 0 2 4
1280Error: popping nterm @2 (: 2)
1281DESTROY 2
1282Stack now 0 2
1283Error: popping nterm $@1 (: )
1284Stack now 0
1285Cleanup: discarding lookahead token $end (: )
1286Stack now 0
1287]])
1288
1289AT_CLEANUP
e785ccf7
JD
1290
1291
1292## ----------------------- ##
1293## @$ implies %locations. ##
1294## ----------------------- ##
1295
1296# Bison once forgot to check for @$ in actions other than semantic actions.
1297
1298# AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1299# -------------------------------------------------------
1300m4_define([AT_CHECK_ACTION_LOCATIONS],
1301[AT_SETUP([[@$ in ]$1[ implies %locations]])
1302
1303AT_DATA_GRAMMAR([[input.y]],
1304[[%code {
1305 #include <stdio.h>
1306 static int yylex (void);
1307 static void yyerror (char const *msg);
1308}
1309
1310%debug
1311
1312]$1[ {
1313 printf ("%d\n", @$.first_line);
1314} ]m4_if($1, [%initial-action], [], [[start]])[
1315
1316%%
1317
1318start: ;
1319
1320%%
1321
1322static int
1323yylex (void)
1324{
1325 return 0;
1326}
1327
1328static void
1329yyerror (char const *msg)
1330{
1331 fprintf (stderr, "%s\n", msg);
1332}
1333
1334int
1335main (void)
1336{
1337 return yyparse ();
1338}
1339]])
1340
da730230 1341AT_BISON_CHECK([[-o input.c input.y]])
e785ccf7
JD
1342AT_COMPILE([[input]])
1343
1344AT_CLEANUP])
1345
1346AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1347AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1348AT_CHECK_ACTION_LOCATIONS([[%printer]])
42f4393a
DJ
1349
1350
1351## ----------------------------------------------- ##
1352## Fix user actions without a trailing semicolon. ##
1353## ----------------------------------------------- ##
1354
1355AT_SETUP([[Fix user actions without a trailing semicolon]])
1356
1357# This feature is undocumented, but we accidentally broke it in 2.3a,
1358# and there was a complaint at:
1359# <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>.
1360
1361AT_DATA([input.y],
1362[[%%
1363start: test2 test1 test0 testc;
1364
1365test2
1366: 'a' { semi; /* TEST:N:2 */ }
1367| 'b' { if (0) {no_semi} /* TEST:N:2 */ }
1368| 'c' { if (0) {semi;} /* TEST:N:2 */ }
1369| 'd' { semi; no_semi /* TEST:Y:2 */ }
1370| 'e' { semi(); no_semi() /* TEST:Y:2 */ }
1371| 'f' { semi[]; no_semi[] /* TEST:Y:2 */ }
1372| 'g' { semi++; no_semi++ /* TEST:Y:2 */ }
1373| 'h' { {no_semi} no_semi /* TEST:Y:2 */ }
1374| 'i' { {semi;} no_semi /* TEST:Y:2 */ }
1375;
1376test1
1377 : 'a' { semi; // TEST:N:1 ;
1378} | 'b' { if (0) {no_semi} // TEST:N:1 ;
1379} | 'c' { if (0) {semi;} // TEST:N:1 ;
1380} | 'd' { semi; no_semi // TEST:Y:1 ;
1381} | 'e' { semi(); no_semi() // TEST:Y:1 ;
1382} | 'f' { semi[]; no_semi[] // TEST:Y:1 ;
1383} | 'g' { semi++; no_semi++ // TEST:Y:1 ;
1384} | 'h' { {no_semi} no_semi // TEST:Y:1 ;
1385} | 'i' { {semi;} no_semi // TEST:Y:1 ;
1386} ;
1387test0
1388 : 'a' { semi; // TEST:N:1 {}
1389} | 'b' { if (0) {no_semi} // TEST:N:1 {}
1390} | 'c' { if (0) {semi;} // TEST:N:1 {}
1391} | 'd' { semi; no_semi // TEST:Y:1 {}
1392} | 'e' { semi(); no_semi() // TEST:Y:1 {}
1393} | 'f' { semi[]; no_semi[] // TEST:Y:1 {}
1394} | 'g' { semi++; no_semi++ // TEST:Y:1 {}
1395} | 'h' { {no_semi} no_semi // TEST:Y:1 {}
1396} | 'i' { {semi;} no_semi // TEST:Y:1 {}
1397} ;
1398
1399testc
1400: 'a' {
1401#define TEST_MACRO_N \
1402[]"broken\" $ @ $$ @$ [];\
1403string;"}
1404| 'b' {
1405no_semi
1406#define TEST_MACRO_N \
1407[]"broken\" $ @ $$ @$ [];\
1408string;"}
1409]])
1410
1411AT_BISON_CHECK([[-o input.c input.y]], [0], [],
9874f80b
JM
1412[[input.y:8.48: warning: a ';' might be needed at the end of action code
1413input.y:8.48: warning: future versions of Bison will not add the ';'
1414input.y:9.48: warning: a ';' might be needed at the end of action code
1415input.y:9.48: warning: future versions of Bison will not add the ';'
1416input.y:10.48: warning: a ';' might be needed at the end of action code
1417input.y:10.48: warning: future versions of Bison will not add the ';'
1418input.y:11.48: warning: a ';' might be needed at the end of action code
1419input.y:11.48: warning: future versions of Bison will not add the ';'
1420input.y:12.48: warning: a ';' might be needed at the end of action code
1421input.y:12.48: warning: future versions of Bison will not add the ';'
1422input.y:13.48: warning: a ';' might be needed at the end of action code
1423input.y:13.48: warning: future versions of Bison will not add the ';'
1424input.y:20.1: warning: a ';' might be needed at the end of action code
1425input.y:20.1: warning: future versions of Bison will not add the ';'
1426input.y:21.1: warning: a ';' might be needed at the end of action code
1427input.y:21.1: warning: future versions of Bison will not add the ';'
1428input.y:22.1: warning: a ';' might be needed at the end of action code
1429input.y:22.1: warning: future versions of Bison will not add the ';'
1430input.y:23.1: warning: a ';' might be needed at the end of action code
1431input.y:23.1: warning: future versions of Bison will not add the ';'
1432input.y:24.1: warning: a ';' might be needed at the end of action code
1433input.y:24.1: warning: future versions of Bison will not add the ';'
1434input.y:25.1: warning: a ';' might be needed at the end of action code
1435input.y:25.1: warning: future versions of Bison will not add the ';'
1436input.y:31.1: warning: a ';' might be needed at the end of action code
1437input.y:31.1: warning: future versions of Bison will not add the ';'
1438input.y:32.1: warning: a ';' might be needed at the end of action code
1439input.y:32.1: warning: future versions of Bison will not add the ';'
1440input.y:33.1: warning: a ';' might be needed at the end of action code
1441input.y:33.1: warning: future versions of Bison will not add the ';'
1442input.y:34.1: warning: a ';' might be needed at the end of action code
1443input.y:34.1: warning: future versions of Bison will not add the ';'
1444input.y:35.1: warning: a ';' might be needed at the end of action code
1445input.y:35.1: warning: future versions of Bison will not add the ';'
1446input.y:36.1: warning: a ';' might be needed at the end of action code
1447input.y:36.1: warning: future versions of Bison will not add the ';'
42f4393a 1448]])
c4fae1ef
AD
1449
1450AT_MATCHES_CHECK([input.c], [[/\* TEST:N:2 \*/ \}$]], [[3]])
1451AT_MATCHES_CHECK([input.c], [[/\* TEST:Y:2 \*/ ;\}$]], [[6]])
1452AT_MATCHES_CHECK([input.c], [[// TEST:N:1 [;{}]*\n\}$]], [[6]])
1453AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]])
1454AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]])
42f4393a
DJ
1455
1456AT_CLEANUP
abcc7c03
JD
1457
1458
1459## -------------------------------------------------- ##
1460## Destroying lookahead assigned by semantic action. ##
1461## -------------------------------------------------- ##
1462
1463AT_SETUP([[Destroying lookahead assigned by semantic action]])
1464
1465AT_DATA_GRAMMAR([input.y],
1466[[
1467%code {
1468 #include <assert.h>
1469 #include <stdio.h>
1470 static void yyerror (char const *);
1471 static int yylex (void);
1472 #define USE(Var)
1473}
1474
1475%destructor { fprintf (stderr, "'a' destructor\n"); } 'a'
1476%destructor { fprintf (stderr, "'b' destructor\n"); } 'b'
1477
1478%%
1479
1480// In a previous version of Bison, yychar assigned by the semantic
1481// action below was not translated into yytoken before the lookahead was
1482// discarded and thus before its destructor (selected according to
1483// yytoken) was called in order to return from yyparse. This would
1484// happen even if YYACCEPT was performed in a later semantic action as
1485// long as only consistent states with default reductions were visited
1486// in between. However, we leave YYACCEPT in the same semantic action
1487// for this test in order to show that skeletons cannot simply translate
1488// immediately after every semantic action because a semantic action
1489// that has set yychar might not always return normally. Instead,
1490// skeletons must translate before every use of yytoken.
1491start: 'a' accept { USE($1); } ;
1492accept: /*empty*/ {
1493 assert (yychar == YYEMPTY);
1494 yychar = 'b';
1495 YYACCEPT;
1496} ;
1497
1498%%
1499
1500static void
1501yyerror (char const *msg)
1502{
1503 fprintf (stderr, "%s\n", msg);
1504}
1505
1506static int
1507yylex (void)
1508{
1509 static char const *input = "a";
1510 return *input++;
1511}
1512
1513int
1514main (void)
1515{
1516 return yyparse ();
1517}
1518]])
1519
1520AT_BISON_CHECK([[-o input.c input.y]])
1521AT_COMPILE([[input]])
1522AT_PARSER_CHECK([[./input]], [[0]], [],
1523[['b' destructor
1524'a' destructor
1525]])
1526
1527AT_CLEANUP
94556574
AD
1528
1529## ---------- ##
1530## YYBACKUP. ##
1531## ---------- ##
1532
1533AT_SETUP([[YYBACKUP]])
1534
1535AT_DATA_GRAMMAR([input.y],
1536[[
1537%error-verbose
1538%debug
1539%pure-parser
1540%code {
1541# include <stdio.h>
1542# include <stdlib.h>
1543# include <assert.h>
1544
1545 static void yyerror (const char *msg);
1546 static int yylex (YYSTYPE *yylval);
1547}
1548%%
1549input:
1550 exp exp {}
1551;
1552
1553exp:
1554 'a' { printf ("a: %d\n", $1); }
1555| 'b' { YYBACKUP('a', 123); }
1556| 'c' 'd' { YYBACKUP('a', 456); }
1557;
1558
1559%%
1560static int
1561yylex (YYSTYPE *yylval)
1562{
1563 static char const input[] = "bcd";
1564 static size_t toknum;
1565 assert (toknum < sizeof input);
1566 *yylval = (toknum + 1) * 10;
1567 return input[toknum++];
1568}
1569
1570static void
1571yyerror (const char *msg)
1572{
1573 fprintf (stderr, "%s\n", msg);
1574}
1575
1576int
1577main (void)
1578{
1579 yydebug = !!getenv("YYDEBUG");
1580 return yyparse ();
1581}
1582]])
1583
1584AT_BISON_CHECK([[-o input.c input.y]])
1585AT_COMPILE([[input]])
1586AT_PARSER_CHECK([[./input]], [[0]],
1587[[a: 123
1588a: 456
94556574
AD
1589]])
1590
1591AT_CLEANUP