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