]>
Commit | Line | Data |
---|---|---|
3c31a486 | 1 | # Exercising Bison on conflicts. -*- Autotest -*- |
643a5994 | 2 | # Copyright (C) 2002 Free Software Foundation, Inc. |
3c31a486 AD |
3 | |
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 2, or (at your option) | |
7 | # any later version. | |
8 | ||
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. | |
13 | ||
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
17 | # 02111-1307, USA. | |
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 | ||
b56471a6 | 40 | AT_CHECK([bison -o input.c input.y], 0, [], |
2bf21a83 | 41 | [[input.y:4.9: warning: rule never reduced because of 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> | |
1207eeac AD |
57 | |
58 | #if STDC_HEADERS | |
59 | # include <stdlib.h> | |
60 | #endif | |
61 | ||
3c31a486 | 62 | #define YYERROR_VERBOSE 1 |
1207eeac AD |
63 | static void |
64 | yyerror (const char *msg) | |
65 | { | |
66 | fprintf (stderr, "%s\n", msg); | |
67 | exit (1); | |
68 | } | |
3c31a486 AD |
69 | |
70 | /* The current argument. */ | |
71 | static const char *input = NULL; | |
72 | ||
73 | static int | |
74 | yylex (void) | |
75 | { | |
76 | /* No token stands for end of file. */ | |
77 | if (input && *input) | |
78 | return *input++; | |
79 | else | |
80 | return 0; | |
81 | } | |
82 | ||
83 | %} | |
84 | ||
85 | %nonassoc '<' '>' | |
86 | ||
87 | %% | |
88 | expr: expr '<' expr | |
89 | | expr '>' expr | |
90 | | '0' | |
91 | ; | |
92 | %% | |
93 | int | |
94 | main (int argc, const char *argv[]) | |
95 | { | |
96 | if (argc > 1) | |
97 | input = argv[1]; | |
98 | return yyparse (); | |
99 | } | |
100 | ]]) | |
101 | ||
102 | # Specify the output files to avoid problems on different file systems. | |
b56471a6 | 103 | AT_CHECK([bison -o input.c input.y]) |
1154cced | 104 | AT_COMPILE([input]) |
3c31a486 | 105 | |
1154cced | 106 | AT_PARSER_CHECK([./input '0<0']) |
3c31a486 AD |
107 | # FIXME: This is an actual bug, but a new one, in the sense that |
108 | # no one has ever spotted it! The messages are *wrong*: there should | |
109 | # be nothing there, it should be expected eof. | |
1154cced | 110 | AT_PARSER_CHECK([./input '0<0<0'], [1], [], |
6e649e65 | 111 | [syntax error, unexpected '<', expecting '<' or '>' |
3c31a486 AD |
112 | ]) |
113 | ||
1154cced AD |
114 | AT_PARSER_CHECK([./input '0>0']) |
115 | AT_PARSER_CHECK([./input '0>0>0'], [1], [], | |
6e649e65 | 116 | [syntax error, unexpected '>', expecting '<' or '>' |
3c31a486 AD |
117 | ]) |
118 | ||
1154cced | 119 | AT_PARSER_CHECK([./input '0<0>0'], [1], [], |
6e649e65 | 120 | [syntax error, unexpected '>', expecting '<' or '>' |
3c31a486 AD |
121 | ]) |
122 | ||
123 | AT_CLEANUP | |
124 | ||
125 | ||
126 | ||
127 | ## ------------------------- ## | |
128 | ## Unresolved SR Conflicts. ## | |
129 | ## ------------------------- ## | |
130 | ||
131 | AT_SETUP([Unresolved SR Conflicts]) | |
132 | ||
6b98e4b5 AD |
133 | AT_KEYWORDS([report]) |
134 | ||
3c31a486 AD |
135 | AT_DATA([input.y], |
136 | [[%token NUM OP | |
137 | %% | |
138 | exp: exp OP exp | NUM; | |
139 | ]]) | |
140 | ||
b56471a6 | 141 | AT_CHECK([bison -o input.c --report=all input.y], 0, [], |
2c8ba4cd | 142 | [input.y: conflicts: 1 shift/reduce |
3c31a486 AD |
143 | ]) |
144 | ||
145 | # Check the contents of the report. | |
146 | AT_CHECK([cat input.output], [], | |
2c8ba4cd | 147 | [[State 5 conflicts: 1 shift/reduce |
3c31a486 AD |
148 | |
149 | ||
150 | Grammar | |
151 | ||
88bce5a2 | 152 | 0 $accept: exp $end |
6b98e4b5 AD |
153 | |
154 | 1 exp: exp OP exp | |
155 | 2 | NUM | |
3c31a486 AD |
156 | |
157 | ||
158 | Terminals, with rules where they appear | |
159 | ||
88bce5a2 | 160 | $end (0) 0 |
3c31a486 | 161 | error (256) |
007a50a4 AD |
162 | NUM (258) 2 |
163 | OP (259) 1 | |
3c31a486 AD |
164 | |
165 | ||
166 | Nonterminals, with rules where they appear | |
167 | ||
88bce5a2 | 168 | $accept (5) |
3c31a486 AD |
169 | on left: 0 |
170 | exp (6) | |
171 | on left: 1 2, on right: 0 1 | |
172 | ||
173 | ||
174 | state 0 | |
175 | ||
88bce5a2 | 176 | 0 $accept: . exp $end |
ce4ccb4b AD |
177 | 1 exp: . exp OP exp |
178 | 2 | . NUM | |
643a5994 | 179 | |
87675353 | 180 | NUM shift, and go to state 1 |
3c31a486 | 181 | |
87675353 | 182 | exp go to state 2 |
3c31a486 AD |
183 | |
184 | ||
185 | state 1 | |
186 | ||
ce4ccb4b | 187 | 2 exp: NUM . |
3c31a486 | 188 | |
87675353 | 189 | $default reduce using rule 2 (exp) |
3c31a486 AD |
190 | |
191 | ||
192 | state 2 | |
193 | ||
88bce5a2 | 194 | 0 $accept: exp . $end |
ce4ccb4b | 195 | 1 exp: exp . OP exp |
3c31a486 | 196 | |
88bce5a2 AD |
197 | $end shift, and go to state 3 |
198 | OP shift, and go to state 4 | |
3c31a486 AD |
199 | |
200 | ||
201 | state 3 | |
202 | ||
88bce5a2 | 203 | 0 $accept: exp $end . |
3c31a486 | 204 | |
e8832397 | 205 | $default accept |
3c31a486 AD |
206 | |
207 | ||
208 | state 4 | |
209 | ||
ce4ccb4b AD |
210 | 1 exp: . exp OP exp |
211 | 1 | exp OP . exp | |
212 | 2 | . NUM | |
3c31a486 | 213 | |
87675353 | 214 | NUM shift, and go to state 1 |
3c31a486 | 215 | |
87675353 | 216 | exp go to state 5 |
3c31a486 AD |
217 | |
218 | ||
219 | state 5 | |
220 | ||
88bce5a2 AD |
221 | 1 exp: exp . OP exp [$end, OP] |
222 | 1 | exp OP exp . [$end, OP] | |
3c31a486 | 223 | |
87675353 | 224 | OP shift, and go to state 4 |
3c31a486 | 225 | |
87675353 AD |
226 | OP [reduce using rule 1 (exp)] |
227 | $default reduce using rule 1 (exp) | |
3c31a486 AD |
228 | ]]) |
229 | ||
230 | AT_CLEANUP | |
231 | ||
232 | ||
3c31a486 | 233 | |
ce4ccb4b AD |
234 | ## ----------------------- ## |
235 | ## Resolved SR Conflicts. ## | |
236 | ## ----------------------- ## | |
237 | ||
238 | AT_SETUP([Resolved SR Conflicts]) | |
3c31a486 | 239 | |
6b98e4b5 AD |
240 | AT_KEYWORDS([report]) |
241 | ||
3c31a486 AD |
242 | AT_DATA([input.y], |
243 | [[%token NUM OP | |
ce4ccb4b | 244 | %left OP |
3c31a486 AD |
245 | %% |
246 | exp: exp OP exp | NUM; | |
247 | ]]) | |
248 | ||
b56471a6 | 249 | AT_CHECK([bison -o input.c --report=all input.y]) |
3c31a486 AD |
250 | |
251 | # Check the contents of the report. | |
252 | AT_CHECK([cat input.output], [], | |
ce4ccb4b | 253 | [[Grammar |
3c31a486 | 254 | |
88bce5a2 | 255 | 0 $accept: exp $end |
6b98e4b5 AD |
256 | |
257 | 1 exp: exp OP exp | |
258 | 2 | NUM | |
3c31a486 AD |
259 | |
260 | ||
261 | Terminals, with rules where they appear | |
262 | ||
88bce5a2 | 263 | $end (0) 0 |
3c31a486 | 264 | error (256) |
007a50a4 AD |
265 | NUM (258) 2 |
266 | OP (259) 1 | |
3c31a486 AD |
267 | |
268 | ||
269 | Nonterminals, with rules where they appear | |
270 | ||
88bce5a2 | 271 | $accept (5) |
3c31a486 AD |
272 | on left: 0 |
273 | exp (6) | |
274 | on left: 1 2, on right: 0 1 | |
275 | ||
276 | ||
277 | state 0 | |
278 | ||
88bce5a2 | 279 | 0 $accept: . exp $end |
ce4ccb4b AD |
280 | 1 exp: . exp OP exp |
281 | 2 | . NUM | |
643a5994 | 282 | |
87675353 | 283 | NUM shift, and go to state 1 |
3c31a486 | 284 | |
87675353 | 285 | exp go to state 2 |
3c31a486 AD |
286 | |
287 | ||
288 | state 1 | |
289 | ||
ce4ccb4b | 290 | 2 exp: NUM . |
3c31a486 | 291 | |
87675353 | 292 | $default reduce using rule 2 (exp) |
3c31a486 AD |
293 | |
294 | ||
295 | state 2 | |
296 | ||
88bce5a2 | 297 | 0 $accept: exp . $end |
ce4ccb4b | 298 | 1 exp: exp . OP exp |
3c31a486 | 299 | |
88bce5a2 AD |
300 | $end shift, and go to state 3 |
301 | OP shift, and go to state 4 | |
3c31a486 AD |
302 | |
303 | ||
304 | state 3 | |
305 | ||
88bce5a2 | 306 | 0 $accept: exp $end . |
3c31a486 | 307 | |
e8832397 | 308 | $default accept |
3c31a486 AD |
309 | |
310 | ||
311 | state 4 | |
312 | ||
ce4ccb4b AD |
313 | 1 exp: . exp OP exp |
314 | 1 | exp OP . exp | |
315 | 2 | . NUM | |
3c31a486 | 316 | |
87675353 | 317 | NUM shift, and go to state 1 |
3c31a486 | 318 | |
87675353 | 319 | exp go to state 5 |
3c31a486 AD |
320 | |
321 | ||
322 | state 5 | |
323 | ||
88bce5a2 AD |
324 | 1 exp: exp . OP exp [$end, OP] |
325 | 1 | exp OP exp . [$end, OP] | |
3c31a486 | 326 | |
87675353 | 327 | $default reduce using rule 1 (exp) |
7ea9a33f | 328 | |
4b3d3a8e | 329 | Conflict between rule 1 and token OP resolved as reduce (%left OP). |
bc933ef1 AD |
330 | ]]) |
331 | ||
332 | AT_CLEANUP | |
333 | ||
334 | ||
bc933ef1 AD |
335 | ## -------------------------------- ## |
336 | ## Defaulted Conflicted Reduction. ## | |
337 | ## -------------------------------- ## | |
338 | ||
339 | # When there are RR conflicts, some rules are disabled. Usually it is | |
340 | # simply displayed as: | |
341 | # | |
88bce5a2 AD |
342 | # $end reduce using rule 3 (num) |
343 | # $end [reduce using rule 4 (id)] | |
bc933ef1 AD |
344 | # |
345 | # But when `reduce 3' is the default action, we'd produce: | |
346 | # | |
88bce5a2 | 347 | # $end [reduce using rule 4 (id)] |
bc933ef1 AD |
348 | # $default reduce using rule 3 (num) |
349 | # | |
350 | # In this precise case (a reduction is masked by the default | |
351 | # reduction), we make the `reduce 3' explicit: | |
352 | # | |
88bce5a2 AD |
353 | # $end reduce using rule 3 (num) |
354 | # $end [reduce using rule 4 (id)] | |
bc933ef1 AD |
355 | # $default reduce using rule 3 (num) |
356 | # | |
357 | # Maybe that's not the best display, but then, please propose something | |
358 | # else. | |
359 | ||
360 | AT_SETUP([Defaulted Conflicted Reduction]) | |
361 | AT_KEYWORDS([report]) | |
362 | ||
363 | AT_DATA([input.y], | |
364 | [[%% | |
365 | exp: num | id; | |
366 | num: '0'; | |
367 | id : '0'; | |
368 | %% | |
369 | ]]) | |
370 | ||
b56471a6 | 371 | AT_CHECK([bison -o input.c --report=all input.y], 0, [], |
2c8ba4cd | 372 | [[input.y: conflicts: 1 reduce/reduce |
2bf21a83 | 373 | input.y:4.6-8: warning: rule never reduced because of conflicts: id: '0' |
e8832397 | 374 | ]]) |
bc933ef1 AD |
375 | |
376 | # Check the contents of the report. | |
377 | AT_CHECK([cat input.output], [], | |
c8f002c7 AD |
378 | [[Rules never reduced |
379 | ||
380 | 4 id: '0' | |
381 | ||
382 | ||
2c8ba4cd | 383 | State 1 conflicts: 1 reduce/reduce |
bc933ef1 AD |
384 | |
385 | ||
386 | Grammar | |
387 | ||
88bce5a2 | 388 | 0 $accept: exp $end |
bc933ef1 AD |
389 | |
390 | 1 exp: num | |
391 | 2 | id | |
392 | ||
393 | 3 num: '0' | |
394 | ||
395 | 4 id: '0' | |
396 | ||
397 | ||
398 | Terminals, with rules where they appear | |
399 | ||
88bce5a2 | 400 | $end (0) 0 |
bc933ef1 AD |
401 | '0' (48) 3 4 |
402 | error (256) | |
403 | ||
404 | ||
405 | Nonterminals, with rules where they appear | |
406 | ||
88bce5a2 | 407 | $accept (4) |
bc933ef1 AD |
408 | on left: 0 |
409 | exp (5) | |
410 | on left: 1 2, on right: 0 | |
411 | num (6) | |
412 | on left: 3, on right: 1 | |
413 | id (7) | |
414 | on left: 4, on right: 2 | |
415 | ||
416 | ||
417 | state 0 | |
418 | ||
88bce5a2 | 419 | 0 $accept: . exp $end |
ce4ccb4b AD |
420 | 1 exp: . num |
421 | 2 | . id | |
422 | 3 num: . '0' | |
423 | 4 id: . '0' | |
bc933ef1 | 424 | |
87675353 | 425 | '0' shift, and go to state 1 |
bc933ef1 | 426 | |
87675353 AD |
427 | exp go to state 2 |
428 | num go to state 3 | |
429 | id go to state 4 | |
bc933ef1 AD |
430 | |
431 | ||
432 | state 1 | |
433 | ||
88bce5a2 AD |
434 | 3 num: '0' . [$end] |
435 | 4 id: '0' . [$end] | |
bc933ef1 | 436 | |
88bce5a2 AD |
437 | $end reduce using rule 3 (num) |
438 | $end [reduce using rule 4 (id)] | |
87675353 | 439 | $default reduce using rule 3 (num) |
bc933ef1 AD |
440 | |
441 | ||
442 | state 2 | |
443 | ||
88bce5a2 | 444 | 0 $accept: exp . $end |
bc933ef1 | 445 | |
88bce5a2 | 446 | $end shift, and go to state 5 |
bc933ef1 AD |
447 | |
448 | ||
449 | state 3 | |
450 | ||
ce4ccb4b | 451 | 1 exp: num . |
bc933ef1 | 452 | |
87675353 | 453 | $default reduce using rule 1 (exp) |
bc933ef1 AD |
454 | |
455 | ||
456 | state 4 | |
457 | ||
ce4ccb4b | 458 | 2 exp: id . |
bc933ef1 | 459 | |
87675353 | 460 | $default reduce using rule 2 (exp) |
bc933ef1 AD |
461 | |
462 | ||
463 | state 5 | |
464 | ||
88bce5a2 | 465 | 0 $accept: exp $end . |
bc933ef1 | 466 | |
e8832397 | 467 | $default accept |
3c31a486 AD |
468 | ]]) |
469 | ||
470 | AT_CLEANUP | |
471 | ||
472 | ||
473 | ||
474 | ||
475 | ## -------------------- ## | |
476 | ## %expect not enough. ## | |
477 | ## -------------------- ## | |
478 | ||
479 | AT_SETUP([%expect not enough]) | |
480 | ||
481 | AT_DATA([input.y], | |
482 | [[%token NUM OP | |
483 | %expect 0 | |
484 | %% | |
485 | exp: exp OP exp | NUM; | |
486 | ]]) | |
487 | ||
b56471a6 | 488 | AT_CHECK([bison -o input.c input.y], 1, [], |
2c8ba4cd | 489 | [input.y: conflicts: 1 shift/reduce |
52489d44 | 490 | input.y: expected 0 shift/reduce conflicts |
3c31a486 AD |
491 | ]) |
492 | AT_CLEANUP | |
493 | ||
494 | ||
495 | ## --------------- ## | |
496 | ## %expect right. ## | |
497 | ## --------------- ## | |
498 | ||
499 | AT_SETUP([%expect right]) | |
500 | ||
501 | AT_DATA([input.y], | |
502 | [[%token NUM OP | |
503 | %expect 1 | |
504 | %% | |
505 | exp: exp OP exp | NUM; | |
506 | ]]) | |
507 | ||
b56471a6 | 508 | AT_CHECK([bison -o input.c input.y]) |
3c31a486 AD |
509 | AT_CLEANUP |
510 | ||
511 | ||
512 | ## ------------------ ## | |
513 | ## %expect too much. ## | |
514 | ## ------------------ ## | |
515 | ||
516 | AT_SETUP([%expect too much]) | |
517 | ||
518 | AT_DATA([input.y], | |
519 | [[%token NUM OP | |
520 | %expect 2 | |
521 | %% | |
522 | exp: exp OP exp | NUM; | |
523 | ]]) | |
524 | ||
b56471a6 | 525 | AT_CHECK([bison -o input.c input.y], 1, [], |
2c8ba4cd | 526 | [input.y: conflicts: 1 shift/reduce |
52489d44 | 527 | input.y: expected 2 shift/reduce conflicts |
3c31a486 AD |
528 | ]) |
529 | AT_CLEANUP | |
6876ecd3 PE |
530 | |
531 | ||
532 | ## ------------------------------ ## | |
533 | ## %expect with reduce conflicts ## | |
534 | ## ------------------------------ ## | |
535 | ||
536 | AT_SETUP([%expect with reduce conflicts]) | |
537 | ||
538 | AT_DATA([input.y], | |
539 | [[%expect 0 | |
540 | %% | |
541 | program: a 'a' | a a; | |
542 | a: 'a'; | |
543 | ]]) | |
544 | ||
545 | AT_CHECK([bison -o input.c input.y], 1, [], | |
2c8ba4cd | 546 | [input.y: conflicts: 1 reduce/reduce |
6876ecd3 PE |
547 | input.y: expected 0 reduce/reduce conflicts |
548 | ]) | |
549 | AT_CLEANUP |