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