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