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