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