]>
Commit | Line | Data |
---|---|---|
3c31a486 | 1 | # Exercising Bison on conflicts. -*- Autotest -*- |
643a5994 | 2 | # Copyright (C) 2002 Free Software Foundation, Inc. |
3c31a486 AD |
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([[Conflicts.]]) | |
20 | ||
21 | ||
643a5994 AD |
22 | ## ---------------- ## |
23 | ## S/R in initial. ## | |
24 | ## ---------------- ## | |
25 | ||
26 | # I once hacked Bison in such a way that it lost its reductions on the | |
27 | # initial state (because it was confusing it with the last state). It | |
28 | # took me a while to strip down my failures to this simple case. So | |
29 | # make sure it finds the s/r conflict below. | |
30 | ||
31 | AT_SETUP([S/R in initial]) | |
32 | ||
33 | AT_DATA([[input.y]], | |
34 | [[%expect 1 | |
35 | %% | |
36 | exp: e 'e'; | |
37 | e: 'e' | /* Nothing. */; | |
38 | ]]) | |
39 | ||
40 | AT_CHECK([bison input.y -o input.c]) | |
41 | ||
42 | AT_CLEANUP | |
43 | ||
bc933ef1 | 44 | |
3c31a486 AD |
45 | ## ------------------- ## |
46 | ## %nonassoc and eof. ## | |
47 | ## ------------------- ## | |
48 | ||
49 | AT_SETUP([%nonassoc and eof]) | |
50 | ||
51 | AT_DATA([input.y], | |
52 | [[ | |
53 | %{ | |
1207eeac AD |
54 | #include <config.h> |
55 | /* We don't need a perfect malloc for these tests. */ | |
56 | #undef malloc | |
3c31a486 | 57 | #include <stdio.h> |
1207eeac AD |
58 | |
59 | #if STDC_HEADERS | |
60 | # include <stdlib.h> | |
61 | #endif | |
62 | ||
3c31a486 | 63 | #define YYERROR_VERBOSE 1 |
1207eeac AD |
64 | static void |
65 | yyerror (const char *msg) | |
66 | { | |
67 | fprintf (stderr, "%s\n", msg); | |
68 | exit (1); | |
69 | } | |
3c31a486 AD |
70 | |
71 | /* The current argument. */ | |
72 | static const char *input = NULL; | |
73 | ||
74 | static int | |
75 | yylex (void) | |
76 | { | |
77 | /* No token stands for end of file. */ | |
78 | if (input && *input) | |
79 | return *input++; | |
80 | else | |
81 | return 0; | |
82 | } | |
83 | ||
84 | %} | |
85 | ||
86 | %nonassoc '<' '>' | |
87 | ||
88 | %% | |
89 | expr: expr '<' expr | |
90 | | expr '>' expr | |
91 | | '0' | |
92 | ; | |
93 | %% | |
94 | int | |
95 | main (int argc, const char *argv[]) | |
96 | { | |
97 | if (argc > 1) | |
98 | input = argv[1]; | |
99 | return yyparse (); | |
100 | } | |
101 | ]]) | |
102 | ||
103 | # Specify the output files to avoid problems on different file systems. | |
104 | AT_CHECK([bison input.y -o input.c]) | |
105 | AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore]) | |
106 | ||
107 | AT_CHECK([./input '0<0']) | |
108 | # FIXME: This is an actual bug, but a new one, in the sense that | |
109 | # no one has ever spotted it! The messages are *wrong*: there should | |
110 | # be nothing there, it should be expected eof. | |
111 | AT_CHECK([./input '0<0<0'], [1], [], | |
112 | [parse error, unexpected '<', expecting '<' or '>' | |
113 | ]) | |
114 | ||
115 | AT_CHECK([./input '0>0']) | |
116 | AT_CHECK([./input '0>0>0'], [1], [], | |
117 | [parse error, unexpected '>', expecting '<' or '>' | |
118 | ]) | |
119 | ||
120 | AT_CHECK([./input '0<0>0'], [1], [], | |
121 | [parse error, unexpected '>', expecting '<' or '>' | |
122 | ]) | |
123 | ||
124 | AT_CLEANUP | |
125 | ||
126 | ||
127 | ||
128 | ## ------------------------- ## | |
129 | ## Unresolved SR Conflicts. ## | |
130 | ## ------------------------- ## | |
131 | ||
132 | AT_SETUP([Unresolved SR Conflicts]) | |
133 | ||
6b98e4b5 AD |
134 | AT_KEYWORDS([report]) |
135 | ||
3c31a486 AD |
136 | AT_DATA([input.y], |
137 | [[%token NUM OP | |
138 | %% | |
139 | exp: exp OP exp | NUM; | |
140 | ]]) | |
141 | ||
b408954b | 142 | AT_CHECK([bison input.y -o input.c --report=all], 0, [], |
3c31a486 AD |
143 | [input.y contains 1 shift/reduce conflict. |
144 | ]) | |
145 | ||
146 | # Check the contents of the report. | |
147 | AT_CHECK([cat input.output], [], | |
148 | [[State 5 contains 1 shift/reduce conflict. | |
149 | ||
150 | ||
151 | Grammar | |
152 | ||
6b98e4b5 AD |
153 | 0 $axiom: exp $ |
154 | ||
155 | 1 exp: exp OP exp | |
156 | 2 | NUM | |
3c31a486 AD |
157 | |
158 | ||
159 | Terminals, with rules where they appear | |
160 | ||
161 | $ (0) 0 | |
162 | error (256) | |
007a50a4 AD |
163 | NUM (258) 2 |
164 | OP (259) 1 | |
3c31a486 AD |
165 | |
166 | ||
167 | Nonterminals, with rules where they appear | |
168 | ||
169 | $axiom (5) | |
170 | on left: 0 | |
171 | exp (6) | |
172 | on left: 1 2, on right: 0 1 | |
173 | ||
174 | ||
175 | state 0 | |
176 | ||
ce4ccb4b AD |
177 | 0 $axiom: . exp $ |
178 | 1 exp: . exp OP exp | |
179 | 2 | . NUM | |
643a5994 | 180 | |
3c31a486 AD |
181 | NUM shift, and go to state 1 |
182 | ||
183 | exp go to state 2 | |
184 | ||
185 | ||
186 | ||
187 | state 1 | |
188 | ||
ce4ccb4b | 189 | 2 exp: NUM . |
3c31a486 AD |
190 | |
191 | $default reduce using rule 2 (exp) | |
192 | ||
193 | ||
194 | ||
195 | state 2 | |
196 | ||
ce4ccb4b AD |
197 | 0 $axiom: exp . $ |
198 | 1 exp: exp . OP exp | |
3c31a486 AD |
199 | |
200 | $ shift, and go to state 3 | |
201 | OP shift, and go to state 4 | |
202 | ||
203 | ||
204 | ||
205 | state 3 | |
206 | ||
ce4ccb4b | 207 | 0 $axiom: exp $ . |
3c31a486 AD |
208 | |
209 | $default accept | |
210 | ||
211 | ||
212 | state 4 | |
213 | ||
ce4ccb4b AD |
214 | 1 exp: . exp OP exp |
215 | 1 | exp OP . exp | |
216 | 2 | . NUM | |
3c31a486 AD |
217 | |
218 | NUM shift, and go to state 1 | |
219 | ||
220 | exp go to state 5 | |
221 | ||
222 | ||
223 | ||
224 | state 5 | |
225 | ||
ce4ccb4b AD |
226 | 1 exp: exp . OP exp [$, OP] |
227 | 1 | exp OP exp . [$, OP] | |
3c31a486 AD |
228 | |
229 | OP shift, and go to state 4 | |
230 | ||
231 | OP [reduce using rule 1 (exp)] | |
232 | $default reduce using rule 1 (exp) | |
233 | ||
234 | ||
235 | ||
236 | ]]) | |
237 | ||
238 | AT_CLEANUP | |
239 | ||
240 | ||
3c31a486 | 241 | |
ce4ccb4b AD |
242 | ## ----------------------- ## |
243 | ## Resolved SR Conflicts. ## | |
244 | ## ----------------------- ## | |
245 | ||
246 | AT_SETUP([Resolved SR Conflicts]) | |
3c31a486 | 247 | |
6b98e4b5 AD |
248 | AT_KEYWORDS([report]) |
249 | ||
3c31a486 AD |
250 | AT_DATA([input.y], |
251 | [[%token NUM OP | |
ce4ccb4b | 252 | %left OP |
3c31a486 AD |
253 | %% |
254 | exp: exp OP exp | NUM; | |
255 | ]]) | |
256 | ||
ce4ccb4b | 257 | AT_CHECK([bison input.y -o input.c --report=all]) |
3c31a486 AD |
258 | |
259 | # Check the contents of the report. | |
260 | AT_CHECK([cat input.output], [], | |
ce4ccb4b | 261 | [[Grammar |
3c31a486 | 262 | |
6b98e4b5 AD |
263 | 0 $axiom: exp $ |
264 | ||
265 | 1 exp: exp OP exp | |
266 | 2 | NUM | |
3c31a486 AD |
267 | |
268 | ||
269 | Terminals, with rules where they appear | |
270 | ||
271 | $ (0) 0 | |
272 | error (256) | |
007a50a4 AD |
273 | NUM (258) 2 |
274 | OP (259) 1 | |
3c31a486 AD |
275 | |
276 | ||
277 | Nonterminals, with rules where they appear | |
278 | ||
279 | $axiom (5) | |
280 | on left: 0 | |
281 | exp (6) | |
282 | on left: 1 2, on right: 0 1 | |
283 | ||
284 | ||
285 | state 0 | |
286 | ||
ce4ccb4b AD |
287 | 0 $axiom: . exp $ |
288 | 1 exp: . exp OP exp | |
289 | 2 | . NUM | |
643a5994 | 290 | |
3c31a486 AD |
291 | NUM shift, and go to state 1 |
292 | ||
293 | exp go to state 2 | |
294 | ||
295 | ||
296 | ||
297 | state 1 | |
298 | ||
ce4ccb4b | 299 | 2 exp: NUM . |
3c31a486 AD |
300 | |
301 | $default reduce using rule 2 (exp) | |
302 | ||
303 | ||
304 | ||
305 | state 2 | |
306 | ||
ce4ccb4b AD |
307 | 0 $axiom: exp . $ |
308 | 1 exp: exp . OP exp | |
3c31a486 AD |
309 | |
310 | $ shift, and go to state 3 | |
311 | OP shift, and go to state 4 | |
312 | ||
313 | ||
314 | ||
315 | state 3 | |
316 | ||
ce4ccb4b | 317 | 0 $axiom: exp $ . |
3c31a486 AD |
318 | |
319 | $default accept | |
320 | ||
321 | ||
322 | state 4 | |
323 | ||
ce4ccb4b AD |
324 | 1 exp: . exp OP exp |
325 | 1 | exp OP . exp | |
326 | 2 | . NUM | |
3c31a486 AD |
327 | |
328 | NUM shift, and go to state 1 | |
329 | ||
330 | exp go to state 5 | |
331 | ||
332 | ||
333 | ||
334 | state 5 | |
335 | ||
ce4ccb4b AD |
336 | 1 exp: exp . OP exp [$, OP] |
337 | 1 | exp OP exp . [$, OP] | |
3c31a486 | 338 | |
3c31a486 | 339 | |
bc933ef1 AD |
340 | $default reduce using rule 1 (exp) |
341 | ||
ce4ccb4b | 342 | Conflict between rule 2 and token OP resolved as shift (%left OP). |
bc933ef1 AD |
343 | |
344 | ||
345 | ]]) | |
346 | ||
347 | AT_CLEANUP | |
348 | ||
349 | ||
bc933ef1 AD |
350 | ## -------------------------------- ## |
351 | ## Defaulted Conflicted Reduction. ## | |
352 | ## -------------------------------- ## | |
353 | ||
354 | # When there are RR conflicts, some rules are disabled. Usually it is | |
355 | # simply displayed as: | |
356 | # | |
357 | # $ reduce using rule 3 (num) | |
358 | # $ [reduce using rule 4 (id)] | |
359 | # | |
360 | # But when `reduce 3' is the default action, we'd produce: | |
361 | # | |
362 | # $ [reduce using rule 4 (id)] | |
363 | # $default reduce using rule 3 (num) | |
364 | # | |
365 | # In this precise case (a reduction is masked by the default | |
366 | # reduction), we make the `reduce 3' explicit: | |
367 | # | |
368 | # $ reduce using rule 3 (num) | |
369 | # $ [reduce using rule 4 (id)] | |
370 | # $default reduce using rule 3 (num) | |
371 | # | |
372 | # Maybe that's not the best display, but then, please propose something | |
373 | # else. | |
374 | ||
375 | AT_SETUP([Defaulted Conflicted Reduction]) | |
376 | AT_KEYWORDS([report]) | |
377 | ||
378 | AT_DATA([input.y], | |
379 | [[%% | |
380 | exp: num | id; | |
381 | num: '0'; | |
382 | id : '0'; | |
383 | %% | |
384 | ]]) | |
385 | ||
386 | AT_CHECK([bison input.y -o input.c --report=all], 1, [], | |
387 | [input.y contains 1 reduce/reduce conflict. | |
388 | ]) | |
389 | ||
390 | # Check the contents of the report. | |
391 | AT_CHECK([cat input.output], [], | |
392 | [[State 1 contains 1 reduce/reduce conflict. | |
393 | ||
394 | ||
395 | Grammar | |
396 | ||
397 | 0 $axiom: exp $ | |
398 | ||
399 | 1 exp: num | |
400 | 2 | id | |
401 | ||
402 | 3 num: '0' | |
403 | ||
404 | 4 id: '0' | |
405 | ||
406 | ||
407 | Terminals, with rules where they appear | |
408 | ||
409 | $ (0) 0 | |
410 | '0' (48) 3 4 | |
411 | error (256) | |
412 | ||
413 | ||
414 | Nonterminals, with rules where they appear | |
415 | ||
416 | $axiom (4) | |
417 | on left: 0 | |
418 | exp (5) | |
419 | on left: 1 2, on right: 0 | |
420 | num (6) | |
421 | on left: 3, on right: 1 | |
422 | id (7) | |
423 | on left: 4, on right: 2 | |
424 | ||
425 | ||
426 | state 0 | |
427 | ||
ce4ccb4b AD |
428 | 0 $axiom: . exp $ |
429 | 1 exp: . num | |
430 | 2 | . id | |
431 | 3 num: . '0' | |
432 | 4 id: . '0' | |
bc933ef1 AD |
433 | |
434 | '0' shift, and go to state 1 | |
435 | ||
436 | exp go to state 2 | |
437 | num go to state 3 | |
438 | id go to state 4 | |
439 | ||
440 | ||
441 | ||
442 | state 1 | |
443 | ||
ce4ccb4b AD |
444 | 3 num: '0' . [$] |
445 | 4 id: '0' . [$] | |
bc933ef1 AD |
446 | |
447 | $ reduce using rule 3 (num) | |
448 | $ [reduce using rule 4 (id)] | |
449 | $default reduce using rule 3 (num) | |
450 | ||
451 | ||
452 | ||
453 | state 2 | |
454 | ||
ce4ccb4b | 455 | 0 $axiom: exp . $ |
bc933ef1 AD |
456 | |
457 | $ shift, and go to state 5 | |
458 | ||
459 | ||
460 | ||
461 | state 3 | |
462 | ||
ce4ccb4b | 463 | 1 exp: num . |
bc933ef1 | 464 | |
3c31a486 AD |
465 | $default reduce using rule 1 (exp) |
466 | ||
bc933ef1 AD |
467 | |
468 | ||
469 | state 4 | |
470 | ||
ce4ccb4b | 471 | 2 exp: id . |
bc933ef1 AD |
472 | |
473 | $default reduce using rule 2 (exp) | |
474 | ||
475 | ||
476 | ||
477 | state 5 | |
478 | ||
ce4ccb4b | 479 | 0 $axiom: exp $ . |
bc933ef1 AD |
480 | |
481 | $default accept | |
3c31a486 AD |
482 | |
483 | ||
484 | ]]) | |
485 | ||
486 | AT_CLEANUP | |
487 | ||
488 | ||
489 | ||
490 | ||
491 | ## -------------------- ## | |
492 | ## %expect not enough. ## | |
493 | ## -------------------- ## | |
494 | ||
495 | AT_SETUP([%expect not enough]) | |
496 | ||
497 | AT_DATA([input.y], | |
498 | [[%token NUM OP | |
499 | %expect 0 | |
500 | %% | |
501 | exp: exp OP exp | NUM; | |
502 | ]]) | |
503 | ||
504 | AT_CHECK([bison input.y -o input.c], 1, [], | |
505 | [input.y contains 1 shift/reduce conflict. | |
506 | expected 0 shift/reduce conflicts | |
507 | ]) | |
508 | AT_CLEANUP | |
509 | ||
510 | ||
511 | ## --------------- ## | |
512 | ## %expect right. ## | |
513 | ## --------------- ## | |
514 | ||
515 | AT_SETUP([%expect right]) | |
516 | ||
517 | AT_DATA([input.y], | |
518 | [[%token NUM OP | |
519 | %expect 1 | |
520 | %% | |
521 | exp: exp OP exp | NUM; | |
522 | ]]) | |
523 | ||
bc933ef1 | 524 | AT_CHECK([bison input.y -o input.c]) |
3c31a486 AD |
525 | AT_CLEANUP |
526 | ||
527 | ||
528 | ## ------------------ ## | |
529 | ## %expect too much. ## | |
530 | ## ------------------ ## | |
531 | ||
532 | AT_SETUP([%expect too much]) | |
533 | ||
534 | AT_DATA([input.y], | |
535 | [[%token NUM OP | |
536 | %expect 2 | |
537 | %% | |
538 | exp: exp OP exp | NUM; | |
539 | ]]) | |
540 | ||
541 | AT_CHECK([bison input.y -o input.c], 1, [], | |
542 | [input.y contains 1 shift/reduce conflict. | |
543 | expected 2 shift/reduce conflicts | |
544 | ]) | |
545 | AT_CLEANUP |