]> git.saurik.com Git - bison.git/blame_incremental - tests/actions.at
Regenerate.
[bison.git] / tests / actions.at
... / ...
CommitLineData
1# Executing Actions. -*- Autotest -*-
2# Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2, or (at your option)
7# any later version.
8
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17# 02110-1301, USA.
18
19AT_BANNER([[User Actions.]])
20
21## ------------------ ##
22## Mid-rule actions. ##
23## ------------------ ##
24
25AT_SETUP([Mid-rule actions])
26
27# Bison once forgot the mid-rule actions. It was because the action
28# was attached to the host rule (the one with the mid-rule action),
29# instead of being attached to the empty rule dedicated to this
30# action.
31
32AT_DATA_GRAMMAR([[input.y]],
33[[%error-verbose
34%debug
35%{
36# include <stdio.h>
37# include <stdlib.h>
38 static void yyerror (const char *msg);
39 static int yylex (void);
40%}
41%%
42exp: { putchar ('0'); }
43 '1' { putchar ('1'); }
44 '2' { putchar ('2'); }
45 '3' { putchar ('3'); }
46 '4' { putchar ('4'); }
47 '5' { putchar ('5'); }
48 '6' { putchar ('6'); }
49 '7' { putchar ('7'); }
50 '8' { putchar ('8'); }
51 '9' { putchar ('9'); }
52 { putchar ('\n'); }
53 ;
54%%
55static int
56yylex (void)
57{
58 static const char *input = "123456789";
59 return *input++;
60}
61
62static void
63yyerror (const char *msg)
64{
65 fprintf (stderr, "%s\n", msg);
66}
67
68int
69main (void)
70{
71 return yyparse ();
72}
73]])
74
75AT_CHECK([bison -d -v -o input.c input.y])
76AT_COMPILE([input])
77AT_PARSER_CHECK([./input], 0,
78[[0123456789
79]])
80
81AT_CLEANUP
82
83
84
85
86
87## ---------------- ##
88## Exotic Dollars. ##
89## ---------------- ##
90
91AT_SETUP([Exotic Dollars])
92
93AT_DATA_GRAMMAR([[input.y]],
94[[%error-verbose
95%debug
96%{
97# include <stdio.h>
98# include <stdlib.h>
99 static void yyerror (const char *msg);
100 static int yylex (void);
101# define USE(Var)
102%}
103
104%union
105{
106 int val;
107};
108
109%type <val> a_1 a_2 a_5
110 exp sum_of_the_five_previous_values
111
112%%
113exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
114 sum_of_the_five_previous_values
115 {
116 printf ("%d\n", $6);
117 }
118;
119a_1: { $$ = 1; };
120a_2: { $$ = 2; };
121a_5: { $$ = 5; };
122
123sum_of_the_five_previous_values:
124 {
125 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
126 }
127;
128
129%%
130static int
131yylex (void)
132{
133 return EOF;
134}
135
136static void
137yyerror (const char *msg)
138{
139 fprintf (stderr, "%s\n", msg);
140}
141
142int
143main (void)
144{
145 return yyparse ();
146}
147]])
148
149AT_CHECK([bison -d -v -o input.c input.y], 0, [],
150[input.y:30.6-34.5: warning: unused value: $1
151input.y:30.6-34.5: warning: unused value: $2
152input.y:30.6-34.5: warning: unused value: $5
153])
154AT_COMPILE([input])
155AT_PARSER_CHECK([./input], 0,
156[[15
157]])
158
159AT_CLEANUP
160
161
162
163## -------------------------- ##
164## Printers and Destructors. ##
165## -------------------------- ##
166
167# _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
168# -----------------------------------------------------------------------------
169m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
170[# Make sure complex $n work.
171m4_if([$1$2$3], $[1]$[2]$[3], [],
172 [m4_fatal([$0: Invalid arguments: $@])])dnl
173
174# Be sure to pass all the %directives to this macro to have correct
175# helping macros. So don't put any directly in the Bison file.
176AT_BISON_OPTION_PUSHDEFS([$5])
177AT_DATA_GRAMMAR([[input.y]],
178[[%{
179#include <stdio.h>
180#include <stdlib.h>
181#include <assert.h>
182
183#define YYINITDEPTH 10
184#define YYMAXDEPTH 10
185]AT_LALR1_CC_IF(
186 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
187 [#define RANGE(Location) (Location).first_line, (Location).last_line])
188[%}
189
190$5]
191m4_ifval([$6], [%union
192{
193 int ival;
194}])
195AT_LALR1_CC_IF([%define "global_tokens_and_yystype"])
196[
197%{
198]AT_LALR1_CC_IF([typedef yy::location YYLTYPE;
199 m4_ifval([$6], , [#define YYSTYPE int])])
200[static int yylex (]AT_LEX_FORMALS[);
201]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
202[%}
203
204]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input])[
205
206%printer
207 {
208 ]AT_LALR1_CC_IF([debug_stream () << $$;],
209 [fprintf (yyoutput, "%d", $$)])[;
210 }
211 input line thing 'x' 'y'
212
213%destructor
214 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
215 input
216
217%destructor
218 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
219 line
220
221%destructor
222 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
223 thing
224
225%destructor
226 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
227 'x'
228
229%destructor
230 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
231 'y'
232
233%%
234/*
235 This grammar is made to exercise error recovery.
236 "Lines" starting with `(' support error recovery, with
237 ')' as synchronizing token. Lines starting with 'x' can never
238 be recovered from if in error.
239*/
240
241input:
242 /* Nothing. */
243 {
244 $$ = 0;
245 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
246 }
247| line input /* Right recursive to load the stack so that popping at
248 EOF can be exercised. */
249 {
250 $$ = 2;
251 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
252 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
253 }
254;
255
256line:
257 thing thing thing ';'
258 {
259 $$ = $1;
260 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
261 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
262 $3, RANGE (@3), $4, RANGE (@4));
263 }
264| '(' thing thing ')'
265 {
266 $$ = $1;
267 printf ("line (%d@%d-%d): '(' (%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 ')'
272 {
273 $$ = $1;
274 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
275 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
276 }
277| '(' error ')'
278 {
279 $$ = -1;
280 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
281 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
282 }
283;
284
285thing:
286 'x'
287 {
288 $$ = $1;
289 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
290 $$, RANGE (@$), $1, RANGE (@1));
291 }
292;
293%%
294/* Alias to ARGV[1]. */
295const char *source = 0;
296
297static int
298yylex (]AT_LEX_FORMALS[)
299{
300 static unsigned int counter = 0;
301
302 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
303 /* As in BASIC, line numbers go from 10 to 10. */
304]AT_LALR1_CC_IF(
305[ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
306 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
307],
308[ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
309 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
310])[
311
312 if (source[c])
313 printf ("sending: '%c'", source[c]);
314 else
315 printf ("sending: EOF");
316 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
317 return source[c];
318}
319
320]AT_LALR1_CC_IF(
321[/* A C++ error reporting function. */
322void
323yy::parser::error (const location& l, const std::string& m)
324{
325 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
326}
327
328static bool yydebug;
329int
330yyparse ()
331{
332 yy::parser parser;
333 parser.set_debug_level (yydebug);
334 return parser.parse ();
335}
336],
337[static void
338yyerror (const char *msg)
339{
340 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
341}])[
342
343int
344main (int argc, const char *argv[])
345{
346 int status;
347 yydebug = !!getenv ("YYDEBUG");
348 assert (argc == 2);
349 source = argv[1];
350 status = yyparse ();
351 switch (status)
352 {
353 case 0: printf ("Successful parse.\n"); break;
354 case 1: printf ("Parsing FAILED.\n"); break;
355 default: printf ("Parsing FAILED (status %d).\n", status); break;
356 }
357 return status;
358}
359]])
360
361AT_LALR1_CC_IF(
362 [AT_CHECK([bison -o input.cc input.y])
363 AT_COMPILE_CXX([input])],
364 [AT_CHECK([bison -o input.c input.y])
365 AT_COMPILE([input])])
366
367
368# Check the location of "empty"
369# -----------------------------
370# I.e., epsilon-reductions, as in "(x)" which ends by reducing
371# an empty "line" nterm.
372# FIXME: This location is not satisfying. Depend on the lookahead?
373AT_PARSER_CHECK([./input '(x)'], 0,
374[[sending: '(' (0@0-9)
375sending: 'x' (1@10-19)
376thing (1@10-19): 'x' (1@10-19)
377sending: ')' (2@20-29)
378line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
379sending: EOF (3@30-39)
380input (0@29-29): /* Nothing */
381input (2@0-29): line (0@0-29) input (0@29-29)
382Freeing nterm input (2@0-29)
383Successful parse.
384]])
385
386
387# Check locations in error recovery
388# ---------------------------------
389# '(y)' is an error, but can be recovered from. But what's the location
390# of the error itself ('y'), and of the resulting reduction ('(error)').
391AT_PARSER_CHECK([./input '(y)'], 0,
392[[sending: '(' (0@0-9)
393sending: 'y' (1@10-19)
39410-19: syntax error, unexpected 'y', expecting 'x'
395Freeing token 'y' (1@10-19)
396sending: ')' (2@20-29)
397line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
398sending: EOF (3@30-39)
399input (0@29-29): /* Nothing */
400input (2@0-29): line (-1@0-29) input (0@29-29)
401Freeing nterm input (2@0-29)
402Successful parse.
403]])
404
405
406# Syntax errors caught by the parser
407# ----------------------------------
408# Exercise the discarding of stack top and input until `error'
409# can be reduced.
410#
411# '(', 'x', 'x', 'x', 'x', 'x', ')',
412#
413# Load the stack and provoke an error that cannot be caught by the
414# grammar, to check that the stack is cleared. And make sure the
415# lookahead is freed.
416#
417# '(', 'x', ')',
418# '(', 'x', ')',
419# 'y'
420AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
421[[sending: '(' (0@0-9)
422sending: 'x' (1@10-19)
423thing (1@10-19): 'x' (1@10-19)
424sending: 'x' (2@20-29)
425thing (2@20-29): 'x' (2@20-29)
426sending: 'x' (3@30-39)
42730-39: syntax error, unexpected 'x', expecting ')'
428Freeing nterm thing (2@20-29)
429Freeing nterm thing (1@10-19)
430Freeing token 'x' (3@30-39)
431sending: 'x' (4@40-49)
432Freeing token 'x' (4@40-49)
433sending: 'x' (5@50-59)
434Freeing token 'x' (5@50-59)
435sending: ')' (6@60-69)
436line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
437sending: '(' (7@70-79)
438sending: 'x' (8@80-89)
439thing (8@80-89): 'x' (8@80-89)
440sending: ')' (9@90-99)
441line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
442sending: '(' (10@100-109)
443sending: 'x' (11@110-119)
444thing (11@110-119): 'x' (11@110-119)
445sending: ')' (12@120-129)
446line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
447sending: 'y' (13@130-139)
448input (0@129-129): /* Nothing */
449input (2@100-129): line (10@100-129) input (0@129-129)
450input (2@70-129): line (7@70-99) input (2@100-129)
451input (2@0-129): line (-1@0-69) input (2@70-129)
452130-139: syntax error, unexpected 'y', expecting $end
453Freeing nterm input (2@0-129)
454Freeing token 'y' (13@130-139)
455Parsing FAILED.
456]])
457
458# Check destruction upon stack overflow
459# -------------------------------------
460# Upon stack overflow, all symbols on the stack should be destroyed.
461# Only check for yacc.c.
462AT_YACC_IF([
463AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
464[[sending: '(' (0@0-9)
465sending: 'x' (1@10-19)
466thing (1@10-19): 'x' (1@10-19)
467sending: ')' (2@20-29)
468line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
469sending: '(' (3@30-39)
470sending: 'x' (4@40-49)
471thing (4@40-49): 'x' (4@40-49)
472sending: ')' (5@50-59)
473line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
474sending: '(' (6@60-69)
475sending: 'x' (7@70-79)
476thing (7@70-79): 'x' (7@70-79)
477sending: ')' (8@80-89)
478line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
479sending: '(' (9@90-99)
480sending: 'x' (10@100-109)
481thing (10@100-109): 'x' (10@100-109)
482sending: ')' (11@110-119)
483line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
484sending: '(' (12@120-129)
485sending: 'x' (13@130-139)
486thing (13@130-139): 'x' (13@130-139)
487sending: ')' (14@140-149)
488line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
489sending: '(' (15@150-159)
490sending: 'x' (16@160-169)
491thing (16@160-169): 'x' (16@160-169)
492sending: ')' (17@170-179)
493line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
494sending: '(' (18@180-189)
495sending: 'x' (19@190-199)
496thing (19@190-199): 'x' (19@190-199)
497sending: ')' (20@200-209)
498200-209: memory exhausted
499Freeing nterm thing (19@190-199)
500Freeing nterm line (15@150-179)
501Freeing nterm line (12@120-149)
502Freeing nterm line (9@90-119)
503Freeing nterm line (6@60-89)
504Freeing nterm line (3@30-59)
505Freeing nterm line (0@0-29)
506Parsing FAILED (status 2).
507]])
508])
509
510])
511
512
513# AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
514# ---------------------------------------------------------------------------
515m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
516[AT_SETUP([Printers and Destructors $2: $1])
517
518$3
519_AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
520[%error-verbose
521%debug
522%verbose
523%locations
524$1], [$2])
525
526AT_CLEANUP
527])
528
529
530AT_CHECK_PRINTER_AND_DESTRUCTOR([])
531AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
532
533# These tests currently fail on a Debian GNU/Linux 3.0r2 x86 host,
534# but the 2nd test succeeds on a Solaris 9 sparc hosts (Forte 7 cc).
535# Skip them until we figure out what the problem is.
536AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
537AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
538
539AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
540AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])