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