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