]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
9501dc6e | 32 | AT_DATA_GRAMMAR([[input.y]], |
82c035a8 | 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 |
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'); } | |
82c035a8 AD |
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 | ||
b56471a6 | 75 | AT_CHECK([bison -d -v -o input.c input.y]) |
1154cced AD |
76 | AT_COMPILE([input]) |
77 | AT_PARSER_CHECK([./input], 0, | |
931394cb | 78 | [[0123456789 |
82c035a8 AD |
79 | ]]) |
80 | ||
81 | AT_CLEANUP | |
75d1fe16 AD |
82 | |
83 | ||
84 | ||
5dac0025 PE |
85 | ## ---------------------- ## |
86 | ## Actions after errors. ## | |
87 | ## ---------------------- ## | |
88 | ||
89 | AT_SETUP([Actions after errors]) | |
90 | ||
91 | ||
92 | ||
93 | AT_DATA_GRAMMAR([[input.y]], | |
94 | [[%{ | |
95 | #include <stdio.h> | |
96 | #include <stdlib.h> | |
97 | ||
98 | static int yylex (void); | |
99 | static void yyerror (char const *); | |
100 | ||
101 | #define YYDEBUG 1 | |
102 | %} | |
103 | %union { int ival; } | |
104 | %type <ival> 'x' ';' thing line input | |
105 | ||
106 | %% | |
107 | input: | |
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 | ||
121 | line: | |
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 | ||
145 | thing: | |
146 | 'x' | |
147 | { | |
148 | $$ = $1; | |
149 | printf ("thing(%d): 'x'(%d)\n", $$, $1); | |
150 | } | |
151 | ; | |
152 | %% | |
153 | static size_t counter; | |
154 | ||
155 | static int | |
156 | yylex (void) | |
157 | { | |
158 | static char const input[] = | |
159 | { | |
160 | /* Exericise 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' (value = %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 | ||
184 | static void | |
185 | yyerror (char const *msg) | |
186 | { | |
187 | printf ("%lu: %s\n", (unsigned long int) counter, msg); | |
188 | } | |
189 | ||
190 | int | |
191 | main (void) | |
192 | { | |
193 | yydebug = !!getenv ("YYDEBUG"); | |
194 | return yyparse (); | |
195 | } | |
196 | ]]) | |
197 | ||
198 | AT_CHECK([bison -o input.c input.y]) | |
199 | AT_COMPILE([input]) | |
200 | AT_PARSER_CHECK([./input], 1, | |
201 | [[sending: 'x' (value = 0) | |
202 | thing(0): 'x'(0) | |
203 | sending: 'x' (value = 1) | |
204 | thing(1): 'x'(1) | |
205 | sending: 'x' (value = 2) | |
206 | thing(2): 'x'(2) | |
207 | sending: 'x' (value = 3) | |
208 | 4: syntax error | |
209 | sending: 'x' (value = 4) | |
210 | sending: 'x' (value = 5) | |
211 | sending: ';' (value = 6) | |
212 | line(-1): error ';'(6) | |
213 | sending: 'x' (value = 7) | |
214 | thing(7): 'x'(7) | |
215 | sending: 'x' (value = 8) | |
216 | thing(8): 'x'(8) | |
217 | sending: ';' (value = 9) | |
218 | line(7): thing(7) thing(8) ';'(9) | |
219 | sending: 'x' (value = 10) | |
220 | thing(10): 'x'(10) | |
221 | sending: ';' (value = 11) | |
222 | line(10): thing(10) ';'(11) | |
223 | sending: 'y' (value = 12) | |
224 | 13: syntax error | |
225 | sending: EOF | |
226 | ]]) | |
227 | ||
228 | AT_CLEANUP | |
229 | ||
230 | ||
231 | ||
75d1fe16 AD |
232 | ## ---------------- ## |
233 | ## Exotic Dollars. ## | |
234 | ## ---------------- ## | |
235 | ||
236 | AT_SETUP([Exotic Dollars]) | |
237 | ||
9501dc6e | 238 | AT_DATA_GRAMMAR([[input.y]], |
75d1fe16 AD |
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 | ||
0ff67d71 | 253 | %type <val> a_1 a_2 a_5 |
75d1fe16 AD |
254 | sum_of_the_five_previous_values |
255 | ||
256 | %% | |
0ff67d71 PE |
257 | exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5 |
258 | sum_of_the_five_previous_values | |
75d1fe16 AD |
259 | { |
260 | printf ("%d\n", $6); | |
261 | } | |
262 | ; | |
263 | a_1: { $$ = 1; }; | |
264 | a_2: { $$ = 2; }; | |
75d1fe16 AD |
265 | a_5: { $$ = 5; }; |
266 | ||
267 | sum_of_the_five_previous_values: | |
268 | { | |
269 | $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4; | |
270 | } | |
271 | ; | |
272 | ||
273 | %% | |
274 | static int | |
275 | yylex (void) | |
276 | { | |
277 | return EOF; | |
278 | } | |
279 | ||
280 | static void | |
281 | yyerror (const char *msg) | |
282 | { | |
283 | fprintf (stderr, "%s\n", msg); | |
284 | } | |
285 | ||
286 | int | |
287 | main (void) | |
288 | { | |
289 | return yyparse (); | |
290 | } | |
291 | ]]) | |
292 | ||
b56471a6 | 293 | AT_CHECK([bison -d -v -o input.c input.y]) |
1154cced AD |
294 | AT_COMPILE([input]) |
295 | AT_PARSER_CHECK([./input], 0, | |
75d1fe16 AD |
296 | [[15 |
297 | ]]) | |
298 | ||
299 | AT_CLEANUP | |
9280d3ef AD |
300 | |
301 | ||
302 | ||
e776192e AD |
303 | ## -------------------------- ## |
304 | ## Printers and Destructors. ## | |
305 | ## -------------------------- ## | |
9280d3ef | 306 | |
3df37415 AD |
307 | # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, BISON-DIRECTIVE) |
308 | # ------------------------------------------------------------- | |
309 | m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR], | |
310 | [m4_if([$1$2$3], $[1]$[2]$[3], [], | |
311 | [m4_fatal([$0: Invalid arguments: $@])])dnl | |
312 | ||
313 | AT_SETUP([Printers and Destructors: $4]) | |
9280d3ef AD |
314 | |
315 | # Make sure complex $n work. | |
316 | ||
9501dc6e | 317 | AT_DATA_GRAMMAR([[input.y]], |
3df37415 AD |
318 | [[$4 |
319 | %{ | |
9280d3ef AD |
320 | #include <stdio.h> |
321 | #include <stdlib.h> | |
322 | #include <assert.h> | |
323 | ||
7bd6c77e AD |
324 | static int yylex (void); |
325 | static void yyerror (const char *msg); | |
9280d3ef | 326 | %} |
7bd6c77e AD |
327 | %error-verbose |
328 | %debug | |
5719c109 | 329 | %verbose |
7bd6c77e | 330 | %locations |
9280d3ef AD |
331 | %union |
332 | { | |
333 | int ival; | |
334 | } | |
5719c109 | 335 | %type <ival> 'x' thing line input |
e3170060 | 336 | |
283f1e64 | 337 | %printer { fprintf (yyout, "%d@%d", $$, @$.first_line); } |
e3170060 AD |
338 | input line thing 'x' |
339 | ||
340 | %destructor | |
283f1e64 | 341 | { fprintf (stdout, "Freeing nterm input (%d@%d)\n", $$, @$.first_line); } |
7bd6c77e | 342 | input |
5719c109 | 343 | |
7bd6c77e | 344 | %destructor |
283f1e64 | 345 | { fprintf (stdout, "Freeing nterm line (%d@%d)\n", $$, @$.first_line); } |
7bd6c77e AD |
346 | line |
347 | ||
348 | %destructor | |
283f1e64 | 349 | { fprintf (stdout, "Freeing nterm thing (%d@%d)\n", $$, @$.first_line); } |
7bd6c77e AD |
350 | thing |
351 | ||
352 | %destructor | |
283f1e64 | 353 | { fprintf (stdout, "Freeing token 'x' (%d@%d)\n", $$, @$.first_line); } |
7bd6c77e | 354 | 'x' |
5719c109 | 355 | |
9280d3ef AD |
356 | %% |
357 | input: | |
358 | /* Nothing. */ | |
5719c109 AD |
359 | { |
360 | $$ = 0; | |
283f1e64 | 361 | printf ("input(%d@%d): /* Nothing */';'\n", $$, @$.first_line); |
5719c109 AD |
362 | } |
363 | | line input /* Right recursive to load the stack so that popping at | |
364 | EOF can be exercised. */ | |
365 | { | |
366 | $$ = 2; | |
283f1e64 AD |
367 | printf ("input(%d@%d): line(%d@%d) input(%d@%d)';'\n", |
368 | $$, @$.first_line, $1, @1.first_line, $2, @2.first_line); | |
5719c109 | 369 | } |
9280d3ef AD |
370 | ; |
371 | ||
372 | line: | |
373 | thing thing thing ';' | |
5719c109 AD |
374 | { |
375 | $$ = $1; | |
283f1e64 AD |
376 | printf ("line(%d@%d): thing(%d@%d) thing(%d@%d) thing(%d@%d) ';'\n", |
377 | $$, @$.first_line, $1, @1.first_line, $2, @2.first_line, | |
378 | $3, @3.first_line); | |
5719c109 | 379 | } |
9280d3ef | 380 | | thing thing ';' |
5719c109 AD |
381 | { |
382 | $$ = $1; | |
283f1e64 AD |
383 | printf ("line(%d@%d): thing(%d@%d) thing(%d@%d) ';'\n", |
384 | $$, @$.first_line, $1, @1.first_line, $2, @2.first_line); | |
5719c109 | 385 | } |
9280d3ef | 386 | | thing ';' |
5719c109 AD |
387 | { |
388 | $$ = $1; | |
283f1e64 AD |
389 | printf ("line(%d@%d): thing(%d@%d) ';'\n", |
390 | $$, @$.first_line, $1, @1.first_line); | |
5719c109 | 391 | } |
9280d3ef | 392 | | error ';' |
5719c109 AD |
393 | { |
394 | $$ = -1; | |
283f1e64 AD |
395 | printf ("line(%d@%d): error(@%d) ';'\n", |
396 | $$, @$.first_line, @1.first_line); | |
5719c109 | 397 | } |
9280d3ef AD |
398 | ; |
399 | ||
400 | thing: | |
5719c109 AD |
401 | 'x' |
402 | { | |
403 | $$ = $1; | |
283f1e64 AD |
404 | printf ("thing(%d@%d): 'x'(%d@%d)\n", |
405 | $$, @$.first_line, $1, @1.first_line); | |
5719c109 | 406 | } |
9280d3ef AD |
407 | ; |
408 | %% | |
409 | static int | |
410 | yylex (void) | |
411 | { | |
5719c109 | 412 | static const unsigned int input[] = |
9280d3ef | 413 | { |
5719c109 AD |
414 | /* Exericise the discarding of stack top and input until `error' |
415 | can be reduced. */ | |
9280d3ef | 416 | 'x', 'x', 'x', 'x', 'x', 'x', ';', |
5719c109 | 417 | |
5a08f1ce AD |
418 | /* Load the stack and provoke an error that cannot be caught by |
419 | the grammar, to check that the stack is cleared. */ | |
9280d3ef AD |
420 | 'x', 'x', ';', |
421 | 'x', ';', | |
5719c109 | 422 | 'y' |
9280d3ef | 423 | }; |
5a08f1ce | 424 | static unsigned int counter = 0; |
9280d3ef AD |
425 | |
426 | if (counter < (sizeof(input) / sizeof (input[0]))) | |
427 | { | |
428 | yylval.ival = counter; | |
93b68a0e AD |
429 | /* As in BASIC, line numbers go from 10 to 10. */ |
430 | yylloc.first_line = 10 * counter; | |
5a08f1ce AD |
431 | printf ("sending: '%c' (value = %d, line %d)\n", |
432 | input[counter], yylval.ival, yylloc.first_line); | |
433 | return (int) input[counter++]; | |
9280d3ef AD |
434 | } |
435 | else | |
5719c109 AD |
436 | { |
437 | printf ("sending: EOF\n"); | |
438 | return EOF; | |
439 | } | |
9280d3ef AD |
440 | } |
441 | ||
442 | static void | |
443 | yyerror (const char *msg) | |
444 | { | |
93b68a0e | 445 | fprintf (stdout, "%d: %s\n", yylloc.first_line, msg); |
9280d3ef AD |
446 | } |
447 | ||
9280d3ef AD |
448 | int |
449 | main (void) | |
450 | { | |
451 | yydebug = !!getenv ("YYDEBUG"); | |
452 | if (yyparse ()) | |
453 | { | |
454 | fprintf (stdout, "Parsing FAILED.\n"); | |
455 | exit (1); | |
456 | } | |
457 | fprintf (stdout, "Successful parse.\n"); | |
458 | return 0; | |
459 | } | |
460 | ]]) | |
461 | ||
7bd6c77e | 462 | AT_CHECK([bison -o input.c input.y]) |
1154cced AD |
463 | AT_COMPILE([input]) |
464 | AT_PARSER_CHECK([./input], 1, | |
5a08f1ce | 465 | [[sending: 'x' (value = 0, line 0) |
283f1e64 | 466 | thing(0@0): 'x'(0@0) |
5a08f1ce | 467 | sending: 'x' (value = 1, line 10) |
283f1e64 | 468 | thing(1@10): 'x'(1@10) |
5a08f1ce | 469 | sending: 'x' (value = 2, line 20) |
283f1e64 | 470 | thing(2@20): 'x'(2@20) |
5a08f1ce | 471 | sending: 'x' (value = 3, line 30) |
6e649e65 | 472 | 30: syntax error, unexpected 'x', expecting ';' |
283f1e64 AD |
473 | Freeing nterm thing (2@20) |
474 | Freeing nterm thing (1@10) | |
475 | Freeing nterm thing (0@0) | |
476 | Freeing token 'x' (3@30) | |
5a08f1ce | 477 | sending: 'x' (value = 4, line 40) |
283f1e64 | 478 | Freeing token 'x' (4@40) |
5a08f1ce | 479 | sending: 'x' (value = 5, line 50) |
283f1e64 | 480 | Freeing token 'x' (5@50) |
5a08f1ce | 481 | sending: ';' (value = 6, line 60) |
283f1e64 | 482 | line(-1@50): error(@50) ';' |
5a08f1ce | 483 | sending: 'x' (value = 7, line 70) |
283f1e64 | 484 | thing(7@70): 'x'(7@70) |
5a08f1ce | 485 | sending: 'x' (value = 8, line 80) |
283f1e64 | 486 | thing(8@80): 'x'(8@80) |
5a08f1ce | 487 | sending: ';' (value = 9, line 90) |
283f1e64 | 488 | line(7@70): thing(7@70) thing(8@80) ';' |
5a08f1ce | 489 | sending: 'x' (value = 10, line 100) |
283f1e64 | 490 | thing(10@100): 'x'(10@100) |
5a08f1ce | 491 | sending: ';' (value = 11, line 110) |
283f1e64 | 492 | line(10@100): thing(10@100) ';' |
5a08f1ce | 493 | sending: 'y' (value = 12, line 120) |
6e649e65 | 494 | 120: syntax error, unexpected $undefined, expecting $end or 'x' |
5719c109 | 495 | sending: EOF |
283f1e64 AD |
496 | Freeing nterm line (10@100) |
497 | Freeing nterm line (7@70) | |
498 | Freeing nterm line (-1@50) | |
5719c109 | 499 | Parsing FAILED. |
9280d3ef AD |
500 | ]]) |
501 | ||
502 | AT_CLEANUP | |
3df37415 AD |
503 | ]) |
504 | ||
505 | ||
506 | # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS]) | |
507 | # ------------------------------------------------ | |
508 | # Produce `calc.y'. | |
509 | m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR], | |
510 | [_AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], [$1]) | |
511 | ]) | |
512 | ||
513 | ||
514 | AT_CHECK_PRINTER_AND_DESTRUCTOR() | |
515 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser]) |