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