]> git.saurik.com Git - bison.git/blame - tests/actions.at
* tests/actions.at (Destructors): Augment to test locations.
[bison.git] / tests / actions.at
CommitLineData
82c035a8
AD
1# Executing Actions. -*- Autotest -*-
2# Copyright 2001 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([[input.y]],
33[[%{
d99361e6
AD
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
82c035a8
AD
40%}
41%%
931394cb
AD
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'); }
82c035a8
AD
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 input.y -d -v -o input.c])
76AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
30d2f3d5 77AT_CHECK([./input], 0,
931394cb 78[[0123456789
82c035a8
AD
79]])
80
81AT_CLEANUP
75d1fe16
AD
82
83
84
85## ---------------- ##
86## Exotic Dollars. ##
87## ---------------- ##
88
89AT_SETUP([Exotic Dollars])
90
75d1fe16
AD
91AT_DATA([[input.y]],
92[[%{
93# include <stdio.h>
94# include <stdlib.h>
95 static void yyerror (const char *msg);
96 static int yylex (void);
97# define YYDEBUG 1
98# define YYERROR_VERBOSE 1
99%}
100
101%union
102{
103 int val;
104};
105
106%type <val> a_1 a_2 a_4 a_5
107 sum_of_the_five_previous_values
108
109%%
110exp: a_1 a_2 { $<val>$ = 3; } a_4 a_5 sum_of_the_five_previous_values
111 {
112 printf ("%d\n", $6);
113 }
114;
115a_1: { $$ = 1; };
116a_2: { $$ = 2; };
117a_4: { $$ = 4; };
118a_5: { $$ = 5; };
119
120sum_of_the_five_previous_values:
121 {
122 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
123 }
124;
125
126%%
127static int
128yylex (void)
129{
130 return EOF;
131}
132
133static void
134yyerror (const char *msg)
135{
136 fprintf (stderr, "%s\n", msg);
137}
138
139int
140main (void)
141{
142 return yyparse ();
143}
144]])
145
146AT_CHECK([bison input.y -d -v -o input.c])
147AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
148AT_CHECK([./input], 0,
149[[15
150]])
151
152AT_CLEANUP
9280d3ef
AD
153
154
155
156## ------------- ##
157## Destructors. ##
158## ------------- ##
159
160AT_SETUP([Destructors])
161
162# Make sure complex $n work.
163
164AT_DATA([[input.y]],
165[[%{
166#include <stdio.h>
167#include <stdlib.h>
168#include <assert.h>
169
170#define YYERROR_VERBOSE 1
171#define YYDEBUG 1
5719c109 172#define YYPRINT yyprint
9280d3ef 173%}
5719c109 174%verbose
9280d3ef
AD
175%union
176{
177 int ival;
178}
5719c109 179%type <ival> 'x' thing line input
93b68a0e
AD
180%destructor { printf ("Freeing input %d from %d\n", $$, @$.first_line); } input
181%destructor { printf ("Freeing line %d from %d\n", $$, @$.first_line); } line
182%destructor { printf ("Freeing thing %d from %d\n", $$, @$.first_line); } thing
183%destructor { printf ("Freeing 'x' %d from %d\n", $$, @$.first_line); } 'x'
9280d3ef 184
5719c109
AD
185%{
186static int yylex (void);
187static void yyerror (const char *msg);
188static void yyprint (FILE *out, int num, YYSTYPE val);
189%}
190
191
9280d3ef
AD
192%%
193input:
194 /* Nothing. */
5719c109
AD
195 {
196 $$ = 0;
197 printf ("input(%d): /* Nothing */';'\n", $$);
198 }
199| line input /* Right recursive to load the stack so that popping at
200 EOF can be exercised. */
201 {
202 $$ = 2;
203 printf ("input(%d): line(%d) input(%d)';'\n", $$, $1, $2);
204 }
9280d3ef
AD
205;
206
207line:
208 thing thing thing ';'
5719c109
AD
209 {
210 $$ = $1;
211 printf ("line(%d): thing(%d) thing(%d) thing(%d) ';'\n", $$, $1, $2, $3);
212 }
9280d3ef 213| thing thing ';'
5719c109
AD
214 {
215 $$ = $1;
216 printf ("line(%d): thing(%d) thing(%d) ';'\n", $$, $1, $2);
217 }
9280d3ef 218| thing ';'
5719c109
AD
219 {
220 $$ = $1;
221 printf ("line(%d): thing(%d) ';'\n", $$, $1);
222 }
9280d3ef 223| error ';'
5719c109
AD
224 {
225 $$ = -1;
226 printf ("line(%d): error ';'\n", $$);
227 }
9280d3ef
AD
228;
229
230thing:
5719c109
AD
231 'x'
232 {
233 $$ = $1;
234 printf ("thing(%d): 'x'(%d)\n", $$, $1);
235 }
9280d3ef
AD
236;
237%%
238static int
239yylex (void)
240{
5719c109 241 static const unsigned int input[] =
9280d3ef 242 {
5719c109
AD
243 /* Exericise the discarding of stack top and input until `error'
244 can be reduced. */
9280d3ef 245 'x', 'x', 'x', 'x', 'x', 'x', ';',
5719c109
AD
246
247 /* Load the stack and provoke an error that cannot be caught be
248 the grammar, and check that the stack is cleared. */
9280d3ef
AD
249 'x', 'x', ';',
250 'x', ';',
5719c109 251 'y'
9280d3ef
AD
252 };
253 static int counter = 0;
254
255 if (counter < (sizeof(input) / sizeof (input[0])))
256 {
257 yylval.ival = counter;
5719c109 258 printf ("sending: '%c'(%d)\n", input[counter], counter);
93b68a0e
AD
259 /* As in BASIC, line numbers go from 10 to 10. */
260 yylloc.first_line = 10 * counter;
9280d3ef
AD
261 return input[counter++];
262 }
263 else
5719c109
AD
264 {
265 printf ("sending: EOF\n");
266 return EOF;
267 }
9280d3ef
AD
268}
269
270static void
271yyerror (const char *msg)
272{
93b68a0e 273 fprintf (stdout, "%d: %s\n", yylloc.first_line, msg);
9280d3ef
AD
274}
275
276static void
5719c109 277yyprint (FILE *out, int num, YYSTYPE val)
9280d3ef 278{
5719c109 279 fprintf (out, " = %d", val.ival);
9280d3ef
AD
280}
281
282int
283main (void)
284{
285 yydebug = !!getenv ("YYDEBUG");
286 if (yyparse ())
287 {
288 fprintf (stdout, "Parsing FAILED.\n");
289 exit (1);
290 }
291 fprintf (stdout, "Successful parse.\n");
292 return 0;
293}
294]])
295
93b68a0e 296AT_CHECK([bison input.y --location -d -v -o input.c])
9280d3ef 297AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
5719c109
AD
298AT_CHECK([./input], 1,
299[[sending: 'x'(0)
300thing(0): 'x'(0)
301sending: 'x'(1)
302thing(1): 'x'(1)
303sending: 'x'(2)
304thing(2): 'x'(2)
305sending: 'x'(3)
93b68a0e
AD
30630: parse error, unexpected 'x', expecting ';'
307Freeing thing 2 from 20
308Freeing thing 1 from 10
309Freeing thing 0 from 0
310Freeing 'x' 3 from 30
5719c109 311sending: 'x'(4)
93b68a0e 312Freeing 'x' 4 from 40
5719c109 313sending: 'x'(5)
93b68a0e 314Freeing 'x' 5 from 50
5719c109
AD
315sending: ';'(6)
316line(-1): error ';'
317sending: 'x'(7)
318thing(7): 'x'(7)
319sending: 'x'(8)
320thing(8): 'x'(8)
321sending: ';'(9)
322line(7): thing(7) thing(8) ';'
323sending: 'x'(10)
324thing(10): 'x'(10)
325sending: ';'(11)
326line(10): thing(10) ';'
327sending: 'y'(12)
93b68a0e 328120: parse error, unexpected $undefined., expecting $ or error or 'x'
5719c109 329sending: EOF
93b68a0e
AD
330Freeing line 10 from 100
331Freeing line 7 from 70
332Freeing line -1 from 50
5719c109 333Parsing FAILED.
9280d3ef
AD
334]])
335
336AT_CLEANUP