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