]> git.saurik.com Git - bison.git/blame_incremental - tests/actions.at
* NEWS, data/c++-skel.m4, data/c++.m4, data/c-skel.m4, data/c.m4,
[bison.git] / tests / actions.at
... / ...
CommitLineData
1# Executing Actions. -*- Autotest -*-
2# Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
3# 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 2, or (at your option)
8# 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, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18# 02110-1301, USA.
19
20AT_BANNER([[User Actions.]])
21
22## ------------------ ##
23## Mid-rule actions. ##
24## ------------------ ##
25
26AT_SETUP([Mid-rule actions])
27
28# Bison once forgot the mid-rule actions. It was because the action
29# was attached to the host rule (the one with the mid-rule action),
30# instead of being attached to the empty rule dedicated to this
31# action.
32
33AT_DATA_GRAMMAR([[input.y]],
34[[%error-verbose
35%debug
36%{
37# include <stdio.h>
38# include <stdlib.h>
39 static void yyerror (const char *msg);
40 static int yylex (void);
41%}
42%%
43exp: { putchar ('0'); }
44 '1' { putchar ('1'); }
45 '2' { putchar ('2'); }
46 '3' { putchar ('3'); }
47 '4' { putchar ('4'); }
48 '5' { putchar ('5'); }
49 '6' { putchar ('6'); }
50 '7' { putchar ('7'); }
51 '8' { putchar ('8'); }
52 '9' { putchar ('9'); }
53 { putchar ('\n'); }
54 ;
55%%
56static int
57yylex (void)
58{
59 static char const input[] = "123456789";
60 static size_t toknum;
61 if (! (toknum < sizeof input))
62 abort ();
63 return input[toknum++];
64}
65
66static void
67yyerror (const char *msg)
68{
69 fprintf (stderr, "%s\n", msg);
70}
71
72int
73main (void)
74{
75 return yyparse ();
76}
77]])
78
79AT_CHECK([bison -d -v -o input.c input.y])
80AT_COMPILE([input])
81AT_PARSER_CHECK([./input], 0,
82[[0123456789
83]])
84
85AT_CLEANUP
86
87
88
89
90
91## ---------------- ##
92## Exotic Dollars. ##
93## ---------------- ##
94
95AT_SETUP([Exotic Dollars])
96
97AT_DATA_GRAMMAR([[input.y]],
98[[%error-verbose
99%debug
100%{
101# include <stdio.h>
102# include <stdlib.h>
103 static void yyerror (const char *msg);
104 static int yylex (void);
105# define USE(Var)
106%}
107
108%union
109{
110 int val;
111};
112
113%type <val> a_1 a_2 a_5
114 sum_of_the_five_previous_values
115
116%%
117exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
118 sum_of_the_five_previous_values
119 {
120 USE (($1, $2, $<foo>3, $<foo>4, $5));
121 printf ("%d\n", $6);
122 }
123;
124a_1: { $$ = 1; };
125a_2: { $$ = 2; };
126a_5: { $$ = 5; };
127
128sum_of_the_five_previous_values:
129 {
130 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
131 }
132;
133
134%%
135static int
136yylex (void)
137{
138 static int called;
139 if (called++)
140 abort ();
141 return EOF;
142}
143
144static void
145yyerror (const char *msg)
146{
147 fprintf (stderr, "%s\n", msg);
148}
149
150int
151main (void)
152{
153 return yyparse ();
154}
155]])
156
157AT_CHECK([bison -d -v -o input.c input.y], 0)
158AT_COMPILE([input])
159AT_PARSER_CHECK([./input], 0,
160[[15
161]])
162
163AT_CLEANUP
164
165
166
167## -------------------------- ##
168## Printers and Destructors. ##
169## -------------------------- ##
170
171# _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
172# -----------------------------------------------------------------------------
173m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
174[# Make sure complex $n work.
175m4_if([$1$2$3], $[1]$[2]$[3], [],
176 [m4_fatal([$0: Invalid arguments: $@])])dnl
177
178# Be sure to pass all the %directives to this macro to have correct
179# helping macros. So don't put any directly in the Bison file.
180AT_BISON_OPTION_PUSHDEFS([$5])
181AT_DATA_GRAMMAR([[input.y]],
182[[%code requires {
183#include <stdio.h>
184#include <stdlib.h>
185#include <string.h>
186#include <assert.h>
187
188#define YYINITDEPTH 10
189#define YYMAXDEPTH 10
190]AT_LALR1_CC_IF(
191 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
192 [#define RANGE(Location) (Location).first_line, (Location).last_line])
193[}
194
195$5]
196m4_ifval([$6], [%union
197{
198 int ival;
199}])
200AT_LALR1_CC_IF([%define global_tokens_and_yystype])
201m4_ifval([$6], [[%code provides {]], [[%code {]])
202AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
203[static int yylex (]AT_LEX_FORMALS[);
204]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
205[}
206
207]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
208
209/* FIXME: This %printer isn't actually tested. */
210%printer
211 {
212 ]AT_LALR1_CC_IF([debug_stream () << $$;],
213 [fprintf (yyoutput, "%d", $$)])[;
214 }
215 input line thing 'x' 'y'
216
217%destructor
218 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
219 input
220
221%destructor
222 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
223 line
224
225%destructor
226 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
227 thing
228
229%destructor
230 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
231 'x'
232
233%destructor
234 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
235 'y'
236
237%token END 0
238%destructor
239 { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
240 END
241
242%%
243/*
244 This grammar is made to exercise error recovery.
245 "Lines" starting with `(' support error recovery, with
246 ')' as synchronizing token. Lines starting with 'x' can never
247 be recovered from if in error.
248*/
249
250input:
251 /* Nothing. */
252 {
253 $$ = 0;
254 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
255 }
256| line input /* Right recursive to load the stack so that popping at
257 END can be exercised. */
258 {
259 $$ = 2;
260 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
261 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
262 }
263;
264
265line:
266 thing thing thing ';'
267 {
268 $$ = $1;
269 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
270 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
271 $3, RANGE (@3), $4, RANGE (@4));
272 }
273| '(' thing thing ')'
274 {
275 $$ = $1;
276 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
277 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
278 $3, RANGE (@3), $4, RANGE (@4));
279 }
280| '(' thing ')'
281 {
282 $$ = $1;
283 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
284 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
285 }
286| '(' error ')'
287 {
288 $$ = -1;
289 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
290 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
291 }
292;
293
294thing:
295 'x'
296 {
297 $$ = $1;
298 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
299 $$, RANGE (@$), $1, RANGE (@1));
300 }
301;
302%%
303/* Alias to ARGV[1]. */
304const char *source = 0;
305
306static int
307yylex (]AT_LEX_FORMALS[)
308{
309 static unsigned int counter = 0;
310
311 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
312 /* As in BASIC, line numbers go from 10 to 10. */
313]AT_LALR1_CC_IF(
314[ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
315 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
316],
317[ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
318 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
319])[
320
321 if (! (0 <= c && c <= strlen (source)))
322 abort ();
323 if (source[c])
324 printf ("sending: '%c'", source[c]);
325 else
326 printf ("sending: END");
327 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
328 return source[c];
329}
330
331]AT_LALR1_CC_IF(
332[/* A C++ error reporting function. */
333void
334yy::parser::error (const location& l, const std::string& m)
335{
336 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
337}
338
339static bool yydebug;
340int
341yyparse ()
342{
343 yy::parser parser;
344 parser.set_debug_level (yydebug);
345 return parser.parse ();
346}
347],
348[static void
349yyerror (const char *msg)
350{
351 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
352}])[
353
354int
355main (int argc, const char *argv[])
356{
357 int status;
358 yydebug = !!getenv ("YYDEBUG");
359 assert (argc == 2);
360 source = argv[1];
361 status = yyparse ();
362 switch (status)
363 {
364 case 0: printf ("Successful parse.\n"); break;
365 case 1: printf ("Parsing FAILED.\n"); break;
366 default: printf ("Parsing FAILED (status %d).\n", status); break;
367 }
368 return status;
369}
370]])
371
372AT_LALR1_CC_IF(
373 [AT_CHECK([bison -o input.cc input.y])
374 AT_COMPILE_CXX([input])],
375 [AT_CHECK([bison -o input.c input.y])
376 AT_COMPILE([input])])
377
378
379# Check the location of "empty"
380# -----------------------------
381# I.e., epsilon-reductions, as in "(x)" which ends by reducing
382# an empty "line" nterm.
383# FIXME: This location is not satisfying. Depend on the lookahead?
384AT_PARSER_CHECK([./input '(x)'], 0,
385[[sending: '(' (0@0-9)
386sending: 'x' (1@10-19)
387thing (1@10-19): 'x' (1@10-19)
388sending: ')' (2@20-29)
389line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
390sending: END (3@30-39)
391input (0@29-29): /* Nothing */
392input (2@0-29): line (0@0-29) input (0@29-29)
393Freeing token END (3@30-39)
394Freeing nterm input (2@0-29)
395Successful parse.
396]])
397
398
399# Check locations in error recovery
400# ---------------------------------
401# '(y)' is an error, but can be recovered from. But what's the location
402# of the error itself ('y'), and of the resulting reduction ('(error)').
403AT_PARSER_CHECK([./input '(y)'], 0,
404[[sending: '(' (0@0-9)
405sending: 'y' (1@10-19)
40610-19: syntax error, unexpected 'y', expecting 'x'
407Freeing token 'y' (1@10-19)
408sending: ')' (2@20-29)
409line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
410sending: END (3@30-39)
411input (0@29-29): /* Nothing */
412input (2@0-29): line (-1@0-29) input (0@29-29)
413Freeing token END (3@30-39)
414Freeing nterm input (2@0-29)
415Successful parse.
416]])
417
418
419# Syntax errors caught by the parser
420# ----------------------------------
421# Exercise the discarding of stack top and input until `error'
422# can be reduced.
423#
424# '(', 'x', 'x', 'x', 'x', 'x', ')',
425#
426# Load the stack and provoke an error that cannot be caught by the
427# grammar, to check that the stack is cleared. And make sure the
428# lookahead is freed.
429#
430# '(', 'x', ')',
431# '(', 'x', ')',
432# 'y'
433AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
434[[sending: '(' (0@0-9)
435sending: 'x' (1@10-19)
436thing (1@10-19): 'x' (1@10-19)
437sending: 'x' (2@20-29)
438thing (2@20-29): 'x' (2@20-29)
439sending: 'x' (3@30-39)
44030-39: syntax error, unexpected 'x', expecting ')'
441Freeing nterm thing (2@20-29)
442Freeing nterm thing (1@10-19)
443Freeing token 'x' (3@30-39)
444sending: 'x' (4@40-49)
445Freeing token 'x' (4@40-49)
446sending: 'x' (5@50-59)
447Freeing token 'x' (5@50-59)
448sending: ')' (6@60-69)
449line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
450sending: '(' (7@70-79)
451sending: 'x' (8@80-89)
452thing (8@80-89): 'x' (8@80-89)
453sending: ')' (9@90-99)
454line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
455sending: '(' (10@100-109)
456sending: 'x' (11@110-119)
457thing (11@110-119): 'x' (11@110-119)
458sending: ')' (12@120-129)
459line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
460sending: 'y' (13@130-139)
461input (0@129-129): /* Nothing */
462input (2@100-129): line (10@100-129) input (0@129-129)
463input (2@70-129): line (7@70-99) input (2@100-129)
464input (2@0-129): line (-1@0-69) input (2@70-129)
465130-139: syntax error, unexpected 'y', expecting END
466Freeing nterm input (2@0-129)
467Freeing token 'y' (13@130-139)
468Parsing FAILED.
469]])
470
471
472# Syntax error caught by the parser where lookahead = END
473# --------------------------------------------------------
474# Load the stack and provoke an error that cannot be caught by the
475# grammar, to check that the stack is cleared. And make sure the
476# lookahead is freed.
477#
478# '(', 'x', ')',
479# '(', 'x', ')',
480# 'x'
481AT_PARSER_CHECK([./input '(x)(x)x'], 1,
482[[sending: '(' (0@0-9)
483sending: 'x' (1@10-19)
484thing (1@10-19): 'x' (1@10-19)
485sending: ')' (2@20-29)
486line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
487sending: '(' (3@30-39)
488sending: 'x' (4@40-49)
489thing (4@40-49): 'x' (4@40-49)
490sending: ')' (5@50-59)
491line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
492sending: 'x' (6@60-69)
493thing (6@60-69): 'x' (6@60-69)
494sending: END (7@70-79)
49570-79: syntax error, unexpected END, expecting 'x'
496Freeing nterm thing (6@60-69)
497Freeing nterm line (3@30-59)
498Freeing nterm line (0@0-29)
499Freeing token END (7@70-79)
500Parsing FAILED.
501]])
502
503
504# Check destruction upon stack overflow
505# -------------------------------------
506# Upon stack overflow, all symbols on the stack should be destroyed.
507# Only check for yacc.c.
508AT_YACC_IF([
509AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
510[[sending: '(' (0@0-9)
511sending: 'x' (1@10-19)
512thing (1@10-19): 'x' (1@10-19)
513sending: ')' (2@20-29)
514line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
515sending: '(' (3@30-39)
516sending: 'x' (4@40-49)
517thing (4@40-49): 'x' (4@40-49)
518sending: ')' (5@50-59)
519line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
520sending: '(' (6@60-69)
521sending: 'x' (7@70-79)
522thing (7@70-79): 'x' (7@70-79)
523sending: ')' (8@80-89)
524line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
525sending: '(' (9@90-99)
526sending: 'x' (10@100-109)
527thing (10@100-109): 'x' (10@100-109)
528sending: ')' (11@110-119)
529line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
530sending: '(' (12@120-129)
531sending: 'x' (13@130-139)
532thing (13@130-139): 'x' (13@130-139)
533sending: ')' (14@140-149)
534line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
535sending: '(' (15@150-159)
536sending: 'x' (16@160-169)
537thing (16@160-169): 'x' (16@160-169)
538sending: ')' (17@170-179)
539line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
540sending: '(' (18@180-189)
541sending: 'x' (19@190-199)
542thing (19@190-199): 'x' (19@190-199)
543sending: ')' (20@200-209)
544200-209: memory exhausted
545Freeing nterm thing (19@190-199)
546Freeing nterm line (15@150-179)
547Freeing nterm line (12@120-149)
548Freeing nterm line (9@90-119)
549Freeing nterm line (6@60-89)
550Freeing nterm line (3@30-59)
551Freeing nterm line (0@0-29)
552Parsing FAILED (status 2).
553]])
554])
555
556])
557
558
559# AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
560# ---------------------------------------------------------------------------
561m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
562[AT_SETUP([Printers and Destructors $2: $1])
563
564$3
565_AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
566[%error-verbose
567%debug
568%verbose
569%locations
570$1], [$2])
571
572AT_CLEANUP
573])
574
575
576AT_CHECK_PRINTER_AND_DESTRUCTOR([])
577AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
578
579AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
580AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
581
582AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
583AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
584
585
586
587## ----------------------------------------- ##
588## Default tagless %printer and %destructor. ##
589## ----------------------------------------- ##
590
591# Check that the right %printer and %destructor are called, that they're not
592# called for $end, and that $$ and @$ work correctly.
593
594AT_SETUP([Default tagless %printer and %destructor])
595
596AT_DATA_GRAMMAR([[input.y]],
597[[%error-verbose
598%debug
599%locations
600%initial-action {
601 @$.first_line = @$.last_line = 1;
602 @$.first_column = @$.last_column = 1;
603}
604
605%{
606# include <stdio.h>
607# include <stdlib.h>
608 static void yyerror (const char *msg);
609 static int yylex (void);
610# define USE(SYM)
611%}
612
613%printer {
614 fprintf (yyoutput, "<*> printer should not be called.\n");
615} <*>
616
617%printer {
618 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
619} <>
620%destructor {
621 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
622} <>
623
624%printer {
625 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
626} 'b' 'c'
627%destructor {
628 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
629} 'b' 'c'
630
631%destructor {
632 fprintf (yyoutput, "<*> destructor should not be called.\n");
633} <*>
634
635%%
636
637start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
638
639%%
640
641static int
642yylex (void)
643{
644 static char const input[] = "abcd";
645 static size_t toknum;
646 if (! (toknum < sizeof input))
647 abort ();
648 yylval = input[toknum++];
649 yylloc.first_line = yylloc.last_line = 1;
650 yylloc.first_column = yylloc.last_column = toknum;
651 return yylval;
652}
653
654static void
655yyerror (const char *msg)
656{
657 fprintf (stderr, "%s\n", msg);
658}
659
660int
661main (void)
662{
663 yydebug = 1;
664 return yyparse ();
665}
666]])
667
668AT_CHECK([bison -o input.c input.y])
669AT_COMPILE([input])
670AT_PARSER_CHECK([./input], 1,
671[[<> destructor for 'd' @ 4.
672'b'/'c' destructor for 'c' @ 3.
673'b'/'c' destructor for 'b' @ 2.
674<> destructor for 'a' @ 1.
675]],
676[[Starting parse
677Entering state 0
678Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1)
679Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1)
680Entering state 1
681Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
682Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
683Entering state 3
684Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
685Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
686Entering state 5
687Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4)
688Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4)
689Entering state 6
690Reading a token: Now at end of input.
691syntax error, unexpected $end, expecting 'e'
692Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4)
693Stack now 0 1 3 5
694Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
695Stack now 0 1 3
696Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
697Stack now 0 1
698Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1)
699Stack now 0
700Cleanup: discarding lookahead token $end (1.5-1.5: )
701Stack now 0
702]])
703
704AT_CLEANUP
705
706
707
708## ------------------------------------------------------ ##
709## Default tagged and per-type %printer and %destructor. ##
710## ------------------------------------------------------ ##
711
712AT_SETUP([Default tagged and per-type %printer and %destructor])
713
714AT_DATA_GRAMMAR([[input.y]],
715[[%error-verbose
716%debug
717
718%{
719# include <stdio.h>
720# include <stdlib.h>
721 static void yyerror (const char *msg);
722 static int yylex (void);
723# define USE(SYM)
724%}
725
726%printer {
727 fprintf (yyoutput, "<> printer should not be called.\n");
728} <>
729
730%union { int field0; int field1; int field2; }
731%type <field0> start 'a' 'g'
732%type <field1> 'e'
733%type <field2> 'f'
734%printer {
735 fprintf (yyoutput, "<*>/<field2>/e printer");
736} <*> 'e' <field2>
737%destructor {
738 fprintf (stdout, "<*>/<field2>/e destructor.\n");
739} <*> 'e' <field2>
740
741%type <field1> 'b'
742%printer { fprintf (yyoutput, "<field1> printer"); } <field1>
743%destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
744
745%type <field0> 'c'
746%printer { fprintf (yyoutput, "'c' printer"); } 'c'
747%destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
748
749%type <field1> 'd'
750%printer { fprintf (yyoutput, "'d' printer"); } 'd'
751%destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
752
753%destructor {
754 fprintf (yyoutput, "<> destructor should not be called.\n");
755} <>
756
757%%
758
759start:
760 'a' 'b' 'c' 'd' 'e' 'f' 'g'
761 {
762 USE(($1, $2, $3, $4, $5, $6, $7));
763 $$ = 'S';
764 }
765 ;
766
767%%
768
769static int
770yylex (void)
771{
772 static char const input[] = "abcdef";
773 static size_t toknum;
774 if (! (toknum < sizeof input))
775 abort ();
776 return input[toknum++];
777}
778
779static void
780yyerror (const char *msg)
781{
782 fprintf (stderr, "%s\n", msg);
783}
784
785int
786main (void)
787{
788 yydebug = 1;
789 return yyparse ();
790}
791]])
792
793AT_CHECK([bison -o input.c input.y])
794AT_COMPILE([input])
795AT_PARSER_CHECK([./input], 1,
796[[<*>/<field2>/e destructor.
797<*>/<field2>/e destructor.
798'd' destructor.
799'c' destructor.
800<field1> destructor.
801<*>/<field2>/e destructor.
802]],
803[[Starting parse
804Entering state 0
805Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
806Shifting token 'a' (<*>/<field2>/e printer)
807Entering state 1
808Reading a token: Next token is token 'b' (<field1> printer)
809Shifting token 'b' (<field1> printer)
810Entering state 3
811Reading a token: Next token is token 'c' ('c' printer)
812Shifting token 'c' ('c' printer)
813Entering state 5
814Reading a token: Next token is token 'd' ('d' printer)
815Shifting token 'd' ('d' printer)
816Entering state 6
817Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
818Shifting token 'e' (<*>/<field2>/e printer)
819Entering state 7
820Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
821Shifting token 'f' (<*>/<field2>/e printer)
822Entering state 8
823Reading a token: Now at end of input.
824syntax error, unexpected $end, expecting 'g'
825Error: popping token 'f' (<*>/<field2>/e printer)
826Stack now 0 1 3 5 6 7
827Error: popping token 'e' (<*>/<field2>/e printer)
828Stack now 0 1 3 5 6
829Error: popping token 'd' ('d' printer)
830Stack now 0 1 3 5
831Error: popping token 'c' ('c' printer)
832Stack now 0 1 3
833Error: popping token 'b' (<field1> printer)
834Stack now 0 1
835Error: popping token 'a' (<*>/<field2>/e printer)
836Stack now 0
837Cleanup: discarding lookahead token $end ()
838Stack now 0
839]])
840
841AT_CLEANUP
842
843
844
845## ------------------------------------------------------------- ##
846## Default %printer and %destructor for user-defined end token. ##
847## ------------------------------------------------------------- ##
848
849AT_SETUP([Default %printer and %destructor for user-defined end token])
850
851# _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED)
852# -----------------------------------------------------------------------------
853m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN],
854[m4_if($1, 0,
855 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
856 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
857
858AT_DATA_GRAMMAR([[input]]$1[[.y]],
859[[%error-verbose
860%debug
861%locations
862%initial-action {
863 @$.first_line = @$.last_line = 1;
864 @$.first_column = @$.last_column = 1;
865}
866
867%{
868# include <stdio.h>
869# include <stdlib.h>
870 static void yyerror (const char *msg);
871 static int yylex (void);
872# define USE(SYM)
873%}
874
875%destructor {
876 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
877} <]]not_kind[[>
878
879%token END 0
880%printer {
881 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
882} <]]kind[[>
883%destructor {
884 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
885} <]]kind[[>
886
887%printer {
888 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
889} <]]not_kind[[>
890
891]]m4_if($1, 0, [[[
892]]],
893[[[%union { char tag; }
894%type <tag> start END]]])[[
895
896%%
897
898start: { $$ = 'S'; } ;
899
900%%
901
902static int
903yylex (void)
904{
905 static int called;
906 if (called++)
907 abort ();
908 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
909 yylloc.first_line = yylloc.last_line = 1;
910 yylloc.first_column = yylloc.last_column = 1;
911 return 0;
912}
913
914static void
915yyerror (const char *msg)
916{
917 fprintf (stderr, "%s\n", msg);
918}
919
920int
921main (void)
922{
923 yydebug = 1;
924 return yyparse ();
925}
926]])
927
928AT_CHECK([bison -o input$1.c input$1.y])
929AT_COMPILE([input$1])
930AT_PARSER_CHECK([./input$1], 0,
931[[<]]kind[[> for 'E' @ 1.
932<]]kind[[> for 'S' @ 1.
933]],
934[[Starting parse
935Entering state 0
936Reducing stack by rule 1 (line 46):
937-> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
938Stack now 0
939Entering state 1
940Reading a token: Now at end of input.
941Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
942Entering state 2
943Stack now 0 1 2
944Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
945Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
946]])
947
948m4_popdef([kind])
949m4_popdef([not_kind])
950])
951
952_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0)
953_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1)
954
955AT_CLEANUP
956
957
958
959## ------------------------------------------------------------------ ##
960## Default %printer and %destructor are not for error or $undefined. ##
961## ------------------------------------------------------------------ ##
962
963AT_SETUP([Default %printer and %destructor are not for error or $undefined])
964
965# If Bison were to apply the default %printer and %destructor to the error
966# token or to $undefined:
967# - For the error token:
968# - It would generate warnings for unused $n.
969# - It would invoke the %printer and %destructor on the error token's
970# semantic value, which would be initialized from the lookahead, which
971# would be destroyed separately.
972# - For $undefined, who knows what the semantic value would be.
973
974AT_DATA_GRAMMAR([[input.y]],
975[[%debug
976
977%{
978# include <stdio.h>
979# include <stdlib.h>
980 static void yyerror (const char *msg);
981 static int yylex (void);
982# define USE(SYM)
983%}
984
985%printer {
986 fprintf (yyoutput, "'%c'", $$);
987} <> <*>
988%destructor {
989 fprintf (stderr, "DESTROY '%c'\n", $$);
990} <> <*>
991
992%%
993
994start:
995 { $$ = 'S'; }
996 /* In order to reveal the problems that this bug caused during parsing, add
997 * $2 to USE. */
998 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
999 ;
1000
1001%%
1002
1003static int
1004yylex (void)
1005{
1006 static char const input[] = "abd";
1007 static size_t toknum;
1008 if (! (toknum < sizeof input))
1009 abort ();
1010 yylval = input[toknum++];
1011 return yylval;
1012}
1013
1014static void
1015yyerror (const char *msg)
1016{
1017 fprintf (stderr, "%s\n", msg);
1018}
1019
1020int
1021main (void)
1022{
1023 yydebug = 1;
1024 return yyparse ();
1025}
1026]])
1027
1028AT_CHECK([bison -o input.c input.y])
1029AT_COMPILE([input])
1030AT_PARSER_CHECK([./input], [1], [],
1031[[Starting parse
1032Entering state 0
1033Reading a token: Next token is token 'a' ('a')
1034Shifting token 'a' ('a')
1035Entering state 1
1036Reading a token: Next token is token 'b' ('b')
1037syntax error
1038Shifting token error ()
1039Entering state 3
1040Next token is token 'b' ('b')
1041Shifting token 'b' ('b')
1042Entering state 5
1043Reading a token: Next token is token $undefined ()
1044Error: popping token 'b' ('b')
1045DESTROY 'b'
1046Stack now 0 1 3
1047Error: popping token error ()
1048Stack now 0 1
1049Shifting token error ()
1050Entering state 3
1051Next token is token $undefined ()
1052Error: discarding token $undefined ()
1053Error: popping token error ()
1054Stack now 0 1
1055Shifting token error ()
1056Entering state 3
1057Reading a token: Now at end of input.
1058Cleanup: discarding lookahead token $end ()
1059Stack now 0 1 3
1060Cleanup: popping token error ()
1061Cleanup: popping token 'a' ('a')
1062DESTROY 'a'
1063]])
1064
1065AT_CLEANUP
1066
1067
1068
1069## ------------------------------------------------------ ##
1070## Default %printer and %destructor are not for $accept. ##
1071## ------------------------------------------------------ ##
1072
1073AT_SETUP([Default %printer and %destructor are not for $accept])
1074
1075# If YYSTYPE is a union and Bison were to apply the default %printer and
1076# %destructor to $accept:
1077# - The %printer and %destructor code generated for $accept would always be
1078# dead code because $accept is currently never shifted onto the stack.
1079# - $$ for $accept would always be of type YYSTYPE because it's not possible
1080# to declare `%type <field> $accept'. (Also true for $undefined.)
1081# - Thus, the compiler might complain that the user code assumes the wrong
1082# type for $$ since the code might assume the type associated with a
1083# specific union field, which is especially reasonable in C++ since that
1084# type may be a base type. This test case checks for this problem. (Also
1085# true for $undefined and the error token, so there are three warnings for
1086# %printer and three for %destructor.)
1087
1088AT_DATA_GRAMMAR([[input.y]],
1089[[%debug /* So that %printer is actually compiled. */
1090
1091%{
1092# include <stdio.h>
1093# include <stdlib.h>
1094 static void yyerror (const char *msg);
1095 static int yylex (void);
1096# define USE(SYM)
1097%}
1098
1099%printer {
1100 char chr = $$;
1101 fprintf (yyoutput, "'%c'", chr);
1102} <> <*>
1103%destructor {
1104 char chr = $$;
1105 fprintf (stderr, "DESTROY '%c'\n", chr);
1106} <> <*>
1107
1108%union { char chr; }
1109%type <chr> start
1110
1111%%
1112
1113start: { USE($$); } ;
1114
1115%%
1116
1117static int
1118yylex (void)
1119{
1120 static int called;
1121 if (called++)
1122 abort ();
1123 return 0;
1124}
1125
1126static void
1127yyerror (const char *msg)
1128{
1129 fprintf (stderr, "%s\n", msg);
1130}
1131
1132int
1133main (void)
1134{
1135 return yyparse ();
1136}
1137]])
1138
1139AT_CHECK([bison -o input.c input.y])
1140AT_COMPILE([input])
1141
1142AT_CLEANUP
1143
1144
1145
1146## ------------------------------------------------------ ##
1147## Default %printer and %destructor for mid-rule values. ##
1148## ------------------------------------------------------ ##
1149
1150AT_SETUP([Default %printer and %destructor for mid-rule values])
1151
1152AT_DATA_GRAMMAR([[input.y]],
1153[[%debug /* So that %printer is actually compiled. */
1154
1155%{
1156# include <stdio.h>
1157# include <stdlib.h>
1158 static void yyerror (const char *msg);
1159 static int yylex (void);
1160# define USE(SYM)
1161# define YYLTYPE int
1162# define YYLLOC_DEFAULT(Current, Rhs, N)
1163# define YY_LOCATION_PRINT(File, Loc)
1164%}
1165
1166%printer { fprintf (yyoutput, "%d", @$); } <>
1167%destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1168%printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1169%destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1170
1171%%
1172
1173start:
1174 { @$ = 1; } // Not set or used.
1175 { USE ($$); @$ = 2; } // Both set and used.
1176 { USE ($$); @$ = 3; } // Only set.
1177 { @$ = 4; } // Only used.
1178 'c'
1179 { USE (($$, $2, $4, $5)); @$ = 0; }
1180 ;
1181
1182%%
1183
1184static int
1185yylex (void)
1186{
1187 static int called;
1188 if (called++)
1189 abort ();
1190 return 0;
1191}
1192
1193static void
1194yyerror (const char *msg)
1195{
1196 fprintf (stderr, "%s\n", msg);
1197}
1198
1199int
1200main (void)
1201{
1202 yydebug = 1;
1203 return yyparse ();
1204}
1205]])
1206
1207AT_CHECK([bison -o input.c input.y], 0,,
1208[[input.y:33.3-23: warning: unset value: $$
1209input.y:30.3-35.37: warning: unused value: $3
1210]])
1211
1212AT_COMPILE([input])
1213AT_PARSER_CHECK([./input], 1,,
1214[[Starting parse
1215Entering state 0
1216Reducing stack by rule 1 (line 30):
1217-> $$ = nterm $@1 (: )
1218Stack now 0
1219Entering state 2
1220Reducing stack by rule 2 (line 31):
1221-> $$ = nterm @2 (: 2)
1222Stack now 0 2
1223Entering state 4
1224Reducing stack by rule 3 (line 32):
1225-> $$ = nterm @3 (: 3)
1226Stack now 0 2 4
1227Entering state 5
1228Reducing stack by rule 4 (line 33):
1229-> $$ = nterm @4 (: 4)
1230Stack now 0 2 4 5
1231Entering state 6
1232Reading a token: Now at end of input.
1233syntax error
1234Error: popping nterm @4 (: 4)
1235DESTROY 4
1236Stack now 0 2 4 5
1237Error: popping nterm @3 (: 3)
1238DESTROY 3
1239Stack now 0 2 4
1240Error: popping nterm @2 (: 2)
1241DESTROY 2
1242Stack now 0 2
1243Error: popping nterm $@1 (: )
1244Stack now 0
1245Cleanup: discarding lookahead token $end (: )
1246Stack now 0
1247]])
1248
1249AT_CLEANUP