]> git.saurik.com Git - bison.git/blob - tests/actions.at
* data/bison.simple (yysymprint): Don't print the token number,
[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 %}
173 %verbose
174 %union
175 {
176 int ival;
177 }
178 %type <ival> 'x' thing line input
179
180 %printer { fprintf (yyout, "%d from %d", $$, @$.first_line); }
181 input line thing 'x'
182
183 %destructor
184 {
185 fprintf (stdout, "Freeing ");
186 /* FIXME: Ouch: INTERNAL DETAILS EXPOSED HERE. */
187 /* Cannot use $$ which is the union member, not the union itself. */
188 yysymprint (stdout, yytype, yyvalue, @$);
189 fprintf (stdout, "\n");
190 }
191 input line thing 'x'
192
193 %{
194 static int yylex (void);
195 static void yyerror (const char *msg);
196 static void yyprint (FILE *out, int num, YYSTYPE val);
197 %}
198
199
200 %%
201 input:
202 /* Nothing. */
203 {
204 $$ = 0;
205 printf ("input(%d): /* Nothing */';'\n", $$);
206 }
207 | line input /* Right recursive to load the stack so that popping at
208 EOF can be exercised. */
209 {
210 $$ = 2;
211 printf ("input(%d): line(%d) input(%d)';'\n", $$, $1, $2);
212 }
213 ;
214
215 line:
216 thing thing thing ';'
217 {
218 $$ = $1;
219 printf ("line(%d): thing(%d) thing(%d) thing(%d) ';'\n", $$, $1, $2, $3);
220 }
221 | thing thing ';'
222 {
223 $$ = $1;
224 printf ("line(%d): thing(%d) thing(%d) ';'\n", $$, $1, $2);
225 }
226 | thing ';'
227 {
228 $$ = $1;
229 printf ("line(%d): thing(%d) ';'\n", $$, $1);
230 }
231 | error ';'
232 {
233 $$ = -1;
234 printf ("line(%d): error ';'\n", $$);
235 }
236 ;
237
238 thing:
239 'x'
240 {
241 $$ = $1;
242 printf ("thing(%d): 'x'(%d)\n", $$, $1);
243 }
244 ;
245 %%
246 static int
247 yylex (void)
248 {
249 static const unsigned int input[] =
250 {
251 /* Exericise the discarding of stack top and input until `error'
252 can be reduced. */
253 'x', 'x', 'x', 'x', 'x', 'x', ';',
254
255 /* Load the stack and provoke an error that cannot be caught be
256 the grammar, and check that the stack is cleared. */
257 'x', 'x', ';',
258 'x', ';',
259 'y'
260 };
261 static int counter = 0;
262
263 if (counter < (sizeof(input) / sizeof (input[0])))
264 {
265 yylval.ival = counter;
266 printf ("sending: '%c' (line %d)\n", input[counter], counter);
267 /* As in BASIC, line numbers go from 10 to 10. */
268 yylloc.first_line = 10 * counter;
269 return input[counter++];
270 }
271 else
272 {
273 printf ("sending: EOF\n");
274 return EOF;
275 }
276 }
277
278 static void
279 yyerror (const char *msg)
280 {
281 fprintf (stdout, "%d: %s\n", yylloc.first_line, msg);
282 }
283
284 int
285 main (void)
286 {
287 yydebug = !!getenv ("YYDEBUG");
288 if (yyparse ())
289 {
290 fprintf (stdout, "Parsing FAILED.\n");
291 exit (1);
292 }
293 fprintf (stdout, "Successful parse.\n");
294 return 0;
295 }
296 ]])
297
298 AT_CHECK([bison input.y --location -d -v -o input.c])
299 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
300 AT_CHECK([./input], 1,
301 [[sending: 'x' (line 0)
302 thing(0): 'x'(0)
303 sending: 'x' (line 1)
304 thing(1): 'x'(1)
305 sending: 'x' (line 2)
306 thing(2): 'x'(2)
307 sending: 'x' (line 3)
308 30: parse error, unexpected 'x', expecting ';'
309 Freeing nterm thing (2 from 20)
310 Freeing nterm thing (1 from 10)
311 Freeing nterm thing (0 from 0)
312 Freeing token 'x' (3 from 30)
313 sending: 'x' (line 4)
314 Freeing token 'x' (4 from 40)
315 sending: 'x' (line 5)
316 Freeing token 'x' (5 from 50)
317 sending: ';' (line 6)
318 line(-1): error ';'
319 sending: 'x' (line 7)
320 thing(7): 'x'(7)
321 sending: 'x' (line 8)
322 thing(8): 'x'(8)
323 sending: ';' (line 9)
324 line(7): thing(7) thing(8) ';'
325 sending: 'x' (line 10)
326 thing(10): 'x'(10)
327 sending: ';' (line 11)
328 line(10): thing(10) ';'
329 sending: 'y' (line 12)
330 120: parse error, unexpected $undefined., expecting $ or error or 'x'
331 sending: EOF
332 Freeing nterm line (10 from 100)
333 Freeing nterm line (7 from 70)
334 Freeing nterm line (-1 from 50)
335 Parsing FAILED.
336 ]])
337
338 AT_CLEANUP