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