]> git.saurik.com Git - bison.git/blame_incremental - tests/actions.at
($(TESTSUITE)): Remove warning about ignoring
[bison.git] / tests / actions.at
... / ...
CommitLineData
1# Executing Actions. -*- Autotest -*-
2# Copyright (C) 2001, 2002 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., 59 Temple Place - Suite 330, Boston, MA
17# 02111-1307, 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[[%{
34# include <stdio.h>
35# include <stdlib.h>
36 static void yyerror (const char *msg);
37 static int yylex (void);
38# define YYDEBUG 1
39# define YYERROR_VERBOSE 1
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## Actions after errors. ##
87## ---------------------- ##
88
89AT_SETUP([Actions after errors])
90
91
92
93AT_DATA_GRAMMAR([[input.y]],
94[[%{
95#include <stdio.h>
96#include <stdlib.h>
97
98static int yylex (void);
99static void yyerror (char const *);
100
101#define YYDEBUG 1
102%}
103%union { int ival; }
104%type <ival> 'x' ';' thing line input
105
106%%
107input:
108 /* Nothing. */
109 {
110 $$ = 0;
111 printf ("input (%d): /* Nothing */\n", $$);
112 }
113| line input /* Right recursive to load the stack so that popping at
114 EOF can be exercised. */
115 {
116 $$ = 2;
117 printf ("input (%d): line (%d) input (%d)\n", $$, $1, $2);
118 }
119;
120
121line:
122 thing thing thing ';'
123 {
124 $$ = $1;
125 printf ("line (%d): thing (%d) thing (%d) thing (%d) ';' (%d)\n",
126 $$, $1, $2, $3, $4);
127 }
128| thing thing ';'
129 {
130 $$ = $1;
131 printf ("line (%d): thing (%d) thing (%d) ';' (%d)\n", $$, $1, $2, $3);
132 }
133| thing ';'
134 {
135 $$ = $1;
136 printf ("line (%d): thing (%d) ';' (%d)\n", $$, $1, $2);
137 }
138| error ';'
139 {
140 $$ = -1;
141 printf ("line (%d): error ';' (%d)\n", $$, $2);
142 }
143;
144
145thing:
146 'x'
147 {
148 $$ = $1;
149 printf ("thing (%d): 'x' (%d)\n", $$, $1);
150 }
151;
152%%
153static size_t counter;
154
155static int
156yylex (void)
157{
158 static char const input[] =
159 {
160 /* Exercise the discarding of stack top and input until `error'
161 can be reduced. */
162 'x', 'x', 'x', 'x', 'x', 'x', ';',
163
164 /* Load the stack and provoke an error that cannot be caught by
165 the grammar, to check that the stack is cleared. */
166 'x', 'x', ';',
167 'x', ';',
168 'y'
169 };
170
171 if (counter < sizeof input)
172 {
173 yylval.ival = counter;
174 printf ("sending: '%c' (%d)\n", input[counter], yylval.ival);
175 return input[counter++];
176 }
177 else
178 {
179 printf ("sending: EOF\n");
180 return EOF;
181 }
182}
183
184static void
185yyerror (char const *msg)
186{
187 printf ("%lu: %s\n", (unsigned long int) counter, msg);
188}
189
190int
191main (void)
192{
193 yydebug = !!getenv ("YYDEBUG");
194 return yyparse ();
195}
196]])
197
198AT_CHECK([bison -o input.c input.y])
199AT_COMPILE([input])
200AT_PARSER_CHECK([./input], 1,
201[[sending: 'x' (0)
202thing (0): 'x' (0)
203sending: 'x' (1)
204thing (1): 'x' (1)
205sending: 'x' (2)
206thing (2): 'x' (2)
207sending: 'x' (3)
2084: syntax error
209sending: 'x' (4)
210sending: 'x' (5)
211sending: ';' (6)
212line (-1): error ';' (6)
213sending: 'x' (7)
214thing (7): 'x' (7)
215sending: 'x' (8)
216thing (8): 'x' (8)
217sending: ';' (9)
218line (7): thing (7) thing (8) ';' (9)
219sending: 'x' (10)
220thing (10): 'x' (10)
221sending: ';' (11)
222line (10): thing (10) ';' (11)
223sending: 'y' (12)
22413: syntax error
225sending: EOF
226]])
227
228AT_CLEANUP
229
230
231
232## ---------------- ##
233## Exotic Dollars. ##
234## ---------------- ##
235
236AT_SETUP([Exotic Dollars])
237
238AT_DATA_GRAMMAR([[input.y]],
239[[%{
240# include <stdio.h>
241# include <stdlib.h>
242 static void yyerror (const char *msg);
243 static int yylex (void);
244# define YYDEBUG 1
245# define YYERROR_VERBOSE 1
246%}
247
248%union
249{
250 int val;
251};
252
253%type <val> a_1 a_2 a_5
254 sum_of_the_five_previous_values
255
256%%
257exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
258 sum_of_the_five_previous_values
259 {
260 printf ("%d\n", $6);
261 }
262;
263a_1: { $$ = 1; };
264a_2: { $$ = 2; };
265a_5: { $$ = 5; };
266
267sum_of_the_five_previous_values:
268 {
269 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
270 }
271;
272
273%%
274static int
275yylex (void)
276{
277 return EOF;
278}
279
280static void
281yyerror (const char *msg)
282{
283 fprintf (stderr, "%s\n", msg);
284}
285
286int
287main (void)
288{
289 return yyparse ();
290}
291]])
292
293AT_CHECK([bison -d -v -o input.c input.y])
294AT_COMPILE([input])
295AT_PARSER_CHECK([./input], 0,
296[[15
297]])
298
299AT_CLEANUP
300
301
302
303## -------------------------- ##
304## Printers and Destructors. ##
305## -------------------------- ##
306
307# _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE)
308# -----------------------------------------------------------------
309m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
310[m4_if([$1$2$3], $[1]$[2]$[3], [],
311 [m4_fatal([$0: Invalid arguments: $@])])dnl
312
313AT_SETUP([Printers and Destructors: $5])
314
315# Make sure complex $n work.
316
317AT_DATA_GRAMMAR([[input.y]],
318[[$5
319%{
320#include <stdio.h>
321#include <stdlib.h>
322#define RANGE(Location) (Location).first_line, (Location).last_line
323static int yylex (void);
324static void yyerror (const char *msg);
325%}
326%error-verbose
327%debug
328%verbose
329%locations
330%union
331{
332 int ival;
333}
334%type <ival> 'x' ';' thing line input
335
336%printer { fprintf (yyoutput, "%d@%d-%d", $$, RANGE (@$)); }
337 input line thing 'x'
338
339%destructor
340 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
341 input
342
343%destructor
344 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
345 line
346
347%destructor
348 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
349 thing
350
351%destructor
352 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
353 'x'
354
355%%
356input:
357 /* Nothing. */
358 {
359 $$ = 0;
360 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
361 }
362| line input /* Right recursive to load the stack so that popping at
363 EOF can be exercised. */
364 {
365 $$ = 2;
366 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
367 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
368 }
369;
370
371line:
372 thing thing thing ';'
373 {
374 $$ = $1;
375 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
376 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
377 $3, RANGE (@3), $4, RANGE (@4));
378 }
379| thing thing ';'
380 {
381 $$ = $1;
382 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
383 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
384 }
385| thing ';'
386 {
387 $$ = $1;
388 printf ("line (%d@%d-%d): thing (%d@%d-%d) ';' (%d@%d-%d)\n",
389 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
390 }
391| error ';'
392 {
393 $$ = -1;
394 printf ("line (%d@%d-%d): error (@%d-%d) ';' (%d@%d-%d)\n",
395 $$, RANGE (@$), RANGE (@1), $2, RANGE (@2));
396 }
397;
398
399thing:
400 'x'
401 {
402 $$ = $1;
403 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
404 $$, RANGE (@$), $1, RANGE (@1));
405 }
406;
407%%
408static int
409yylex (void)
410{
411 static const unsigned int input[] =
412 {
413 /* Exercise the discarding of stack top and input until `error'
414 can be reduced. */
415 'x', 'x', 'x', 'x', 'x', 'x', ';',
416
417 /* Load the stack and provoke an error that cannot be caught by
418 the grammar, to check that the stack is cleared. */
419 'x', 'x', ';',
420 'x', ';',
421 'y'
422 };
423 static unsigned int counter = 0;
424
425 if (counter < (sizeof(input) / sizeof (input[0])))
426 {
427 yylval.ival = counter;
428 /* As in BASIC, line numbers go from 10 to 10. */
429 yylloc.first_line = yylloc.first_column = 10 * counter;
430 yylloc.last_line = yylloc.last_column = yylloc.first_line + 9;
431 printf ("sending: '%c' (%d@%d-%d)\n",
432 input[counter], yylval.ival, RANGE (yylloc));
433 return (int) input[counter++];
434 }
435 else
436 {
437 printf ("sending: EOF\n");
438 return EOF;
439 }
440}
441
442static void
443yyerror (const char *msg)
444{
445 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
446}
447
448int
449main (void)
450{
451 yydebug = !!getenv ("YYDEBUG");
452 if (yyparse ())
453 {
454 printf ("Parsing FAILED.\n");
455 exit (1);
456 }
457 printf ("Successful parse.\n");
458 return 0;
459}
460]])
461
462AT_CHECK([bison -o input.c input.y])
463AT_COMPILE([input])
464AT_PARSER_CHECK([./input], 1,
465[[sending: 'x' (0@0-9)
466thing (0@0-9): 'x' (0@0-9)
467sending: 'x' (1@10-19)
468thing (1@10-19): 'x' (1@10-19)
469sending: 'x' (2@20-29)
470thing (2@20-29): 'x' (2@20-29)
471sending: 'x' (3@30-39)
47230-39: syntax error, unexpected 'x', expecting ';'
473Freeing nterm thing (2@20-29)
474Freeing nterm thing (1@10-19)
475Freeing nterm thing (0@0-9)
476Freeing token 'x' (3@30-39)
477sending: 'x' (4@40-49)
478Freeing token 'x' (4@40-49)
479sending: 'x' (5@50-59)
480Freeing token 'x' (5@50-59)
481sending: ';' (6@60-69)
482line (-1@0-69): error (@0-59) ';' (6@60-69)
483sending: 'x' (7@70-79)
484thing (7@70-79): 'x' (7@70-79)
485sending: 'x' (8@80-89)
486thing (8@80-89): 'x' (8@80-89)
487sending: ';' (9@90-99)
488line (7@70-99): thing (7@70-79) thing (8@80-89) ';' (9@90-99)
489sending: 'x' (10@100-109)
490thing (10@100-109): 'x' (10@100-109)
491sending: ';' (11@110-119)
492line (10@100-119): thing (10@100-109) ';' (11@110-119)
493sending: 'y' (12@120-129)
494120-129: syntax error, unexpected $undefined, expecting $end or 'x'
495sending: EOF
496Freeing nterm line (10@100-119)
497Freeing nterm line (7@70-99)
498Freeing nterm line (-1@0-69)
499Parsing FAILED.
500]])
501
502AT_CLEANUP
503])
504
505
506# AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS])
507# ------------------------------------------------
508# Produce `calc.y'.
509m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
510[_AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4], [$1])
511])
512
513
514AT_CHECK_PRINTER_AND_DESTRUCTOR()
515
516# FIXME. This test case fails.
517#AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])