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