]>
Commit | Line | Data |
---|---|---|
3c31a486 | 1 | # Exercising Bison on conflicts. -*- Autotest -*- |
69363a9e | 2 | |
75ad86ee | 3 | # Copyright (C) 2002, 2003, 2004, 2005, 2007 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 | |
0fb669f9 PE |
17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 | # 02110-1301, USA. | |
3c31a486 AD |
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> | |
6e26ca8c | 58 | #include <stdlib.h> |
cf806753 | 59 | #include <string.h> |
1207eeac | 60 | |
3c31a486 | 61 | #define YYERROR_VERBOSE 1 |
1207eeac AD |
62 | static void |
63 | yyerror (const char *msg) | |
64 | { | |
65 | fprintf (stderr, "%s\n", msg); | |
1207eeac | 66 | } |
3c31a486 AD |
67 | |
68 | /* The current argument. */ | |
cf806753 | 69 | static const char *input; |
3c31a486 AD |
70 | |
71 | static int | |
72 | yylex (void) | |
73 | { | |
cf806753 PE |
74 | static size_t toknum; |
75 | if (! (toknum <= strlen (input))) | |
76 | abort (); | |
77 | return input[toknum++]; | |
3c31a486 AD |
78 | } |
79 | ||
80 | %} | |
81 | ||
82 | %nonassoc '<' '>' | |
83 | ||
84 | %% | |
85 | expr: expr '<' expr | |
86 | | expr '>' expr | |
87 | | '0' | |
88 | ; | |
89 | %% | |
90 | int | |
91 | main (int argc, const char *argv[]) | |
92 | { | |
9d774aff | 93 | input = argc <= 1 ? "" : argv[1]; |
3c31a486 AD |
94 | return yyparse (); |
95 | } | |
96 | ]]) | |
97 | ||
98 | # Specify the output files to avoid problems on different file systems. | |
b56471a6 | 99 | AT_CHECK([bison -o input.c input.y]) |
1154cced | 100 | AT_COMPILE([input]) |
3c31a486 | 101 | |
1154cced | 102 | AT_PARSER_CHECK([./input '0<0']) |
3c31a486 AD |
103 | # FIXME: This is an actual bug, but a new one, in the sense that |
104 | # no one has ever spotted it! The messages are *wrong*: there should | |
105 | # be nothing there, it should be expected eof. | |
1154cced | 106 | AT_PARSER_CHECK([./input '0<0<0'], [1], [], |
6e649e65 | 107 | [syntax error, unexpected '<', expecting '<' or '>' |
3c31a486 AD |
108 | ]) |
109 | ||
1154cced AD |
110 | AT_PARSER_CHECK([./input '0>0']) |
111 | AT_PARSER_CHECK([./input '0>0>0'], [1], [], | |
6e649e65 | 112 | [syntax error, unexpected '>', expecting '<' or '>' |
3c31a486 AD |
113 | ]) |
114 | ||
1154cced | 115 | AT_PARSER_CHECK([./input '0<0>0'], [1], [], |
6e649e65 | 116 | [syntax error, unexpected '>', expecting '<' or '>' |
3c31a486 AD |
117 | ]) |
118 | ||
119 | AT_CLEANUP | |
120 | ||
121 | ||
122 | ||
123 | ## ------------------------- ## | |
124 | ## Unresolved SR Conflicts. ## | |
125 | ## ------------------------- ## | |
126 | ||
127 | AT_SETUP([Unresolved SR Conflicts]) | |
128 | ||
6b98e4b5 AD |
129 | AT_KEYWORDS([report]) |
130 | ||
3c31a486 AD |
131 | AT_DATA([input.y], |
132 | [[%token NUM OP | |
133 | %% | |
134 | exp: exp OP exp | NUM; | |
135 | ]]) | |
136 | ||
b56471a6 | 137 | AT_CHECK([bison -o input.c --report=all input.y], 0, [], |
2c8ba4cd | 138 | [input.y: conflicts: 1 shift/reduce |
3c31a486 AD |
139 | ]) |
140 | ||
141 | # Check the contents of the report. | |
142 | AT_CHECK([cat input.output], [], | |
2c8ba4cd | 143 | [[State 5 conflicts: 1 shift/reduce |
3c31a486 AD |
144 | |
145 | ||
146 | Grammar | |
147 | ||
88bce5a2 | 148 | 0 $accept: exp $end |
6b98e4b5 AD |
149 | |
150 | 1 exp: exp OP exp | |
151 | 2 | NUM | |
3c31a486 AD |
152 | |
153 | ||
154 | Terminals, with rules where they appear | |
155 | ||
88bce5a2 | 156 | $end (0) 0 |
3c31a486 | 157 | error (256) |
007a50a4 AD |
158 | NUM (258) 2 |
159 | OP (259) 1 | |
3c31a486 AD |
160 | |
161 | ||
162 | Nonterminals, with rules where they appear | |
163 | ||
88bce5a2 | 164 | $accept (5) |
3c31a486 AD |
165 | on left: 0 |
166 | exp (6) | |
167 | on left: 1 2, on right: 0 1 | |
168 | ||
169 | ||
170 | state 0 | |
171 | ||
88bce5a2 | 172 | 0 $accept: . exp $end |
ce4ccb4b AD |
173 | 1 exp: . exp OP exp |
174 | 2 | . NUM | |
643a5994 | 175 | |
87675353 | 176 | NUM shift, and go to state 1 |
3c31a486 | 177 | |
87675353 | 178 | exp go to state 2 |
3c31a486 AD |
179 | |
180 | ||
181 | state 1 | |
182 | ||
ce4ccb4b | 183 | 2 exp: NUM . |
3c31a486 | 184 | |
87675353 | 185 | $default reduce using rule 2 (exp) |
3c31a486 AD |
186 | |
187 | ||
188 | state 2 | |
189 | ||
88bce5a2 | 190 | 0 $accept: exp . $end |
ce4ccb4b | 191 | 1 exp: exp . OP exp |
3c31a486 | 192 | |
88bce5a2 AD |
193 | $end shift, and go to state 3 |
194 | OP shift, and go to state 4 | |
3c31a486 AD |
195 | |
196 | ||
197 | state 3 | |
198 | ||
88bce5a2 | 199 | 0 $accept: exp $end . |
3c31a486 | 200 | |
e8832397 | 201 | $default accept |
3c31a486 AD |
202 | |
203 | ||
204 | state 4 | |
205 | ||
ce4ccb4b AD |
206 | 1 exp: . exp OP exp |
207 | 1 | exp OP . exp | |
208 | 2 | . NUM | |
3c31a486 | 209 | |
87675353 | 210 | NUM shift, and go to state 1 |
3c31a486 | 211 | |
87675353 | 212 | exp go to state 5 |
3c31a486 AD |
213 | |
214 | ||
215 | state 5 | |
216 | ||
88bce5a2 AD |
217 | 1 exp: exp . OP exp [$end, OP] |
218 | 1 | exp OP exp . [$end, OP] | |
3c31a486 | 219 | |
87675353 | 220 | OP shift, and go to state 4 |
3c31a486 | 221 | |
87675353 AD |
222 | OP [reduce using rule 1 (exp)] |
223 | $default reduce using rule 1 (exp) | |
3c31a486 AD |
224 | ]]) |
225 | ||
226 | AT_CLEANUP | |
227 | ||
228 | ||
3c31a486 | 229 | |
ce4ccb4b AD |
230 | ## ----------------------- ## |
231 | ## Resolved SR Conflicts. ## | |
232 | ## ----------------------- ## | |
233 | ||
234 | AT_SETUP([Resolved SR Conflicts]) | |
3c31a486 | 235 | |
6b98e4b5 AD |
236 | AT_KEYWORDS([report]) |
237 | ||
3c31a486 AD |
238 | AT_DATA([input.y], |
239 | [[%token NUM OP | |
ce4ccb4b | 240 | %left OP |
3c31a486 AD |
241 | %% |
242 | exp: exp OP exp | NUM; | |
243 | ]]) | |
244 | ||
b56471a6 | 245 | AT_CHECK([bison -o input.c --report=all input.y]) |
3c31a486 AD |
246 | |
247 | # Check the contents of the report. | |
248 | AT_CHECK([cat input.output], [], | |
ce4ccb4b | 249 | [[Grammar |
3c31a486 | 250 | |
88bce5a2 | 251 | 0 $accept: exp $end |
6b98e4b5 AD |
252 | |
253 | 1 exp: exp OP exp | |
254 | 2 | NUM | |
3c31a486 AD |
255 | |
256 | ||
257 | Terminals, with rules where they appear | |
258 | ||
88bce5a2 | 259 | $end (0) 0 |
3c31a486 | 260 | error (256) |
007a50a4 AD |
261 | NUM (258) 2 |
262 | OP (259) 1 | |
3c31a486 AD |
263 | |
264 | ||
265 | Nonterminals, with rules where they appear | |
266 | ||
88bce5a2 | 267 | $accept (5) |
3c31a486 AD |
268 | on left: 0 |
269 | exp (6) | |
270 | on left: 1 2, on right: 0 1 | |
271 | ||
272 | ||
273 | state 0 | |
274 | ||
88bce5a2 | 275 | 0 $accept: . exp $end |
ce4ccb4b AD |
276 | 1 exp: . exp OP exp |
277 | 2 | . NUM | |
643a5994 | 278 | |
87675353 | 279 | NUM shift, and go to state 1 |
3c31a486 | 280 | |
87675353 | 281 | exp go to state 2 |
3c31a486 AD |
282 | |
283 | ||
284 | state 1 | |
285 | ||
ce4ccb4b | 286 | 2 exp: NUM . |
3c31a486 | 287 | |
87675353 | 288 | $default reduce using rule 2 (exp) |
3c31a486 AD |
289 | |
290 | ||
291 | state 2 | |
292 | ||
88bce5a2 | 293 | 0 $accept: exp . $end |
ce4ccb4b | 294 | 1 exp: exp . OP exp |
3c31a486 | 295 | |
88bce5a2 AD |
296 | $end shift, and go to state 3 |
297 | OP shift, and go to state 4 | |
3c31a486 AD |
298 | |
299 | ||
300 | state 3 | |
301 | ||
88bce5a2 | 302 | 0 $accept: exp $end . |
3c31a486 | 303 | |
e8832397 | 304 | $default accept |
3c31a486 AD |
305 | |
306 | ||
307 | state 4 | |
308 | ||
ce4ccb4b AD |
309 | 1 exp: . exp OP exp |
310 | 1 | exp OP . exp | |
311 | 2 | . NUM | |
3c31a486 | 312 | |
87675353 | 313 | NUM shift, and go to state 1 |
3c31a486 | 314 | |
87675353 | 315 | exp go to state 5 |
3c31a486 AD |
316 | |
317 | ||
318 | state 5 | |
319 | ||
88bce5a2 AD |
320 | 1 exp: exp . OP exp [$end, OP] |
321 | 1 | exp OP exp . [$end, OP] | |
3c31a486 | 322 | |
87675353 | 323 | $default reduce using rule 1 (exp) |
7ea9a33f | 324 | |
4b3d3a8e | 325 | Conflict between rule 1 and token OP resolved as reduce (%left OP). |
bc933ef1 AD |
326 | ]]) |
327 | ||
328 | AT_CLEANUP | |
329 | ||
330 | ||
bc933ef1 AD |
331 | ## -------------------------------- ## |
332 | ## Defaulted Conflicted Reduction. ## | |
333 | ## -------------------------------- ## | |
334 | ||
335 | # When there are RR conflicts, some rules are disabled. Usually it is | |
336 | # simply displayed as: | |
337 | # | |
88bce5a2 AD |
338 | # $end reduce using rule 3 (num) |
339 | # $end [reduce using rule 4 (id)] | |
bc933ef1 AD |
340 | # |
341 | # But when `reduce 3' is the default action, we'd produce: | |
342 | # | |
88bce5a2 | 343 | # $end [reduce using rule 4 (id)] |
bc933ef1 AD |
344 | # $default reduce using rule 3 (num) |
345 | # | |
346 | # In this precise case (a reduction is masked by the default | |
347 | # reduction), we make the `reduce 3' explicit: | |
348 | # | |
88bce5a2 AD |
349 | # $end reduce using rule 3 (num) |
350 | # $end [reduce using rule 4 (id)] | |
bc933ef1 AD |
351 | # $default reduce using rule 3 (num) |
352 | # | |
353 | # Maybe that's not the best display, but then, please propose something | |
354 | # else. | |
355 | ||
356 | AT_SETUP([Defaulted Conflicted Reduction]) | |
357 | AT_KEYWORDS([report]) | |
358 | ||
359 | AT_DATA([input.y], | |
360 | [[%% | |
361 | exp: num | id; | |
362 | num: '0'; | |
363 | id : '0'; | |
364 | %% | |
365 | ]]) | |
366 | ||
b56471a6 | 367 | AT_CHECK([bison -o input.c --report=all input.y], 0, [], |
2c8ba4cd | 368 | [[input.y: conflicts: 1 reduce/reduce |
2bf21a83 | 369 | input.y:4.6-8: warning: rule never reduced because of conflicts: id: '0' |
e8832397 | 370 | ]]) |
bc933ef1 AD |
371 | |
372 | # Check the contents of the report. | |
373 | AT_CHECK([cat input.output], [], | |
c8f002c7 AD |
374 | [[Rules never reduced |
375 | ||
376 | 4 id: '0' | |
377 | ||
378 | ||
2c8ba4cd | 379 | State 1 conflicts: 1 reduce/reduce |
bc933ef1 AD |
380 | |
381 | ||
382 | Grammar | |
383 | ||
88bce5a2 | 384 | 0 $accept: exp $end |
bc933ef1 AD |
385 | |
386 | 1 exp: num | |
387 | 2 | id | |
388 | ||
389 | 3 num: '0' | |
390 | ||
391 | 4 id: '0' | |
392 | ||
393 | ||
394 | Terminals, with rules where they appear | |
395 | ||
88bce5a2 | 396 | $end (0) 0 |
bc933ef1 AD |
397 | '0' (48) 3 4 |
398 | error (256) | |
399 | ||
400 | ||
401 | Nonterminals, with rules where they appear | |
402 | ||
88bce5a2 | 403 | $accept (4) |
bc933ef1 AD |
404 | on left: 0 |
405 | exp (5) | |
406 | on left: 1 2, on right: 0 | |
407 | num (6) | |
408 | on left: 3, on right: 1 | |
409 | id (7) | |
410 | on left: 4, on right: 2 | |
411 | ||
412 | ||
413 | state 0 | |
414 | ||
88bce5a2 | 415 | 0 $accept: . exp $end |
ce4ccb4b AD |
416 | 1 exp: . num |
417 | 2 | . id | |
418 | 3 num: . '0' | |
419 | 4 id: . '0' | |
bc933ef1 | 420 | |
87675353 | 421 | '0' shift, and go to state 1 |
bc933ef1 | 422 | |
87675353 AD |
423 | exp go to state 2 |
424 | num go to state 3 | |
425 | id go to state 4 | |
bc933ef1 AD |
426 | |
427 | ||
428 | state 1 | |
429 | ||
88bce5a2 AD |
430 | 3 num: '0' . [$end] |
431 | 4 id: '0' . [$end] | |
bc933ef1 | 432 | |
88bce5a2 AD |
433 | $end reduce using rule 3 (num) |
434 | $end [reduce using rule 4 (id)] | |
87675353 | 435 | $default reduce using rule 3 (num) |
bc933ef1 AD |
436 | |
437 | ||
438 | state 2 | |
439 | ||
88bce5a2 | 440 | 0 $accept: exp . $end |
bc933ef1 | 441 | |
88bce5a2 | 442 | $end shift, and go to state 5 |
bc933ef1 AD |
443 | |
444 | ||
445 | state 3 | |
446 | ||
ce4ccb4b | 447 | 1 exp: num . |
bc933ef1 | 448 | |
87675353 | 449 | $default reduce using rule 1 (exp) |
bc933ef1 AD |
450 | |
451 | ||
452 | state 4 | |
453 | ||
ce4ccb4b | 454 | 2 exp: id . |
bc933ef1 | 455 | |
87675353 | 456 | $default reduce using rule 2 (exp) |
bc933ef1 AD |
457 | |
458 | ||
459 | state 5 | |
460 | ||
88bce5a2 | 461 | 0 $accept: exp $end . |
bc933ef1 | 462 | |
e8832397 | 463 | $default accept |
3c31a486 AD |
464 | ]]) |
465 | ||
466 | AT_CLEANUP | |
467 | ||
468 | ||
469 | ||
470 | ||
471 | ## -------------------- ## | |
472 | ## %expect not enough. ## | |
473 | ## -------------------- ## | |
474 | ||
475 | AT_SETUP([%expect not enough]) | |
476 | ||
477 | AT_DATA([input.y], | |
478 | [[%token NUM OP | |
479 | %expect 0 | |
480 | %% | |
481 | exp: exp OP exp | NUM; | |
482 | ]]) | |
483 | ||
035aa4a0 | 484 | AT_CHECK([bison -o input.c input.y], 1, [], |
2c8ba4cd | 485 | [input.y: conflicts: 1 shift/reduce |
035aa4a0 | 486 | input.y: expected 0 shift/reduce conflicts |
3c31a486 AD |
487 | ]) |
488 | AT_CLEANUP | |
489 | ||
490 | ||
491 | ## --------------- ## | |
492 | ## %expect right. ## | |
493 | ## --------------- ## | |
494 | ||
495 | AT_SETUP([%expect right]) | |
496 | ||
497 | AT_DATA([input.y], | |
498 | [[%token NUM OP | |
499 | %expect 1 | |
500 | %% | |
501 | exp: exp OP exp | NUM; | |
502 | ]]) | |
503 | ||
b56471a6 | 504 | AT_CHECK([bison -o input.c input.y]) |
3c31a486 AD |
505 | AT_CLEANUP |
506 | ||
507 | ||
508 | ## ------------------ ## | |
509 | ## %expect too much. ## | |
510 | ## ------------------ ## | |
511 | ||
512 | AT_SETUP([%expect too much]) | |
513 | ||
514 | AT_DATA([input.y], | |
515 | [[%token NUM OP | |
516 | %expect 2 | |
517 | %% | |
518 | exp: exp OP exp | NUM; | |
519 | ]]) | |
520 | ||
035aa4a0 | 521 | AT_CHECK([bison -o input.c input.y], 1, [], |
2c8ba4cd | 522 | [input.y: conflicts: 1 shift/reduce |
035aa4a0 | 523 | input.y: expected 2 shift/reduce conflicts |
3c31a486 AD |
524 | ]) |
525 | AT_CLEANUP | |
6876ecd3 PE |
526 | |
527 | ||
528 | ## ------------------------------ ## | |
529 | ## %expect with reduce conflicts ## | |
530 | ## ------------------------------ ## | |
531 | ||
532 | AT_SETUP([%expect with reduce conflicts]) | |
533 | ||
534 | AT_DATA([input.y], | |
535 | [[%expect 0 | |
536 | %% | |
537 | program: a 'a' | a a; | |
538 | a: 'a'; | |
539 | ]]) | |
540 | ||
035aa4a0 | 541 | AT_CHECK([bison -o input.c input.y], 1, [], |
2c8ba4cd | 542 | [input.y: conflicts: 1 reduce/reduce |
035aa4a0 | 543 | input.y: expected 0 reduce/reduce conflicts |
6876ecd3 PE |
544 | ]) |
545 | AT_CLEANUP | |
39a06c25 PE |
546 | |
547 | ||
22fccf95 PE |
548 | ## ------------------------------- ## |
549 | ## %no-default-prec without %prec ## | |
550 | ## ------------------------------- ## | |
39a06c25 | 551 | |
22fccf95 | 552 | AT_SETUP([%no-default-prec without %prec]) |
39a06c25 PE |
553 | |
554 | AT_DATA([[input.y]], | |
555 | [[%left '+' | |
556 | %left '*' | |
557 | ||
558 | %% | |
559 | ||
22fccf95 | 560 | %no-default-prec; |
39a06c25 PE |
561 | |
562 | e: e '+' e | |
563 | | e '*' e | |
564 | | '0' | |
565 | ; | |
566 | ]]) | |
567 | ||
568 | AT_CHECK([bison -o input.c input.y], 0, [], | |
569 | [[input.y: conflicts: 4 shift/reduce | |
570 | ]]) | |
571 | AT_CLEANUP | |
572 | ||
573 | ||
22fccf95 PE |
574 | ## ---------------------------- ## |
575 | ## %no-default-prec with %prec ## | |
576 | ## ---------------------------- ## | |
39a06c25 | 577 | |
22fccf95 | 578 | AT_SETUP([%no-default-prec with %prec]) |
39a06c25 PE |
579 | |
580 | AT_DATA([[input.y]], | |
581 | [[%left '+' | |
582 | %left '*' | |
583 | ||
584 | %% | |
585 | ||
22fccf95 | 586 | %no-default-prec; |
39a06c25 PE |
587 | |
588 | e: e '+' e %prec '+' | |
589 | | e '*' e %prec '*' | |
590 | | '0' | |
591 | ; | |
592 | ]]) | |
593 | ||
594 | AT_CHECK([bison -o input.c input.y]) | |
595 | AT_CLEANUP | |
596 | ||
597 | ||
598 | ## ---------------- ## | |
22fccf95 | 599 | ## %default-prec ## |
39a06c25 PE |
600 | ## ---------------- ## |
601 | ||
22fccf95 | 602 | AT_SETUP([%default-prec]) |
39a06c25 PE |
603 | |
604 | AT_DATA([[input.y]], | |
605 | [[%left '+' | |
606 | %left '*' | |
607 | ||
608 | %% | |
609 | ||
22fccf95 | 610 | %default-prec; |
39a06c25 PE |
611 | |
612 | e: e '+' e | |
613 | | e '*' e | |
614 | | '0' | |
615 | ; | |
616 | ]]) | |
617 | ||
618 | AT_CHECK([bison -o input.c input.y]) | |
619 | AT_CLEANUP | |
5967f0cf JD |
620 | |
621 | ||
622 | ## ---------------------------------------------- ## | |
623 | ## Unreachable States After Conflict Resolution. ## | |
624 | ## ---------------------------------------------- ## | |
625 | ||
626 | AT_SETUP([[Unreachable States After Conflict Resolution]]) | |
627 | ||
628 | # If conflict resolution makes states unreachable, remove those states, report | |
629 | # rules that are then unused, and don't report conflicts in those states. Test | |
630 | # what happens when a nonterminal becomes useless as a result of state removal | |
631 | # since that causes lalr.o's goto map to be rewritten. | |
632 | ||
633 | AT_DATA([[input.y]], | |
634 | [[%output "input.c" | |
635 | %left 'a' | |
636 | ||
637 | %% | |
638 | ||
639 | start: resolved_conflict 'a' reported_conflicts 'a' ; | |
640 | ||
641 | /* S/R conflict resolved as shift, so the state with item | |
642 | * (resolved_conflict: 'a' . unreachable1) and all it transition successors are | |
643 | * unreachable, and the associated production is useless. */ | |
644 | resolved_conflict: | |
645 | 'a' unreachable1 | |
646 | | %prec 'a' | |
647 | ; | |
648 | ||
649 | /* S/R conflict that need not be reported since it is unreachable because of | |
650 | * the previous conflict resolution. Nonterminal unreachable1 and all its | |
651 | * productions are useless. */ | |
652 | unreachable1: | |
653 | 'a' unreachable2 | |
654 | | | |
655 | ; | |
656 | ||
657 | /* Likewise for a R/R conflict and nonterminal unreachable2. */ | |
658 | unreachable2: | ; | |
659 | ||
660 | /* Make sure remaining S/R and R/R conflicts are still reported correctly even | |
661 | * when their states are renumbered due to state removal. */ | |
662 | reported_conflicts: | |
663 | 'a' | |
664 | | 'a' | |
665 | | | |
666 | ; | |
667 | ||
668 | ]]) | |
669 | ||
670 | AT_CHECK([[bison --report=all input.y]], 0, [], | |
671 | [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce | |
672 | input.y:12.5-20: warning: rule never reduced because of conflicts: resolved_conflict: 'a' unreachable1 | |
673 | input.y:20.5-20: warning: rule never reduced because of conflicts: unreachable1: 'a' unreachable2 | |
674 | input.y:21.4: warning: rule never reduced because of conflicts: unreachable1: /* empty */ | |
675 | input.y:25.13: warning: rule never reduced because of conflicts: unreachable2: /* empty */ | |
676 | input.y:25.16: warning: rule never reduced because of conflicts: unreachable2: /* empty */ | |
677 | input.y:31.5-7: warning: rule never reduced because of conflicts: reported_conflicts: 'a' | |
678 | input.y:32.4: warning: rule never reduced because of conflicts: reported_conflicts: /* empty */ | |
679 | ]]) | |
680 | ||
681 | AT_CHECK([[cat input.output]], 0, | |
682 | [[Rules never reduced | |
683 | ||
684 | 2 resolved_conflict: 'a' unreachable1 | |
685 | ||
686 | 4 unreachable1: 'a' unreachable2 | |
687 | 5 | /* empty */ | |
688 | ||
689 | 6 unreachable2: /* empty */ | |
690 | 7 | /* empty */ | |
691 | ||
692 | 9 reported_conflicts: 'a' | |
693 | 10 | /* empty */ | |
694 | ||
695 | ||
696 | State 4 conflicts: 1 shift/reduce | |
697 | State 5 conflicts: 1 reduce/reduce | |
698 | ||
699 | ||
700 | Grammar | |
701 | ||
702 | 0 $accept: start $end | |
703 | ||
704 | 1 start: resolved_conflict 'a' reported_conflicts 'a' | |
705 | ||
706 | 2 resolved_conflict: 'a' unreachable1 | |
707 | 3 | /* empty */ | |
708 | ||
709 | 4 unreachable1: 'a' unreachable2 | |
710 | 5 | /* empty */ | |
711 | ||
712 | 6 unreachable2: /* empty */ | |
713 | 7 | /* empty */ | |
714 | ||
715 | 8 reported_conflicts: 'a' | |
716 | 9 | 'a' | |
717 | 10 | /* empty */ | |
718 | ||
719 | ||
720 | Terminals, with rules where they appear | |
721 | ||
722 | $end (0) 0 | |
723 | 'a' (97) 1 2 4 8 9 | |
724 | error (256) | |
725 | ||
726 | ||
727 | Nonterminals, with rules where they appear | |
728 | ||
729 | $accept (4) | |
730 | on left: 0 | |
731 | start (5) | |
732 | on left: 1, on right: 0 | |
733 | resolved_conflict (6) | |
734 | on left: 2 3, on right: 1 | |
735 | unreachable1 (7) | |
736 | on left: 4 5, on right: 2 | |
737 | unreachable2 (8) | |
738 | on left: 6 7, on right: 4 | |
739 | reported_conflicts (9) | |
740 | on left: 8 9 10, on right: 1 | |
741 | ||
742 | ||
743 | state 0 | |
744 | ||
745 | 0 $accept: . start $end | |
746 | 1 start: . resolved_conflict 'a' reported_conflicts 'a' | |
747 | 2 resolved_conflict: . 'a' unreachable1 | |
748 | 3 | . ['a'] | |
749 | ||
750 | $default reduce using rule 3 (resolved_conflict) | |
751 | ||
752 | start go to state 1 | |
753 | resolved_conflict go to state 2 | |
754 | ||
755 | Conflict between rule 3 and token 'a' resolved as reduce (%left 'a'). | |
756 | ||
757 | ||
758 | state 1 | |
759 | ||
760 | 0 $accept: start . $end | |
761 | ||
762 | $end shift, and go to state 3 | |
763 | ||
764 | ||
765 | state 2 | |
766 | ||
767 | 1 start: resolved_conflict . 'a' reported_conflicts 'a' | |
768 | ||
769 | 'a' shift, and go to state 4 | |
770 | ||
771 | ||
772 | state 3 | |
773 | ||
774 | 0 $accept: start $end . | |
775 | ||
776 | $default accept | |
777 | ||
778 | ||
779 | state 4 | |
780 | ||
781 | 1 start: resolved_conflict 'a' . reported_conflicts 'a' | |
782 | 8 reported_conflicts: . 'a' | |
783 | 9 | . 'a' | |
784 | 10 | . ['a'] | |
785 | ||
786 | 'a' shift, and go to state 5 | |
787 | ||
788 | 'a' [reduce using rule 10 (reported_conflicts)] | |
789 | ||
790 | reported_conflicts go to state 6 | |
791 | ||
792 | ||
793 | state 5 | |
794 | ||
795 | 8 reported_conflicts: 'a' . ['a'] | |
796 | 9 | 'a' . ['a'] | |
797 | ||
798 | 'a' reduce using rule 8 (reported_conflicts) | |
799 | 'a' [reduce using rule 9 (reported_conflicts)] | |
800 | $default reduce using rule 8 (reported_conflicts) | |
801 | ||
802 | ||
803 | state 6 | |
804 | ||
805 | 1 start: resolved_conflict 'a' reported_conflicts . 'a' | |
806 | ||
807 | 'a' shift, and go to state 7 | |
808 | ||
809 | ||
810 | state 7 | |
811 | ||
812 | 1 start: resolved_conflict 'a' reported_conflicts 'a' . | |
9d774aff | 813 | |
5967f0cf JD |
814 | $default reduce using rule 1 (start) |
815 | ]]) | |
816 | ||
817 | AT_CLEANUP | |
9d774aff JD |
818 | |
819 | ||
820 | ## ------------------------------------------------------------ ## | |
821 | ## Solved conflicts report for multiple reductions in a state. ## | |
822 | ## ------------------------------------------------------------ ## | |
823 | ||
824 | AT_SETUP([[Solved conflicts report for multiple reductions in a state]]) | |
825 | ||
826 | # Used to lose earlier solved conflict messages even within a single S/R/R. | |
827 | ||
828 | AT_DATA([[input.y]], | |
829 | [[%left 'a' | |
830 | %right 'b' | |
831 | %right 'c' | |
832 | %right 'd' | |
833 | %% | |
834 | start: | |
835 | 'a' | |
836 | | empty_a 'a' | |
837 | | 'b' | |
838 | | empty_b 'b' | |
839 | | 'c' | |
840 | | empty_c1 'c' | |
841 | | empty_c2 'c' | |
842 | | empty_c3 'c' | |
843 | ; | |
844 | empty_a: %prec 'a' ; | |
845 | empty_b: %prec 'b' ; | |
846 | empty_c1: %prec 'c' ; | |
847 | empty_c2: %prec 'c' ; | |
848 | empty_c3: %prec 'd' ; | |
849 | ]]) | |
850 | AT_CHECK([[bison --report=all -o input.c input.y]], 0, [], [ignore]) | |
851 | AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0, | |
852 | [[state 0 | |
853 | ||
854 | 0 $accept: . start $end | |
855 | 1 start: . 'a' | |
856 | 2 | . empty_a 'a' | |
857 | 3 | . 'b' | |
858 | 4 | . empty_b 'b' | |
859 | 5 | . 'c' | |
860 | 6 | . empty_c1 'c' | |
861 | 7 | . empty_c2 'c' | |
862 | 8 | . empty_c3 'c' | |
863 | 9 empty_a: . ['a'] | |
864 | 10 empty_b: . [] | |
865 | 11 empty_c1: . [] | |
866 | 12 empty_c2: . [] | |
867 | 13 empty_c3: . ['c'] | |
868 | ||
869 | 'b' shift, and go to state 1 | |
870 | ||
871 | 'c' reduce using rule 13 (empty_c3) | |
872 | $default reduce using rule 9 (empty_a) | |
873 | ||
874 | start go to state 2 | |
875 | empty_a go to state 3 | |
876 | empty_b go to state 4 | |
877 | empty_c1 go to state 5 | |
878 | empty_c2 go to state 6 | |
879 | empty_c3 go to state 7 | |
880 | ||
881 | Conflict between rule 9 and token 'a' resolved as reduce (%left 'a'). | |
882 | Conflict between rule 10 and token 'b' resolved as shift (%right 'b'). | |
883 | Conflict between rule 11 and token 'c' resolved as shift (%right 'c'). | |
884 | Conflict between rule 12 and token 'c' resolved as shift (%right 'c'). | |
885 | Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd'). | |
886 | ||
887 | ||
888 | state 1 | |
889 | ]]) | |
890 | ||
891 | AT_CLEANUP | |
892 | ||
893 | ||
894 | ## ------------------------------------------------------------ ## | |
895 | ## %nonassoc error actions for multiple reductions in a state. ## | |
896 | ## ------------------------------------------------------------ ## | |
897 | ||
898 | # Used to abort when trying to resolve conflicts as %nonassoc error actions for | |
899 | # multiple reductions in a state. | |
900 | ||
901 | # For a %nonassoc error action token, used to print the first remaining | |
902 | # reduction on that token without brackets. | |
903 | ||
904 | AT_SETUP([[%nonassoc error actions for multiple reductions in a state]]) | |
905 | ||
906 | AT_DATA([[input.y]], | |
907 | [[%nonassoc 'a' 'b' 'c' | |
908 | %% | |
909 | start: | |
910 | 'a' | |
911 | | empty_a 'a' | |
912 | | 'b' | |
913 | | empty_b 'b' | |
914 | | 'c' | |
915 | | empty_c1 'c' | |
916 | | empty_c2 'c' | |
917 | | empty_c3 'c' | |
918 | ; | |
919 | empty_a: %prec 'a' ; | |
920 | empty_b: %prec 'b' ; | |
921 | empty_c1: %prec 'c' ; | |
922 | empty_c2: %prec 'c' ; | |
923 | empty_c3: %prec 'c' ; | |
924 | ]]) | |
925 | ||
926 | AT_CHECK([[bison --report=all -o input.c input.y]], 0, [], [ignore]) | |
927 | AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0, | |
928 | [[state 0 | |
929 | ||
930 | 0 $accept: . start $end | |
931 | 1 start: . 'a' | |
932 | 2 | . empty_a 'a' | |
933 | 3 | . 'b' | |
934 | 4 | . empty_b 'b' | |
935 | 5 | . 'c' | |
936 | 6 | . empty_c1 'c' | |
937 | 7 | . empty_c2 'c' | |
938 | 8 | . empty_c3 'c' | |
939 | 9 empty_a: . [] | |
940 | 10 empty_b: . [] | |
941 | 11 empty_c1: . [] | |
942 | 12 empty_c2: . ['c'] | |
943 | 13 empty_c3: . ['c'] | |
944 | ||
945 | 'a' error (nonassociative) | |
946 | 'b' error (nonassociative) | |
947 | 'c' error (nonassociative) | |
948 | ||
949 | 'c' [reduce using rule 12 (empty_c2)] | |
950 | 'c' [reduce using rule 13 (empty_c3)] | |
951 | ||
952 | start go to state 1 | |
953 | empty_a go to state 2 | |
954 | empty_b go to state 3 | |
955 | empty_c1 go to state 4 | |
956 | empty_c2 go to state 5 | |
957 | empty_c3 go to state 6 | |
958 | ||
959 | Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a'). | |
960 | Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b'). | |
961 | Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c'). | |
962 | ||
963 | ||
964 | state 1 | |
965 | ]]) | |
966 | AT_CLEANUP |