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