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