]> git.saurik.com Git - bison.git/blob - tests/actions.at
0ed50fe49723a3cfa0530d7d06a3cdf543647aba
[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 -d -v -o input.c input.y])
76 AT_COMPILE([input])
77 AT_PARSER_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_5
107 sum_of_the_five_previous_values
108
109 %%
110 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
111 sum_of_the_five_previous_values
112 {
113 printf ("%d\n", $6);
114 }
115 ;
116 a_1: { $$ = 1; };
117 a_2: { $$ = 2; };
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 -d -v -o input.c input.y])
147 AT_COMPILE([input])
148 AT_PARSER_CHECK([./input], 0,
149 [[15
150 ]])
151
152 AT_CLEANUP
153
154
155
156 ## -------------------------- ##
157 ## Printers and Destructors. ##
158 ## -------------------------- ##
159
160 AT_SETUP([Printers and 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 static int yylex (void);
171 static void yyerror (const char *msg);
172 %}
173 %error-verbose
174 %debug
175 %verbose
176 %locations
177 %union
178 {
179 int ival;
180 }
181 %type <ival> 'x' thing line input
182
183 %printer { fprintf (yyout, "%d from %d", $$, @$.first_line); }
184 input line thing 'x'
185
186 %destructor
187 { fprintf (stdout, "Freeing nterm input (%d from %d)\n", $$, @$.first_line); }
188 input
189
190 %destructor
191 { fprintf (stdout, "Freeing nterm line (%d from %d)\n", $$, @$.first_line); }
192 line
193
194 %destructor
195 { fprintf (stdout, "Freeing nterm thing (%d from %d)\n", $$, @$.first_line); }
196 thing
197
198 %destructor
199 { fprintf (stdout, "Freeing token 'x' (%d from %d)\n", $$, @$.first_line); }
200 'x'
201
202 %%
203 input:
204 /* Nothing. */
205 {
206 $$ = 0;
207 printf ("input(%d): /* Nothing */';'\n", $$);
208 }
209 | line input /* Right recursive to load the stack so that popping at
210 EOF can be exercised. */
211 {
212 $$ = 2;
213 printf ("input(%d): line(%d) input(%d)';'\n", $$, $1, $2);
214 }
215 ;
216
217 line:
218 thing thing thing ';'
219 {
220 $$ = $1;
221 printf ("line(%d): thing(%d) thing(%d) thing(%d) ';'\n", $$, $1, $2, $3);
222 }
223 | thing thing ';'
224 {
225 $$ = $1;
226 printf ("line(%d): thing(%d) thing(%d) ';'\n", $$, $1, $2);
227 }
228 | thing ';'
229 {
230 $$ = $1;
231 printf ("line(%d): thing(%d) ';'\n", $$, $1);
232 }
233 | error ';'
234 {
235 $$ = -1;
236 printf ("line(%d): error ';'\n", $$);
237 }
238 ;
239
240 thing:
241 'x'
242 {
243 $$ = $1;
244 printf ("thing(%d): 'x'(%d)\n", $$, $1);
245 }
246 ;
247 %%
248 static int
249 yylex (void)
250 {
251 static const unsigned int input[] =
252 {
253 /* Exericise the discarding of stack top and input until `error'
254 can be reduced. */
255 'x', 'x', 'x', 'x', 'x', 'x', ';',
256
257 /* Load the stack and provoke an error that cannot be caught by
258 the grammar, to check that the stack is cleared. */
259 'x', 'x', ';',
260 'x', ';',
261 'y'
262 };
263 static unsigned int counter = 0;
264
265 if (counter < (sizeof(input) / sizeof (input[0])))
266 {
267 yylval.ival = counter;
268 /* As in BASIC, line numbers go from 10 to 10. */
269 yylloc.first_line = 10 * counter;
270 printf ("sending: '%c' (value = %d, line %d)\n",
271 input[counter], yylval.ival, yylloc.first_line);
272 return (int) input[counter++];
273 }
274 else
275 {
276 printf ("sending: EOF\n");
277 return EOF;
278 }
279 }
280
281 static void
282 yyerror (const char *msg)
283 {
284 fprintf (stdout, "%d: %s\n", yylloc.first_line, msg);
285 }
286
287 int
288 main (void)
289 {
290 yydebug = !!getenv ("YYDEBUG");
291 if (yyparse ())
292 {
293 fprintf (stdout, "Parsing FAILED.\n");
294 exit (1);
295 }
296 fprintf (stdout, "Successful parse.\n");
297 return 0;
298 }
299 ]])
300
301 AT_CHECK([bison -o input.c input.y])
302 AT_COMPILE([input])
303 AT_PARSER_CHECK([./input], 1,
304 [[sending: 'x' (value = 0, line 0)
305 thing(0): 'x'(0)
306 sending: 'x' (value = 1, line 10)
307 thing(1): 'x'(1)
308 sending: 'x' (value = 2, line 20)
309 thing(2): 'x'(2)
310 sending: 'x' (value = 3, line 30)
311 30: parse error, unexpected 'x', expecting ';'
312 Freeing nterm thing (2 from 20)
313 Freeing nterm thing (1 from 10)
314 Freeing nterm thing (0 from 0)
315 Freeing token 'x' (3 from 30)
316 sending: 'x' (value = 4, line 40)
317 Freeing token 'x' (4 from 40)
318 sending: 'x' (value = 5, line 50)
319 Freeing token 'x' (5 from 50)
320 sending: ';' (value = 6, line 60)
321 line(-1): error ';'
322 sending: 'x' (value = 7, line 70)
323 thing(7): 'x'(7)
324 sending: 'x' (value = 8, line 80)
325 thing(8): 'x'(8)
326 sending: ';' (value = 9, line 90)
327 line(7): thing(7) thing(8) ';'
328 sending: 'x' (value = 10, line 100)
329 thing(10): 'x'(10)
330 sending: ';' (value = 11, line 110)
331 line(10): thing(10) ';'
332 sending: 'y' (value = 12, line 120)
333 120: parse error, unexpected $undefined, expecting $end or 'x'
334 sending: EOF
335 Freeing nterm line (10 from 100)
336 Freeing nterm line (7 from 70)
337 Freeing nterm line (-1 from 50)
338 Parsing FAILED.
339 ]])
340
341 AT_CLEANUP