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