]>
Commit | Line | Data |
---|---|---|
3c31a486 | 1 | # Exercising Bison on conflicts. -*- Autotest -*- |
69363a9e PE |
2 | |
3 | # Copyright (C) 2002, 2003 Free Software Foundation, Inc. | |
3c31a486 AD |
4 | |
5 | # This program is free software; you can redistribute it and/or modify | |
6 | # it under the terms of the GNU General Public License as published by | |
7 | # the Free Software Foundation; either version 2, or (at your option) | |
8 | # any later version. | |
9 | ||
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
14 | ||
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with this program; if not, write to the Free Software | |
17 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
18 | # 02111-1307, USA. | |
19 | ||
20 | AT_BANNER([[Conflicts.]]) | |
21 | ||
22 | ||
643a5994 AD |
23 | ## ---------------- ## |
24 | ## S/R in initial. ## | |
25 | ## ---------------- ## | |
26 | ||
27 | # I once hacked Bison in such a way that it lost its reductions on the | |
28 | # initial state (because it was confusing it with the last state). It | |
29 | # took me a while to strip down my failures to this simple case. So | |
30 | # make sure it finds the s/r conflict below. | |
31 | ||
32 | AT_SETUP([S/R in initial]) | |
33 | ||
34 | AT_DATA([[input.y]], | |
35 | [[%expect 1 | |
36 | %% | |
37 | exp: e 'e'; | |
38 | e: 'e' | /* Nothing. */; | |
39 | ]]) | |
40 | ||
b56471a6 | 41 | AT_CHECK([bison -o input.c input.y], 0, [], |
2bf21a83 | 42 | [[input.y:4.9: warning: rule never reduced because of conflicts: e: /* empty */ |
e8832397 | 43 | ]]) |
643a5994 AD |
44 | |
45 | AT_CLEANUP | |
46 | ||
bc933ef1 | 47 | |
3c31a486 AD |
48 | ## ------------------- ## |
49 | ## %nonassoc and eof. ## | |
50 | ## ------------------- ## | |
51 | ||
52 | AT_SETUP([%nonassoc and eof]) | |
53 | ||
9501dc6e | 54 | AT_DATA_GRAMMAR([input.y], |
3c31a486 AD |
55 | [[ |
56 | %{ | |
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. | |
b56471a6 | 104 | AT_CHECK([bison -o input.c input.y]) |
1154cced | 105 | AT_COMPILE([input]) |
3c31a486 | 106 | |
1154cced | 107 | AT_PARSER_CHECK([./input '0<0']) |
3c31a486 AD |
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. | |
1154cced | 111 | AT_PARSER_CHECK([./input '0<0<0'], [1], [], |
6e649e65 | 112 | [syntax error, unexpected '<', expecting '<' or '>' |
3c31a486 AD |
113 | ]) |
114 | ||
1154cced AD |
115 | AT_PARSER_CHECK([./input '0>0']) |
116 | AT_PARSER_CHECK([./input '0>0>0'], [1], [], | |
6e649e65 | 117 | [syntax error, unexpected '>', expecting '<' or '>' |
3c31a486 AD |
118 | ]) |
119 | ||
1154cced | 120 | AT_PARSER_CHECK([./input '0<0>0'], [1], [], |
6e649e65 | 121 | [syntax error, unexpected '>', expecting '<' or '>' |
3c31a486 AD |
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 | ||
b56471a6 | 142 | AT_CHECK([bison -o input.c --report=all input.y], 0, [], |
2c8ba4cd | 143 | [input.y: conflicts: 1 shift/reduce |
3c31a486 AD |
144 | ]) |
145 | ||
146 | # Check the contents of the report. | |
147 | AT_CHECK([cat input.output], [], | |
2c8ba4cd | 148 | [[State 5 conflicts: 1 shift/reduce |
3c31a486 AD |
149 | |
150 | ||
151 | Grammar | |
152 | ||
88bce5a2 | 153 | 0 $accept: exp $end |
6b98e4b5 AD |
154 | |
155 | 1 exp: exp OP exp | |
156 | 2 | NUM | |
3c31a486 AD |
157 | |
158 | ||
159 | Terminals, with rules where they appear | |
160 | ||
88bce5a2 | 161 | $end (0) 0 |
3c31a486 | 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 | ||
88bce5a2 | 169 | $accept (5) |
3c31a486 AD |
170 | on left: 0 |
171 | exp (6) | |
172 | on left: 1 2, on right: 0 1 | |
173 | ||
174 | ||
175 | state 0 | |
176 | ||
88bce5a2 | 177 | 0 $accept: . exp $end |
ce4ccb4b AD |
178 | 1 exp: . exp OP exp |
179 | 2 | . NUM | |
643a5994 | 180 | |
87675353 | 181 | NUM shift, and go to state 1 |
3c31a486 | 182 | |
87675353 | 183 | exp go to state 2 |
3c31a486 AD |
184 | |
185 | ||
186 | state 1 | |
187 | ||
ce4ccb4b | 188 | 2 exp: NUM . |
3c31a486 | 189 | |
87675353 | 190 | $default reduce using rule 2 (exp) |
3c31a486 AD |
191 | |
192 | ||
193 | state 2 | |
194 | ||
88bce5a2 | 195 | 0 $accept: exp . $end |
ce4ccb4b | 196 | 1 exp: exp . OP exp |
3c31a486 | 197 | |
88bce5a2 AD |
198 | $end shift, and go to state 3 |
199 | OP shift, and go to state 4 | |
3c31a486 AD |
200 | |
201 | ||
202 | state 3 | |
203 | ||
88bce5a2 | 204 | 0 $accept: exp $end . |
3c31a486 | 205 | |
e8832397 | 206 | $default accept |
3c31a486 AD |
207 | |
208 | ||
209 | state 4 | |
210 | ||
ce4ccb4b AD |
211 | 1 exp: . exp OP exp |
212 | 1 | exp OP . exp | |
213 | 2 | . NUM | |
3c31a486 | 214 | |
87675353 | 215 | NUM shift, and go to state 1 |
3c31a486 | 216 | |
87675353 | 217 | exp go to state 5 |
3c31a486 AD |
218 | |
219 | ||
220 | state 5 | |
221 | ||
88bce5a2 AD |
222 | 1 exp: exp . OP exp [$end, OP] |
223 | 1 | exp OP exp . [$end, OP] | |
3c31a486 | 224 | |
87675353 | 225 | OP shift, and go to state 4 |
3c31a486 | 226 | |
87675353 AD |
227 | OP [reduce using rule 1 (exp)] |
228 | $default reduce using rule 1 (exp) | |
3c31a486 AD |
229 | ]]) |
230 | ||
231 | AT_CLEANUP | |
232 | ||
233 | ||
3c31a486 | 234 | |
ce4ccb4b AD |
235 | ## ----------------------- ## |
236 | ## Resolved SR Conflicts. ## | |
237 | ## ----------------------- ## | |
238 | ||
239 | AT_SETUP([Resolved SR Conflicts]) | |
3c31a486 | 240 | |
6b98e4b5 AD |
241 | AT_KEYWORDS([report]) |
242 | ||
3c31a486 AD |
243 | AT_DATA([input.y], |
244 | [[%token NUM OP | |
ce4ccb4b | 245 | %left OP |
3c31a486 AD |
246 | %% |
247 | exp: exp OP exp | NUM; | |
248 | ]]) | |
249 | ||
b56471a6 | 250 | AT_CHECK([bison -o input.c --report=all input.y]) |
3c31a486 AD |
251 | |
252 | # Check the contents of the report. | |
253 | AT_CHECK([cat input.output], [], | |
ce4ccb4b | 254 | [[Grammar |
3c31a486 | 255 | |
88bce5a2 | 256 | 0 $accept: exp $end |
6b98e4b5 AD |
257 | |
258 | 1 exp: exp OP exp | |
259 | 2 | NUM | |
3c31a486 AD |
260 | |
261 | ||
262 | Terminals, with rules where they appear | |
263 | ||
88bce5a2 | 264 | $end (0) 0 |
3c31a486 | 265 | error (256) |
007a50a4 AD |
266 | NUM (258) 2 |
267 | OP (259) 1 | |
3c31a486 AD |
268 | |
269 | ||
270 | Nonterminals, with rules where they appear | |
271 | ||
88bce5a2 | 272 | $accept (5) |
3c31a486 AD |
273 | on left: 0 |
274 | exp (6) | |
275 | on left: 1 2, on right: 0 1 | |
276 | ||
277 | ||
278 | state 0 | |
279 | ||
88bce5a2 | 280 | 0 $accept: . exp $end |
ce4ccb4b AD |
281 | 1 exp: . exp OP exp |
282 | 2 | . NUM | |
643a5994 | 283 | |
87675353 | 284 | NUM shift, and go to state 1 |
3c31a486 | 285 | |
87675353 | 286 | exp go to state 2 |
3c31a486 AD |
287 | |
288 | ||
289 | state 1 | |
290 | ||
ce4ccb4b | 291 | 2 exp: NUM . |
3c31a486 | 292 | |
87675353 | 293 | $default reduce using rule 2 (exp) |
3c31a486 AD |
294 | |
295 | ||
296 | state 2 | |
297 | ||
88bce5a2 | 298 | 0 $accept: exp . $end |
ce4ccb4b | 299 | 1 exp: exp . OP exp |
3c31a486 | 300 | |
88bce5a2 AD |
301 | $end shift, and go to state 3 |
302 | OP shift, and go to state 4 | |
3c31a486 AD |
303 | |
304 | ||
305 | state 3 | |
306 | ||
88bce5a2 | 307 | 0 $accept: exp $end . |
3c31a486 | 308 | |
e8832397 | 309 | $default accept |
3c31a486 AD |
310 | |
311 | ||
312 | state 4 | |
313 | ||
ce4ccb4b AD |
314 | 1 exp: . exp OP exp |
315 | 1 | exp OP . exp | |
316 | 2 | . NUM | |
3c31a486 | 317 | |
87675353 | 318 | NUM shift, and go to state 1 |
3c31a486 | 319 | |
87675353 | 320 | exp go to state 5 |
3c31a486 AD |
321 | |
322 | ||
323 | state 5 | |
324 | ||
88bce5a2 AD |
325 | 1 exp: exp . OP exp [$end, OP] |
326 | 1 | exp OP exp . [$end, OP] | |
3c31a486 | 327 | |
87675353 | 328 | $default reduce using rule 1 (exp) |
7ea9a33f | 329 | |
4b3d3a8e | 330 | Conflict between rule 1 and token OP resolved as reduce (%left OP). |
bc933ef1 AD |
331 | ]]) |
332 | ||
333 | AT_CLEANUP | |
334 | ||
335 | ||
bc933ef1 AD |
336 | ## -------------------------------- ## |
337 | ## Defaulted Conflicted Reduction. ## | |
338 | ## -------------------------------- ## | |
339 | ||
340 | # When there are RR conflicts, some rules are disabled. Usually it is | |
341 | # simply displayed as: | |
342 | # | |
88bce5a2 AD |
343 | # $end reduce using rule 3 (num) |
344 | # $end [reduce using rule 4 (id)] | |
bc933ef1 AD |
345 | # |
346 | # But when `reduce 3' is the default action, we'd produce: | |
347 | # | |
88bce5a2 | 348 | # $end [reduce using rule 4 (id)] |
bc933ef1 AD |
349 | # $default reduce using rule 3 (num) |
350 | # | |
351 | # In this precise case (a reduction is masked by the default | |
352 | # reduction), we make the `reduce 3' explicit: | |
353 | # | |
88bce5a2 AD |
354 | # $end reduce using rule 3 (num) |
355 | # $end [reduce using rule 4 (id)] | |
bc933ef1 AD |
356 | # $default reduce using rule 3 (num) |
357 | # | |
358 | # Maybe that's not the best display, but then, please propose something | |
359 | # else. | |
360 | ||
361 | AT_SETUP([Defaulted Conflicted Reduction]) | |
362 | AT_KEYWORDS([report]) | |
363 | ||
364 | AT_DATA([input.y], | |
365 | [[%% | |
366 | exp: num | id; | |
367 | num: '0'; | |
368 | id : '0'; | |
369 | %% | |
370 | ]]) | |
371 | ||
b56471a6 | 372 | AT_CHECK([bison -o input.c --report=all input.y], 0, [], |
2c8ba4cd | 373 | [[input.y: conflicts: 1 reduce/reduce |
2bf21a83 | 374 | input.y:4.6-8: warning: rule never reduced because of conflicts: id: '0' |
e8832397 | 375 | ]]) |
bc933ef1 AD |
376 | |
377 | # Check the contents of the report. | |
378 | AT_CHECK([cat input.output], [], | |
c8f002c7 AD |
379 | [[Rules never reduced |
380 | ||
381 | 4 id: '0' | |
382 | ||
383 | ||
2c8ba4cd | 384 | State 1 conflicts: 1 reduce/reduce |
bc933ef1 AD |
385 | |
386 | ||
387 | Grammar | |
388 | ||
88bce5a2 | 389 | 0 $accept: exp $end |
bc933ef1 AD |
390 | |
391 | 1 exp: num | |
392 | 2 | id | |
393 | ||
394 | 3 num: '0' | |
395 | ||
396 | 4 id: '0' | |
397 | ||
398 | ||
399 | Terminals, with rules where they appear | |
400 | ||
88bce5a2 | 401 | $end (0) 0 |
bc933ef1 AD |
402 | '0' (48) 3 4 |
403 | error (256) | |
404 | ||
405 | ||
406 | Nonterminals, with rules where they appear | |
407 | ||
88bce5a2 | 408 | $accept (4) |
bc933ef1 AD |
409 | on left: 0 |
410 | exp (5) | |
411 | on left: 1 2, on right: 0 | |
412 | num (6) | |
413 | on left: 3, on right: 1 | |
414 | id (7) | |
415 | on left: 4, on right: 2 | |
416 | ||
417 | ||
418 | state 0 | |
419 | ||
88bce5a2 | 420 | 0 $accept: . exp $end |
ce4ccb4b AD |
421 | 1 exp: . num |
422 | 2 | . id | |
423 | 3 num: . '0' | |
424 | 4 id: . '0' | |
bc933ef1 | 425 | |
87675353 | 426 | '0' shift, and go to state 1 |
bc933ef1 | 427 | |
87675353 AD |
428 | exp go to state 2 |
429 | num go to state 3 | |
430 | id go to state 4 | |
bc933ef1 AD |
431 | |
432 | ||
433 | state 1 | |
434 | ||
88bce5a2 AD |
435 | 3 num: '0' . [$end] |
436 | 4 id: '0' . [$end] | |
bc933ef1 | 437 | |
88bce5a2 AD |
438 | $end reduce using rule 3 (num) |
439 | $end [reduce using rule 4 (id)] | |
87675353 | 440 | $default reduce using rule 3 (num) |
bc933ef1 AD |
441 | |
442 | ||
443 | state 2 | |
444 | ||
88bce5a2 | 445 | 0 $accept: exp . $end |
bc933ef1 | 446 | |
88bce5a2 | 447 | $end shift, and go to state 5 |
bc933ef1 AD |
448 | |
449 | ||
450 | state 3 | |
451 | ||
ce4ccb4b | 452 | 1 exp: num . |
bc933ef1 | 453 | |
87675353 | 454 | $default reduce using rule 1 (exp) |
bc933ef1 AD |
455 | |
456 | ||
457 | state 4 | |
458 | ||
ce4ccb4b | 459 | 2 exp: id . |
bc933ef1 | 460 | |
87675353 | 461 | $default reduce using rule 2 (exp) |
bc933ef1 AD |
462 | |
463 | ||
464 | state 5 | |
465 | ||
88bce5a2 | 466 | 0 $accept: exp $end . |
bc933ef1 | 467 | |
e8832397 | 468 | $default accept |
3c31a486 AD |
469 | ]]) |
470 | ||
471 | AT_CLEANUP | |
472 | ||
473 | ||
474 | ||
475 | ||
476 | ## -------------------- ## | |
477 | ## %expect not enough. ## | |
478 | ## -------------------- ## | |
479 | ||
480 | AT_SETUP([%expect not enough]) | |
481 | ||
482 | AT_DATA([input.y], | |
483 | [[%token NUM OP | |
484 | %expect 0 | |
485 | %% | |
486 | exp: exp OP exp | NUM; | |
487 | ]]) | |
488 | ||
69363a9e | 489 | AT_CHECK([bison -o input.c input.y], 0, [], |
2c8ba4cd | 490 | [input.y: conflicts: 1 shift/reduce |
69363a9e | 491 | input.y: warning: expected 0 shift/reduce conflicts |
3c31a486 AD |
492 | ]) |
493 | AT_CLEANUP | |
494 | ||
495 | ||
496 | ## --------------- ## | |
497 | ## %expect right. ## | |
498 | ## --------------- ## | |
499 | ||
500 | AT_SETUP([%expect right]) | |
501 | ||
502 | AT_DATA([input.y], | |
503 | [[%token NUM OP | |
504 | %expect 1 | |
505 | %% | |
506 | exp: exp OP exp | NUM; | |
507 | ]]) | |
508 | ||
b56471a6 | 509 | AT_CHECK([bison -o input.c input.y]) |
3c31a486 AD |
510 | AT_CLEANUP |
511 | ||
512 | ||
513 | ## ------------------ ## | |
514 | ## %expect too much. ## | |
515 | ## ------------------ ## | |
516 | ||
517 | AT_SETUP([%expect too much]) | |
518 | ||
519 | AT_DATA([input.y], | |
520 | [[%token NUM OP | |
521 | %expect 2 | |
522 | %% | |
523 | exp: exp OP exp | NUM; | |
524 | ]]) | |
525 | ||
69363a9e | 526 | AT_CHECK([bison -o input.c input.y], 0, [], |
2c8ba4cd | 527 | [input.y: conflicts: 1 shift/reduce |
69363a9e | 528 | input.y: warning: expected 2 shift/reduce conflicts |
3c31a486 AD |
529 | ]) |
530 | AT_CLEANUP | |
6876ecd3 PE |
531 | |
532 | ||
533 | ## ------------------------------ ## | |
534 | ## %expect with reduce conflicts ## | |
535 | ## ------------------------------ ## | |
536 | ||
537 | AT_SETUP([%expect with reduce conflicts]) | |
538 | ||
539 | AT_DATA([input.y], | |
540 | [[%expect 0 | |
541 | %% | |
542 | program: a 'a' | a a; | |
543 | a: 'a'; | |
544 | ]]) | |
545 | ||
69363a9e | 546 | AT_CHECK([bison -o input.c input.y], 0, [], |
2c8ba4cd | 547 | [input.y: conflicts: 1 reduce/reduce |
69363a9e | 548 | input.y: warning: expected 0 reduce/reduce conflicts |
6876ecd3 PE |
549 | ]) |
550 | AT_CLEANUP | |
39a06c25 PE |
551 | |
552 | ||
22fccf95 PE |
553 | ## ------------------------------- ## |
554 | ## %no-default-prec without %prec ## | |
555 | ## ------------------------------- ## | |
39a06c25 | 556 | |
22fccf95 | 557 | AT_SETUP([%no-default-prec without %prec]) |
39a06c25 PE |
558 | |
559 | AT_DATA([[input.y]], | |
560 | [[%left '+' | |
561 | %left '*' | |
562 | ||
563 | %% | |
564 | ||
22fccf95 | 565 | %no-default-prec; |
39a06c25 PE |
566 | |
567 | e: e '+' e | |
568 | | e '*' e | |
569 | | '0' | |
570 | ; | |
571 | ]]) | |
572 | ||
573 | AT_CHECK([bison -o input.c input.y], 0, [], | |
574 | [[input.y: conflicts: 4 shift/reduce | |
575 | ]]) | |
576 | AT_CLEANUP | |
577 | ||
578 | ||
22fccf95 PE |
579 | ## ---------------------------- ## |
580 | ## %no-default-prec with %prec ## | |
581 | ## ---------------------------- ## | |
39a06c25 | 582 | |
22fccf95 | 583 | AT_SETUP([%no-default-prec with %prec]) |
39a06c25 PE |
584 | |
585 | AT_DATA([[input.y]], | |
586 | [[%left '+' | |
587 | %left '*' | |
588 | ||
589 | %% | |
590 | ||
22fccf95 | 591 | %no-default-prec; |
39a06c25 PE |
592 | |
593 | e: e '+' e %prec '+' | |
594 | | e '*' e %prec '*' | |
595 | | '0' | |
596 | ; | |
597 | ]]) | |
598 | ||
599 | AT_CHECK([bison -o input.c input.y]) | |
600 | AT_CLEANUP | |
601 | ||
602 | ||
603 | ## ---------------- ## | |
22fccf95 | 604 | ## %default-prec ## |
39a06c25 PE |
605 | ## ---------------- ## |
606 | ||
22fccf95 | 607 | AT_SETUP([%default-prec]) |
39a06c25 PE |
608 | |
609 | AT_DATA([[input.y]], | |
610 | [[%left '+' | |
611 | %left '*' | |
612 | ||
613 | %% | |
614 | ||
22fccf95 | 615 | %default-prec; |
39a06c25 PE |
616 | |
617 | e: e '+' e | |
618 | | e '*' e | |
619 | | '0' | |
620 | ; | |
621 | ]]) | |
622 | ||
623 | AT_CHECK([bison -o input.c input.y]) | |
624 | AT_CLEANUP |