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