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