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