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