]> git.saurik.com Git - bison.git/blob - tests/actions.at
* data/bison.simple (b4_pure_if): New.
[bison.git] / tests / actions.at
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
19 AT_BANNER([[User Actions.]])
20
21 ## ------------------ ##
22 ## Mid-rule actions. ##
23 ## ------------------ ##
24
25 AT_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
32 AT_DATA([[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 %%
42 exp: { 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 %%
55 static int
56 yylex (void)
57 {
58 static const char *input = "123456789";
59 return *input++;
60 }
61
62 static void
63 yyerror (const char *msg)
64 {
65 fprintf (stderr, "%s\n", msg);
66 }
67
68 int
69 main (void)
70 {
71 return yyparse ();
72 }
73 ]])
74
75 AT_CHECK([bison input.y -d -v -o input.c])
76 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
77 AT_CHECK([./input], 0,
78 [[0123456789
79 ]])
80
81 AT_CLEANUP
82
83
84
85 ## ---------------- ##
86 ## Exotic Dollars. ##
87 ## ---------------- ##
88
89 AT_SETUP([Exotic Dollars])
90
91 AT_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 %%
110 exp: a_1 a_2 { $<val>$ = 3; } a_4 a_5 sum_of_the_five_previous_values
111 {
112 printf ("%d\n", $6);
113 }
114 ;
115 a_1: { $$ = 1; };
116 a_2: { $$ = 2; };
117 a_4: { $$ = 4; };
118 a_5: { $$ = 5; };
119
120 sum_of_the_five_previous_values:
121 {
122 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
123 }
124 ;
125
126 %%
127 static int
128 yylex (void)
129 {
130 return EOF;
131 }
132
133 static void
134 yyerror (const char *msg)
135 {
136 fprintf (stderr, "%s\n", msg);
137 }
138
139 int
140 main (void)
141 {
142 return yyparse ();
143 }
144 ]])
145
146 AT_CHECK([bison input.y -d -v -o input.c])
147 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
148 AT_CHECK([./input], 0,
149 [[15
150 ]])
151
152 AT_CLEANUP
153
154
155
156 ## ------------- ##
157 ## Destructors. ##
158 ## ------------- ##
159
160 AT_SETUP([Destructors])
161
162 # Make sure complex $n work.
163
164 AT_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
172 #define YYPRINT yyprint
173 %}
174 %verbose
175 %union
176 {
177 int ival;
178 }
179 %type <ival> 'x' thing line input
180 %destructor { printf ("Freeing input %d\n", $$); } input
181 %destructor { printf ("Freeing line %d\n", $$); } line
182 %destructor { printf ("Freeing thing %d\n", $$); } thing
183 %destructor { printf ("Freeing 'x' %d\n", $$); } 'x'
184
185 %{
186 static int yylex (void);
187 static void yyerror (const char *msg);
188 static void yyprint (FILE *out, int num, YYSTYPE val);
189 %}
190
191
192 %%
193 input:
194 /* Nothing. */
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 }
205 ;
206
207 line:
208 thing thing thing ';'
209 {
210 $$ = $1;
211 printf ("line(%d): thing(%d) thing(%d) thing(%d) ';'\n", $$, $1, $2, $3);
212 }
213 | thing thing ';'
214 {
215 $$ = $1;
216 printf ("line(%d): thing(%d) thing(%d) ';'\n", $$, $1, $2);
217 }
218 | thing ';'
219 {
220 $$ = $1;
221 printf ("line(%d): thing(%d) ';'\n", $$, $1);
222 }
223 | error ';'
224 {
225 $$ = -1;
226 printf ("line(%d): error ';'\n", $$);
227 }
228 ;
229
230 thing:
231 'x'
232 {
233 $$ = $1;
234 printf ("thing(%d): 'x'(%d)\n", $$, $1);
235 }
236 ;
237 %%
238 static int
239 yylex (void)
240 {
241 static const unsigned int input[] =
242 {
243 /* Exericise the discarding of stack top and input until `error'
244 can be reduced. */
245 'x', 'x', 'x', 'x', 'x', 'x', ';',
246
247 /* Load the stack and provoke an error that cannot be caught be
248 the grammar, and check that the stack is cleared. */
249 'x', 'x', ';',
250 'x', ';',
251 'y'
252 };
253 static int counter = 0;
254
255 if (counter < (sizeof(input) / sizeof (input[0])))
256 {
257 yylval.ival = counter;
258 printf ("sending: '%c'(%d)\n", input[counter], counter);
259 return input[counter++];
260 }
261 else
262 {
263 printf ("sending: EOF\n");
264 return EOF;
265 }
266 }
267
268 static void
269 yyerror (const char *msg)
270 {
271 fprintf (stdout, "%s\n", msg);
272 }
273
274 static void
275 yyprint (FILE *out, int num, YYSTYPE val)
276 {
277 fprintf (out, " = %d", val.ival);
278 }
279
280 int
281 main (void)
282 {
283 yydebug = !!getenv ("YYDEBUG");
284 if (yyparse ())
285 {
286 fprintf (stdout, "Parsing FAILED.\n");
287 exit (1);
288 }
289 fprintf (stdout, "Successful parse.\n");
290 return 0;
291 }
292 ]])
293
294 AT_CHECK([bison input.y -d -v -o input.c])
295 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
296 AT_CHECK([./input], 1,
297 [[sending: 'x'(0)
298 thing(0): 'x'(0)
299 sending: 'x'(1)
300 thing(1): 'x'(1)
301 sending: 'x'(2)
302 thing(2): 'x'(2)
303 sending: 'x'(3)
304 parse error, unexpected 'x', expecting ';'
305 Freeing thing 2
306 Freeing thing 1
307 Freeing thing 0
308 Freeing 'x' 3
309 sending: 'x'(4)
310 Freeing 'x' 4
311 sending: 'x'(5)
312 Freeing 'x' 5
313 sending: ';'(6)
314 line(-1): error ';'
315 sending: 'x'(7)
316 thing(7): 'x'(7)
317 sending: 'x'(8)
318 thing(8): 'x'(8)
319 sending: ';'(9)
320 line(7): thing(7) thing(8) ';'
321 sending: 'x'(10)
322 thing(10): 'x'(10)
323 sending: ';'(11)
324 line(10): thing(10) ';'
325 sending: 'y'(12)
326 parse error, unexpected $undefined., expecting $ or error or 'x'
327 sending: EOF
328 Freeing line 10
329 Freeing line 7
330 Freeing line -1
331 Parsing FAILED.
332 ]])
333
334 AT_CLEANUP