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