]> git.saurik.com Git - bison.git/blame_incremental - tests/actions.at
AT_FULL_COMPILE.
[bison.git] / tests / actions.at
... / ...
CommitLineData
1# Executing Actions. -*- Autotest -*-
2# Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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_FULL_COMPILE([input])
371
372
373# Check the location of "empty"
374# -----------------------------
375# I.e., epsilon-reductions, as in "(x)" which ends by reducing
376# an empty "line" nterm.
377# FIXME: This location is not satisfying. Depend on the lookahead?
378AT_PARSER_CHECK([./input '(x)'], 0,
379[[sending: '(' (0@0-9)
380sending: 'x' (1@10-19)
381thing (1@10-19): 'x' (1@10-19)
382sending: ')' (2@20-29)
383line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
384sending: END (3@30-39)
385input (0@29-29): /* Nothing */
386input (2@0-29): line (0@0-29) input (0@29-29)
387Freeing token END (3@30-39)
388Freeing nterm input (2@0-29)
389Successful parse.
390]])
391
392
393# Check locations in error recovery
394# ---------------------------------
395# '(y)' is an error, but can be recovered from. But what's the location
396# of the error itself ('y'), and of the resulting reduction ('(error)').
397AT_PARSER_CHECK([./input '(y)'], 0,
398[[sending: '(' (0@0-9)
399sending: 'y' (1@10-19)
40010-19: syntax error, unexpected 'y', expecting 'x'
401Freeing token 'y' (1@10-19)
402sending: ')' (2@20-29)
403line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
404sending: END (3@30-39)
405input (0@29-29): /* Nothing */
406input (2@0-29): line (-1@0-29) input (0@29-29)
407Freeing token END (3@30-39)
408Freeing nterm input (2@0-29)
409Successful parse.
410]])
411
412
413# Syntax errors caught by the parser
414# ----------------------------------
415# Exercise the discarding of stack top and input until `error'
416# can be reduced.
417#
418# '(', 'x', 'x', 'x', 'x', 'x', ')',
419#
420# Load the stack and provoke an error that cannot be caught by the
421# grammar, to check that the stack is cleared. And make sure the
422# lookahead is freed.
423#
424# '(', 'x', ')',
425# '(', 'x', ')',
426# 'y'
427AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
428[[sending: '(' (0@0-9)
429sending: 'x' (1@10-19)
430thing (1@10-19): 'x' (1@10-19)
431sending: 'x' (2@20-29)
432thing (2@20-29): 'x' (2@20-29)
433sending: 'x' (3@30-39)
43430-39: syntax error, unexpected 'x', expecting ')'
435Freeing nterm thing (2@20-29)
436Freeing nterm thing (1@10-19)
437Freeing token 'x' (3@30-39)
438sending: 'x' (4@40-49)
439Freeing token 'x' (4@40-49)
440sending: 'x' (5@50-59)
441Freeing token 'x' (5@50-59)
442sending: ')' (6@60-69)
443line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
444sending: '(' (7@70-79)
445sending: 'x' (8@80-89)
446thing (8@80-89): 'x' (8@80-89)
447sending: ')' (9@90-99)
448line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
449sending: '(' (10@100-109)
450sending: 'x' (11@110-119)
451thing (11@110-119): 'x' (11@110-119)
452sending: ')' (12@120-129)
453line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
454sending: 'y' (13@130-139)
455input (0@129-129): /* Nothing */
456input (2@100-129): line (10@100-129) input (0@129-129)
457input (2@70-129): line (7@70-99) input (2@100-129)
458input (2@0-129): line (-1@0-69) input (2@70-129)
459130-139: syntax error, unexpected 'y', expecting END
460Freeing nterm input (2@0-129)
461Freeing token 'y' (13@130-139)
462Parsing FAILED.
463]])
464
465
466# Syntax error caught by the parser where lookahead = END
467# --------------------------------------------------------
468# Load the stack and provoke an error that cannot be caught by the
469# grammar, to check that the stack is cleared. And make sure the
470# lookahead is freed.
471#
472# '(', 'x', ')',
473# '(', 'x', ')',
474# 'x'
475AT_PARSER_CHECK([./input '(x)(x)x'], 1,
476[[sending: '(' (0@0-9)
477sending: 'x' (1@10-19)
478thing (1@10-19): 'x' (1@10-19)
479sending: ')' (2@20-29)
480line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
481sending: '(' (3@30-39)
482sending: 'x' (4@40-49)
483thing (4@40-49): 'x' (4@40-49)
484sending: ')' (5@50-59)
485line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
486sending: 'x' (6@60-69)
487thing (6@60-69): 'x' (6@60-69)
488sending: END (7@70-79)
48970-79: syntax error, unexpected END, expecting 'x'
490Freeing nterm thing (6@60-69)
491Freeing nterm line (3@30-59)
492Freeing nterm line (0@0-29)
493Freeing token END (7@70-79)
494Parsing FAILED.
495]])
496
497
498# Check destruction upon stack overflow
499# -------------------------------------
500# Upon stack overflow, all symbols on the stack should be destroyed.
501# Only check for yacc.c.
502AT_YACC_IF([
503AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
504[[sending: '(' (0@0-9)
505sending: 'x' (1@10-19)
506thing (1@10-19): 'x' (1@10-19)
507sending: ')' (2@20-29)
508line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
509sending: '(' (3@30-39)
510sending: 'x' (4@40-49)
511thing (4@40-49): 'x' (4@40-49)
512sending: ')' (5@50-59)
513line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
514sending: '(' (6@60-69)
515sending: 'x' (7@70-79)
516thing (7@70-79): 'x' (7@70-79)
517sending: ')' (8@80-89)
518line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
519sending: '(' (9@90-99)
520sending: 'x' (10@100-109)
521thing (10@100-109): 'x' (10@100-109)
522sending: ')' (11@110-119)
523line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
524sending: '(' (12@120-129)
525sending: 'x' (13@130-139)
526thing (13@130-139): 'x' (13@130-139)
527sending: ')' (14@140-149)
528line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
529sending: '(' (15@150-159)
530sending: 'x' (16@160-169)
531thing (16@160-169): 'x' (16@160-169)
532sending: ')' (17@170-179)
533line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
534sending: '(' (18@180-189)
535sending: 'x' (19@190-199)
536thing (19@190-199): 'x' (19@190-199)
537sending: ')' (20@200-209)
538200-209: memory exhausted
539Freeing nterm thing (19@190-199)
540Freeing nterm line (15@150-179)
541Freeing nterm line (12@120-149)
542Freeing nterm line (9@90-119)
543Freeing nterm line (6@60-89)
544Freeing nterm line (3@30-59)
545Freeing nterm line (0@0-29)
546Parsing FAILED (status 2).
547]])
548])
549
550])
551
552
553# AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
554# ---------------------------------------------------------------------------
555m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
556[AT_SETUP([Printers and Destructors $2: $1])
557
558$3
559_AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
560[%error-verbose
561%debug
562%verbose
563%locations
564$1], [$2])
565
566AT_CLEANUP
567])
568
569
570AT_CHECK_PRINTER_AND_DESTRUCTOR([])
571AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
572
573AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
574AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
575
576AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
577AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
578
579
580
581## ----------------------------------------- ##
582## Default tagless %printer and %destructor. ##
583## ----------------------------------------- ##
584
585# Check that the right %printer and %destructor are called, that they're not
586# called for $end, and that $$ and @$ work correctly.
587
588AT_SETUP([Default tagless %printer and %destructor])
589
590AT_DATA_GRAMMAR([[input.y]],
591[[%error-verbose
592%debug
593%locations
594%initial-action {
595 @$.first_line = @$.last_line = 1;
596 @$.first_column = @$.last_column = 1;
597}
598
599%{
600# include <stdio.h>
601# include <stdlib.h>
602 static void yyerror (const char *msg);
603 static int yylex (void);
604# define USE(SYM)
605%}
606
607%printer {
608 fprintf (yyoutput, "<*> printer should not be called.\n");
609} <*>
610
611%printer {
612 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
613} <>
614%destructor {
615 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
616} <>
617
618%printer {
619 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
620} 'b' 'c'
621%destructor {
622 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
623} 'b' 'c'
624
625%destructor {
626 fprintf (yyoutput, "<*> destructor should not be called.\n");
627} <*>
628
629%%
630
631start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
632
633%%
634
635static int
636yylex (void)
637{
638 static char const input[] = "abcd";
639 static size_t toknum;
640 if (! (toknum < sizeof input))
641 abort ();
642 yylval = input[toknum++];
643 yylloc.first_line = yylloc.last_line = 1;
644 yylloc.first_column = yylloc.last_column = toknum;
645 return yylval;
646}
647
648static void
649yyerror (const char *msg)
650{
651 fprintf (stderr, "%s\n", msg);
652}
653
654int
655main (void)
656{
657 yydebug = 1;
658 return yyparse ();
659}
660]])
661
662AT_BISON_CHECK([-o input.c input.y])
663AT_COMPILE([input])
664AT_PARSER_CHECK([./input], 1,
665[[<> destructor for 'd' @ 4.
666'b'/'c' destructor for 'c' @ 3.
667'b'/'c' destructor for 'b' @ 2.
668<> destructor for 'a' @ 1.
669]],
670[[Starting parse
671Entering state 0
672Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1)
673Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1)
674Entering state 1
675Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
676Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
677Entering state 3
678Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
679Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
680Entering state 5
681Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4)
682Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4)
683Entering state 6
684Reading a token: Now at end of input.
685syntax error, unexpected $end, expecting 'e'
686Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4)
687Stack now 0 1 3 5
688Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
689Stack now 0 1 3
690Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
691Stack now 0 1
692Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1)
693Stack now 0
694Cleanup: discarding lookahead token $end (1.5-1.5: )
695Stack now 0
696]])
697
698AT_CLEANUP
699
700
701
702## ------------------------------------------------------ ##
703## Default tagged and per-type %printer and %destructor. ##
704## ------------------------------------------------------ ##
705
706AT_SETUP([Default tagged and per-type %printer and %destructor])
707
708AT_DATA_GRAMMAR([[input.y]],
709[[%error-verbose
710%debug
711
712%{
713# include <stdio.h>
714# include <stdlib.h>
715 static void yyerror (const char *msg);
716 static int yylex (void);
717# define USE(SYM)
718%}
719
720%printer {
721 fprintf (yyoutput, "<> printer should not be called.\n");
722} <>
723
724%union { int field0; int field1; int field2; }
725%type <field0> start 'a' 'g'
726%type <field1> 'e'
727%type <field2> 'f'
728%printer {
729 fprintf (yyoutput, "<*>/<field2>/e printer");
730} <*> 'e' <field2>
731%destructor {
732 fprintf (stdout, "<*>/<field2>/e destructor.\n");
733} <*> 'e' <field2>
734
735%type <field1> 'b'
736%printer { fprintf (yyoutput, "<field1> printer"); } <field1>
737%destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
738
739%type <field0> 'c'
740%printer { fprintf (yyoutput, "'c' printer"); } 'c'
741%destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
742
743%type <field1> 'd'
744%printer { fprintf (yyoutput, "'d' printer"); } 'd'
745%destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
746
747%destructor {
748 fprintf (yyoutput, "<> destructor should not be called.\n");
749} <>
750
751%%
752
753start:
754 'a' 'b' 'c' 'd' 'e' 'f' 'g'
755 {
756 USE(($1, $2, $3, $4, $5, $6, $7));
757 $$ = 'S';
758 }
759 ;
760
761%%
762
763static int
764yylex (void)
765{
766 static char const input[] = "abcdef";
767 static size_t toknum;
768 if (! (toknum < sizeof input))
769 abort ();
770 return input[toknum++];
771}
772
773static void
774yyerror (const char *msg)
775{
776 fprintf (stderr, "%s\n", msg);
777}
778
779int
780main (void)
781{
782 yydebug = 1;
783 return yyparse ();
784}
785]])
786
787AT_BISON_CHECK([-o input.c input.y])
788AT_COMPILE([input])
789AT_PARSER_CHECK([./input], 1,
790[[<*>/<field2>/e destructor.
791<*>/<field2>/e destructor.
792'd' destructor.
793'c' destructor.
794<field1> destructor.
795<*>/<field2>/e destructor.
796]],
797[[Starting parse
798Entering state 0
799Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
800Shifting token 'a' (<*>/<field2>/e printer)
801Entering state 1
802Reading a token: Next token is token 'b' (<field1> printer)
803Shifting token 'b' (<field1> printer)
804Entering state 3
805Reading a token: Next token is token 'c' ('c' printer)
806Shifting token 'c' ('c' printer)
807Entering state 5
808Reading a token: Next token is token 'd' ('d' printer)
809Shifting token 'd' ('d' printer)
810Entering state 6
811Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
812Shifting token 'e' (<*>/<field2>/e printer)
813Entering state 7
814Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
815Shifting token 'f' (<*>/<field2>/e printer)
816Entering state 8
817Reading a token: Now at end of input.
818syntax error, unexpected $end, expecting 'g'
819Error: popping token 'f' (<*>/<field2>/e printer)
820Stack now 0 1 3 5 6 7
821Error: popping token 'e' (<*>/<field2>/e printer)
822Stack now 0 1 3 5 6
823Error: popping token 'd' ('d' printer)
824Stack now 0 1 3 5
825Error: popping token 'c' ('c' printer)
826Stack now 0 1 3
827Error: popping token 'b' (<field1> printer)
828Stack now 0 1
829Error: popping token 'a' (<*>/<field2>/e printer)
830Stack now 0
831Cleanup: discarding lookahead token $end ()
832Stack now 0
833]])
834
835AT_CLEANUP
836
837
838
839## ------------------------------------------------------------- ##
840## Default %printer and %destructor for user-defined end token. ##
841## ------------------------------------------------------------- ##
842
843AT_SETUP([Default %printer and %destructor for user-defined end token])
844
845# _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED)
846# -----------------------------------------------------------------------------
847m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN],
848[m4_if($1, 0,
849 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
850 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
851
852AT_DATA_GRAMMAR([[input]]$1[[.y]],
853[[%error-verbose
854%debug
855%locations
856%initial-action {
857 @$.first_line = @$.last_line = 1;
858 @$.first_column = @$.last_column = 1;
859}
860
861%{
862# include <stdio.h>
863# include <stdlib.h>
864 static void yyerror (const char *msg);
865 static int yylex (void);
866# define USE(SYM)
867%}
868
869%destructor {
870 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
871} <]]not_kind[[>
872
873%token END 0
874%printer {
875 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
876} <]]kind[[>
877%destructor {
878 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
879} <]]kind[[>
880
881%printer {
882 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
883} <]]not_kind[[>
884
885]]m4_if($1, 0, [[[
886]]],
887[[[%union { char tag; }
888%type <tag> start END]]])[[
889
890%%
891
892start: { $$ = 'S'; } ;
893
894%%
895
896static int
897yylex (void)
898{
899 static int called;
900 if (called++)
901 abort ();
902 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
903 yylloc.first_line = yylloc.last_line = 1;
904 yylloc.first_column = yylloc.last_column = 1;
905 return 0;
906}
907
908static void
909yyerror (const char *msg)
910{
911 fprintf (stderr, "%s\n", msg);
912}
913
914int
915main (void)
916{
917 yydebug = 1;
918 return yyparse ();
919}
920]])
921
922AT_BISON_CHECK([-o input$1.c input$1.y])
923AT_COMPILE([input$1])
924AT_PARSER_CHECK([./input$1], 0,
925[[<]]kind[[> for 'E' @ 1.
926<]]kind[[> for 'S' @ 1.
927]],
928[[Starting parse
929Entering state 0
930Reducing stack by rule 1 (line 46):
931-> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
932Stack now 0
933Entering state 1
934Reading a token: Now at end of input.
935Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
936Entering state 2
937Stack now 0 1 2
938Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
939Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
940]])
941
942m4_popdef([kind])
943m4_popdef([not_kind])
944])
945
946_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0)
947_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1)
948
949AT_CLEANUP
950
951
952
953## ------------------------------------------------------------------ ##
954## Default %printer and %destructor are not for error or $undefined. ##
955## ------------------------------------------------------------------ ##
956
957AT_SETUP([Default %printer and %destructor are not for error or $undefined])
958
959# If Bison were to apply the default %printer and %destructor to the error
960# token or to $undefined:
961# - For the error token:
962# - It would generate warnings for unused $n.
963# - It would invoke the %printer and %destructor on the error token's
964# semantic value, which would be initialized from the lookahead, which
965# would be destroyed separately.
966# - For $undefined, who knows what the semantic value would be.
967
968AT_DATA_GRAMMAR([[input.y]],
969[[%debug
970
971%{
972# include <stdio.h>
973# include <stdlib.h>
974 static void yyerror (const char *msg);
975 static int yylex (void);
976# define USE(SYM)
977%}
978
979%printer {
980 fprintf (yyoutput, "'%c'", $$);
981} <> <*>
982%destructor {
983 fprintf (stderr, "DESTROY '%c'\n", $$);
984} <> <*>
985
986%%
987
988start:
989 { $$ = 'S'; }
990 /* In order to reveal the problems that this bug caused during parsing, add
991 * $2 to USE. */
992 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
993 ;
994
995%%
996
997static int
998yylex (void)
999{
1000 static char const input[] = "abd";
1001 static size_t toknum;
1002 if (! (toknum < sizeof input))
1003 abort ();
1004 yylval = input[toknum++];
1005 return yylval;
1006}
1007
1008static void
1009yyerror (const char *msg)
1010{
1011 fprintf (stderr, "%s\n", msg);
1012}
1013
1014int
1015main (void)
1016{
1017 yydebug = 1;
1018 return yyparse ();
1019}
1020]])
1021
1022AT_BISON_CHECK([-o input.c input.y])
1023AT_COMPILE([input])
1024AT_PARSER_CHECK([./input], [1], [],
1025[[Starting parse
1026Entering state 0
1027Reading a token: Next token is token 'a' ('a')
1028Shifting token 'a' ('a')
1029Entering state 1
1030Reading a token: Next token is token 'b' ('b')
1031syntax error
1032Shifting token error ()
1033Entering state 3
1034Next token is token 'b' ('b')
1035Shifting token 'b' ('b')
1036Entering state 5
1037Reading a token: Next token is token $undefined ()
1038Error: popping token 'b' ('b')
1039DESTROY 'b'
1040Stack now 0 1 3
1041Error: popping token error ()
1042Stack now 0 1
1043Shifting token error ()
1044Entering state 3
1045Next token is token $undefined ()
1046Error: discarding token $undefined ()
1047Error: popping token error ()
1048Stack now 0 1
1049Shifting token error ()
1050Entering state 3
1051Reading a token: Now at end of input.
1052Cleanup: discarding lookahead token $end ()
1053Stack now 0 1 3
1054Cleanup: popping token error ()
1055Cleanup: popping token 'a' ('a')
1056DESTROY 'a'
1057]])
1058
1059AT_CLEANUP
1060
1061
1062
1063## ------------------------------------------------------ ##
1064## Default %printer and %destructor are not for $accept. ##
1065## ------------------------------------------------------ ##
1066
1067AT_SETUP([Default %printer and %destructor are not for $accept])
1068
1069# If YYSTYPE is a union and Bison were to apply the default %printer and
1070# %destructor to $accept:
1071# - The %printer and %destructor code generated for $accept would always be
1072# dead code because $accept is currently never shifted onto the stack.
1073# - $$ for $accept would always be of type YYSTYPE because it's not possible
1074# to declare `%type <field> $accept'. (Also true for $undefined.)
1075# - Thus, the compiler might complain that the user code assumes the wrong
1076# type for $$ since the code might assume the type associated with a
1077# specific union field, which is especially reasonable in C++ since that
1078# type may be a base type. This test case checks for this problem. (Also
1079# true for $undefined and the error token, so there are three warnings for
1080# %printer and three for %destructor.)
1081
1082AT_DATA_GRAMMAR([[input.y]],
1083[[%debug /* So that %printer is actually compiled. */
1084
1085%{
1086# include <stdio.h>
1087# include <stdlib.h>
1088 static void yyerror (const char *msg);
1089 static int yylex (void);
1090# define USE(SYM)
1091%}
1092
1093%printer {
1094 char chr = $$;
1095 fprintf (yyoutput, "'%c'", chr);
1096} <> <*>
1097%destructor {
1098 char chr = $$;
1099 fprintf (stderr, "DESTROY '%c'\n", chr);
1100} <> <*>
1101
1102%union { char chr; }
1103%type <chr> start
1104
1105%%
1106
1107start: { USE($$); } ;
1108
1109%%
1110
1111static int
1112yylex (void)
1113{
1114 static int called;
1115 if (called++)
1116 abort ();
1117 return 0;
1118}
1119
1120static void
1121yyerror (const char *msg)
1122{
1123 fprintf (stderr, "%s\n", msg);
1124}
1125
1126int
1127main (void)
1128{
1129 return yyparse ();
1130}
1131]])
1132
1133AT_BISON_CHECK([-o input.c input.y])
1134AT_COMPILE([input])
1135
1136AT_CLEANUP
1137
1138
1139
1140## ------------------------------------------------------ ##
1141## Default %printer and %destructor for mid-rule values. ##
1142## ------------------------------------------------------ ##
1143
1144AT_SETUP([Default %printer and %destructor for mid-rule values])
1145
1146AT_DATA_GRAMMAR([[input.y]],
1147[[%debug /* So that %printer is actually compiled. */
1148
1149%{
1150# include <stdio.h>
1151# include <stdlib.h>
1152 static void yyerror (const char *msg);
1153 static int yylex (void);
1154# define USE(SYM)
1155# define YYLTYPE int
1156# define YYLLOC_DEFAULT(Current, Rhs, N)
1157# define YY_LOCATION_PRINT(File, Loc)
1158%}
1159
1160%printer { fprintf (yyoutput, "%d", @$); } <>
1161%destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1162%printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1163%destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1164
1165%%
1166
1167start:
1168 { @$ = 1; } // Not set or used.
1169 { USE ($$); @$ = 2; } // Both set and used.
1170 { USE ($$); @$ = 3; } // Only set.
1171 { @$ = 4; } // Only used.
1172 'c'
1173 { USE (($$, $2, $4, $5)); @$ = 0; }
1174 ;
1175
1176%%
1177
1178static int
1179yylex (void)
1180{
1181 static int called;
1182 if (called++)
1183 abort ();
1184 return 0;
1185}
1186
1187static void
1188yyerror (const char *msg)
1189{
1190 fprintf (stderr, "%s\n", msg);
1191}
1192
1193int
1194main (void)
1195{
1196 yydebug = 1;
1197 return yyparse ();
1198}
1199]])
1200
1201AT_BISON_CHECK([-o input.c input.y], 0,,
1202[[input.y:33.3-23: warning: unset value: $$
1203input.y:30.3-35.37: warning: unused value: $3
1204]])
1205
1206AT_COMPILE([input])
1207AT_PARSER_CHECK([./input], 1,,
1208[[Starting parse
1209Entering state 0
1210Reducing stack by rule 1 (line 30):
1211-> $$ = nterm $@1 (: )
1212Stack now 0
1213Entering state 2
1214Reducing stack by rule 2 (line 31):
1215-> $$ = nterm @2 (: 2)
1216Stack now 0 2
1217Entering state 4
1218Reducing stack by rule 3 (line 32):
1219-> $$ = nterm @3 (: 3)
1220Stack now 0 2 4
1221Entering state 5
1222Reducing stack by rule 4 (line 33):
1223-> $$ = nterm @4 (: 4)
1224Stack now 0 2 4 5
1225Entering state 6
1226Reading a token: Now at end of input.
1227syntax error
1228Error: popping nterm @4 (: 4)
1229DESTROY 4
1230Stack now 0 2 4 5
1231Error: popping nterm @3 (: 3)
1232DESTROY 3
1233Stack now 0 2 4
1234Error: popping nterm @2 (: 2)
1235DESTROY 2
1236Stack now 0 2
1237Error: popping nterm $@1 (: )
1238Stack now 0
1239Cleanup: discarding lookahead token $end (: )
1240Stack now 0
1241]])
1242
1243AT_CLEANUP
1244
1245
1246## ----------------------- ##
1247## @$ implies %locations. ##
1248## ----------------------- ##
1249
1250# Bison once forgot to check for @$ in actions other than semantic actions.
1251
1252# AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1253# -------------------------------------------------------
1254m4_define([AT_CHECK_ACTION_LOCATIONS],
1255[AT_SETUP([[@$ in ]$1[ implies %locations]])
1256
1257AT_DATA_GRAMMAR([[input.y]],
1258[[%code {
1259 #include <stdio.h>
1260 static int yylex (void);
1261 static void yyerror (char const *msg);
1262}
1263
1264%debug
1265
1266]$1[ {
1267 printf ("%d\n", @$.first_line);
1268} ]m4_if($1, [%initial-action], [], [[start]])[
1269
1270%%
1271
1272start: ;
1273
1274%%
1275
1276static int
1277yylex (void)
1278{
1279 return 0;
1280}
1281
1282static void
1283yyerror (char const *msg)
1284{
1285 fprintf (stderr, "%s\n", msg);
1286}
1287
1288int
1289main (void)
1290{
1291 return yyparse ();
1292}
1293]])
1294
1295AT_BISON_CHECK([[-o input.c input.y]])
1296AT_COMPILE([[input]])
1297
1298AT_CLEANUP])
1299
1300AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1301AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1302AT_CHECK_ACTION_LOCATIONS([[%printer]])