]>
Commit | Line | Data |
---|---|---|
1 | # Exercising Bison on conflicts. -*- Autotest -*- | |
2 | ||
3 | # Copyright (C) 2002-2005, 2007-2012 Free Software Foundation, Inc. | |
4 | ||
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU General Public License as published by | |
7 | # the Free Software Foundation, either version 3 of the License, or | |
8 | # (at your option) any later version. | |
9 | # | |
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. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | AT_BANNER([[Conflicts.]]) | |
19 | ||
20 | ||
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 | ||
39 | AT_BISON_CHECK([-o input.c input.y], 0, [], | |
40 | [[input.y:4.9: warning: rule useless in parser due to conflicts: e: /* empty */ [-Wother] | |
41 | ]]) | |
42 | ||
43 | AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [], | |
44 | [[input.y:4.9: warning: rule useless in parser due to conflicts [-Wother] | |
45 | e: 'e' | /* Nothing. */; | |
46 | ^ | |
47 | ]]) | |
48 | ||
49 | AT_CLEANUP | |
50 | ||
51 | ||
52 | ## ------------------- ## | |
53 | ## %nonassoc and eof. ## | |
54 | ## ------------------- ## | |
55 | ||
56 | AT_SETUP([%nonassoc and eof]) | |
57 | ||
58 | AT_BISON_OPTION_PUSHDEFS | |
59 | AT_DATA_GRAMMAR([input.y], | |
60 | [[ | |
61 | %{ | |
62 | #include <stdio.h> | |
63 | #include <stdlib.h> | |
64 | #include <string.h> | |
65 | #include <assert.h> | |
66 | ||
67 | #define YYERROR_VERBOSE 1 | |
68 | ]AT_YYERROR_DEFINE[ | |
69 | /* The current argument. */ | |
70 | static const char *input; | |
71 | ||
72 | static int | |
73 | yylex (void) | |
74 | { | |
75 | static size_t toknum; | |
76 | assert (toknum <= strlen (input)); | |
77 | return input[toknum++]; | |
78 | } | |
79 | ||
80 | %} | |
81 | ||
82 | %nonassoc '<' '>' | |
83 | ||
84 | %% | |
85 | expr: expr '<' expr | |
86 | | expr '>' expr | |
87 | | '0' | |
88 | ; | |
89 | %% | |
90 | int | |
91 | main (int argc, const char *argv[]) | |
92 | { | |
93 | input = argc <= 1 ? "" : argv[1]; | |
94 | return yyparse (); | |
95 | } | |
96 | ]]) | |
97 | AT_BISON_OPTION_POPDEFS | |
98 | ||
99 | m4_pushdef([AT_NONASSOC_AND_EOF_CHECK], | |
100 | [AT_BISON_CHECK([$1[ -o input.c input.y]]) | |
101 | AT_COMPILE([input]) | |
102 | ||
103 | m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])]) | |
104 | ||
105 | AT_PARSER_CHECK([./input '0<0']) | |
106 | AT_PARSER_CHECK([./input '0<0<0'], [1], [], | |
107 | [syntax error, unexpected '<'AT_EXPECTING | |
108 | ]) | |
109 | ||
110 | AT_PARSER_CHECK([./input '0>0']) | |
111 | AT_PARSER_CHECK([./input '0>0>0'], [1], [], | |
112 | [syntax error, unexpected '>'AT_EXPECTING | |
113 | ]) | |
114 | ||
115 | AT_PARSER_CHECK([./input '0<0>0'], [1], [], | |
116 | [syntax error, unexpected '>'AT_EXPECTING | |
117 | ]) | |
118 | ||
119 | m4_popdef([AT_EXPECTING])]) | |
120 | ||
121 | # Expected token list is missing. | |
122 | AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]]) | |
123 | ||
124 | # We must disable default reductions in inconsistent states in order to | |
125 | # have an explicit list of all expected tokens. | |
126 | AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reduction=consistent]], | |
127 | [[correct]]) | |
128 | ||
129 | # lr.default-reduction=consistent happens to work for this test case. | |
130 | # However, for other grammars, lookahead sets can be merged for | |
131 | # different left contexts, so it is still possible to have an incorrect | |
132 | # expected list. Canonical LR is almost a general solution (that is, it | |
133 | # can fail only when %nonassoc is used), so make sure it gives the same | |
134 | # result as above. | |
135 | AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]]) | |
136 | ||
137 | # parse.lac=full is a completely general solution that does not require | |
138 | # any of the above sacrifices. Of course, it does not extend the | |
139 | # language-recognition power of LALR to (IE)LR, but it does ensure that | |
140 | # the reported list of expected tokens matches what the given parser | |
141 | # would have accepted in place of the unexpected token. | |
142 | AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]]) | |
143 | ||
144 | m4_popdef([AT_NONASSOC_AND_EOF_CHECK]) | |
145 | ||
146 | AT_CLEANUP | |
147 | ||
148 | ||
149 | ||
150 | ## ------------------------------------------- ## | |
151 | ## parse.error=verbose and consistent errors. ## | |
152 | ## ------------------------------------------- ## | |
153 | ||
154 | AT_SETUP([[parse.error=verbose and consistent errors]]) | |
155 | ||
156 | m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [ | |
157 | ||
158 | AT_BISON_OPTION_PUSHDEFS([$1]) | |
159 | ||
160 | m4_pushdef([AT_YYLEX_PROTOTYPE], | |
161 | [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]], | |
162 | [[int yylex (YYSTYPE *lvalp)]])]) | |
163 | ||
164 | AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y], | |
165 | [AT_SKEL_JAVA_IF([[ | |
166 | ||
167 | %code imports { | |
168 | import java.io.IOException; | |
169 | }]], [[ | |
170 | ||
171 | %code {]AT_SKEL_CC_IF([[ | |
172 | #include <string>]], [[ | |
173 | #include <assert.h> | |
174 | #include <stdio.h> | |
175 | ]AT_YYERROR_DECLARE])[ | |
176 | ]AT_YYLEX_PROTOTYPE[; | |
177 | #define USE(Var) | |
178 | } | |
179 | ||
180 | ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[ | |
181 | ||
182 | ]$1[ | |
183 | ||
184 | %define parse.error verbose | |
185 | ||
186 | %% | |
187 | ||
188 | ]$2[ | |
189 | ||
190 | ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[ | |
191 | ||
192 | /*--------. | |
193 | | yylex. | | |
194 | `--------*/]AT_SKEL_JAVA_IF([[ | |
195 | ||
196 | public String input = "]$3["; | |
197 | public int index = 0; | |
198 | public int yylex () | |
199 | { | |
200 | if (index < input.length ()) | |
201 | return input.charAt (index++); | |
202 | else | |
203 | return 0; | |
204 | } | |
205 | public Object getLVal () | |
206 | { | |
207 | return new Integer(1); | |
208 | }]], [[ | |
209 | ||
210 | ]AT_YYLEX_PROTOTYPE[ | |
211 | { | |
212 | static char const *input = "]$3["; | |
213 | *lvalp = 1; | |
214 | return *input++; | |
215 | }]])[ | |
216 | ]AT_YYERROR_DEFINE[ | |
217 | ]AT_SKEL_JAVA_IF([[ | |
218 | }; | |
219 | ||
220 | %%]])[ | |
221 | ||
222 | /*-------. | |
223 | | main. | | |
224 | `-------*/]AT_SKEL_JAVA_IF([[ | |
225 | ||
226 | class input | |
227 | { | |
228 | public static void main (String args[]) throws IOException | |
229 | { | |
230 | YYParser p = new YYParser (); | |
231 | p.parse (); | |
232 | } | |
233 | }]], [AT_SKEL_CC_IF([[ | |
234 | ||
235 | int | |
236 | main (void) | |
237 | { | |
238 | yy::parser parser; | |
239 | return parser.parse (); | |
240 | }]], [[ | |
241 | ||
242 | int | |
243 | main (void) | |
244 | { | |
245 | return yyparse (); | |
246 | }]])])[ | |
247 | ]]) | |
248 | ||
249 | AT_FULL_COMPILE([[input]]) | |
250 | ||
251 | m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']], | |
252 | $5, [a], [[, expecting 'a']], | |
253 | $5, [b], [[, expecting 'b']])]) | |
254 | ||
255 | AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]], | |
256 | [AT_PARSER_CHECK([[./input]], [[1]]]), | |
257 | [[]], | |
258 | [[syntax error, unexpected ]$4[]AT_EXPECTING[ | |
259 | ]]) | |
260 | ||
261 | m4_popdef([AT_EXPECTING]) | |
262 | m4_popdef([AT_YYLEX_PROTOTYPE]) | |
263 | AT_BISON_OPTION_POPDEFS | |
264 | ||
265 | ]) | |
266 | ||
267 | m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR], | |
268 | [[%nonassoc 'a'; | |
269 | ||
270 | start: consistent-error-on-a-a 'a' ; | |
271 | ||
272 | consistent-error-on-a-a: | |
273 | 'a' default-reduction | |
274 | | 'a' default-reduction 'a' | |
275 | | 'a' shift | |
276 | ; | |
277 | ||
278 | default-reduction: /*empty*/ ; | |
279 | shift: 'b' ; | |
280 | ||
281 | // Provide another context in which all rules are useful so that this | |
282 | // test case looks a little more realistic. | |
283 | start: 'b' consistent-error-on-a-a 'c' ; | |
284 | ]]) | |
285 | ||
286 | m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]]) | |
287 | ||
288 | # Unfortunately, no expected tokens are reported even though 'b' can be | |
289 | # accepted. Nevertheless, the main point of this test is to make sure | |
290 | # that at least the unexpected token is reported. In a previous version | |
291 | # of Bison, it wasn't reported because the error is detected in a | |
292 | # consistent state with an error action, and that case always triggered | |
293 | # the simple "syntax error" message. | |
294 | # | |
295 | # The point isn't to test IELR here, but state merging happens to | |
296 | # complicate this example. | |
297 | AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]], | |
298 | [AT_PREVIOUS_STATE_GRAMMAR], | |
299 | [AT_PREVIOUS_STATE_INPUT], | |
300 | [[$end]], [[none]]) | |
301 | AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr | |
302 | %glr-parser]], | |
303 | [AT_PREVIOUS_STATE_GRAMMAR], | |
304 | [AT_PREVIOUS_STATE_INPUT], | |
305 | [[$end]], [[none]]) | |
306 | AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr | |
307 | %language "c++"]], | |
308 | [AT_PREVIOUS_STATE_GRAMMAR], | |
309 | [AT_PREVIOUS_STATE_INPUT], | |
310 | [[$end]], [[none]]) | |
311 | AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr | |
312 | %language "java"]], | |
313 | [AT_PREVIOUS_STATE_GRAMMAR], | |
314 | [AT_PREVIOUS_STATE_INPUT], | |
315 | [[end of input]], [[none]]) | |
316 | ||
317 | # Even canonical LR doesn't foresee the error for 'a'! | |
318 | AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr | |
319 | %define lr.default-reduction consistent]], | |
320 | [AT_PREVIOUS_STATE_GRAMMAR], | |
321 | [AT_PREVIOUS_STATE_INPUT], | |
322 | [[$end]], [[ab]]) | |
323 | AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr | |
324 | %define lr.default-reduction accepting]], | |
325 | [AT_PREVIOUS_STATE_GRAMMAR], | |
326 | [AT_PREVIOUS_STATE_INPUT], | |
327 | [[$end]], [[ab]]) | |
328 | AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]], | |
329 | [AT_PREVIOUS_STATE_GRAMMAR], | |
330 | [AT_PREVIOUS_STATE_INPUT], | |
331 | [[$end]], [[ab]]) | |
332 | ||
333 | # Only LAC gets it right. | |
334 | AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr | |
335 | %define parse.lac full]], | |
336 | [AT_PREVIOUS_STATE_GRAMMAR], | |
337 | [AT_PREVIOUS_STATE_INPUT], | |
338 | [[$end]], [[b]]) | |
339 | AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr | |
340 | %define parse.lac full]], | |
341 | [AT_PREVIOUS_STATE_GRAMMAR], | |
342 | [AT_PREVIOUS_STATE_INPUT], | |
343 | [[$end]], [[b]]) | |
344 | ||
345 | m4_popdef([AT_PREVIOUS_STATE_GRAMMAR]) | |
346 | m4_popdef([AT_PREVIOUS_STATE_INPUT]) | |
347 | ||
348 | m4_pushdef([AT_USER_ACTION_GRAMMAR], | |
349 | [[%nonassoc 'a'; | |
350 | ||
351 | // If $$ = 0 here, then we know that the 'a' destructor is being invoked | |
352 | // incorrectly for the 'b' set in the semantic action below. All 'a' | |
353 | // tokens are returned by yylex, which sets $$ = 1. | |
354 | %destructor { | |
355 | if (!$$) | |
356 | fprintf (stderr, "Wrong destructor.\n"); | |
357 | } 'a'; | |
358 | ||
359 | // Rather than depend on an inconsistent state to induce reading a | |
360 | // lookahead as in the previous grammar, just assign the lookahead in a | |
361 | // semantic action. That lookahead isn't needed before either error | |
362 | // action is encountered. In a previous version of Bison, this was a | |
363 | // problem as it meant yychar was not translated into yytoken before | |
364 | // either error action. The second error action thus invoked a | |
365 | // destructor that it selected according to the incorrect yytoken. The | |
366 | // first error action would have reported an incorrect unexpected token | |
367 | // except that, due to the bug described in the previous grammar, the | |
368 | // unexpected token was not reported at all. | |
369 | start: error-reduce consistent-error 'a' { USE ($][3); } ; | |
370 | ||
371 | error-reduce: | |
372 | 'a' 'a' consistent-reduction consistent-error 'a' | |
373 | { USE (($][1, $][2, $][5)); } | |
374 | | 'a' error | |
375 | { USE ($][1); } | |
376 | ; | |
377 | ||
378 | consistent-reduction: /*empty*/ { | |
379 | assert (yychar == YYEMPTY); | |
380 | yylval = 0; | |
381 | yychar = 'b'; | |
382 | } ; | |
383 | ||
384 | consistent-error: | |
385 | 'a' { USE ($][1); } | |
386 | | /*empty*/ %prec 'a' | |
387 | ; | |
388 | ||
389 | // Provide another context in which all rules are useful so that this | |
390 | // test case looks a little more realistic. | |
391 | start: 'b' consistent-error 'b' ; | |
392 | ]]) | |
393 | m4_pushdef([AT_USER_ACTION_INPUT], [[aa]]) | |
394 | ||
395 | AT_CONSISTENT_ERRORS_CHECK([[]], | |
396 | [AT_USER_ACTION_GRAMMAR], | |
397 | [AT_USER_ACTION_INPUT], | |
398 | [['b']], [[none]]) | |
399 | AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]], | |
400 | [AT_USER_ACTION_GRAMMAR], | |
401 | [AT_USER_ACTION_INPUT], | |
402 | [['b']], [[none]]) | |
403 | # No C++ or Java test because yychar cannot be manipulated by users. | |
404 | ||
405 | AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction consistent]], | |
406 | [AT_USER_ACTION_GRAMMAR], | |
407 | [AT_USER_ACTION_INPUT], | |
408 | [['b']], [[none]]) | |
409 | ||
410 | # Canonical LR doesn't foresee the error for 'a'! | |
411 | AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction accepting]], | |
412 | [AT_USER_ACTION_GRAMMAR], | |
413 | [AT_USER_ACTION_INPUT], | |
414 | [[$end]], [[a]]) | |
415 | AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]], | |
416 | [AT_USER_ACTION_GRAMMAR], | |
417 | [AT_USER_ACTION_INPUT], | |
418 | [[$end]], [[a]]) | |
419 | ||
420 | AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]], | |
421 | [AT_USER_ACTION_GRAMMAR], | |
422 | [AT_USER_ACTION_INPUT], | |
423 | [['b']], [[none]]) | |
424 | AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full | |
425 | %define lr.default-reduction accepting]], | |
426 | [AT_USER_ACTION_GRAMMAR], | |
427 | [AT_USER_ACTION_INPUT], | |
428 | [[$end]], [[none]]) | |
429 | ||
430 | m4_popdef([AT_USER_ACTION_GRAMMAR]) | |
431 | m4_popdef([AT_USER_ACTION_INPUT]) | |
432 | ||
433 | m4_popdef([AT_CONSISTENT_ERRORS_CHECK]) | |
434 | ||
435 | AT_CLEANUP | |
436 | ||
437 | ||
438 | ||
439 | ## ------------------------------------------------------- ## | |
440 | ## LAC: %nonassoc requires splitting canonical LR states. ## | |
441 | ## ------------------------------------------------------- ## | |
442 | ||
443 | # This test case demonstrates that, when %nonassoc is used, canonical | |
444 | # LR(1) parser table construction followed by conflict resolution | |
445 | # without further state splitting is not always sufficient to produce a | |
446 | # parser that can detect all syntax errors as soon as possible on one | |
447 | # token of lookahead. However, LAC solves the problem completely even | |
448 | # with minimal LR parser tables. | |
449 | ||
450 | AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]]) | |
451 | AT_BISON_OPTION_PUSHDEFS | |
452 | AT_DATA_GRAMMAR([[input.y]], | |
453 | [[%code { | |
454 | #include <stdio.h> | |
455 | ]AT_YYERROR_DECLARE[ | |
456 | ]AT_YYLEX_DECLARE[ | |
457 | } | |
458 | ||
459 | %error-verbose | |
460 | %nonassoc 'a' | |
461 | ||
462 | %% | |
463 | ||
464 | start: | |
465 | 'a' problem 'a' // First context. | |
466 | | 'b' problem 'b' // Second context. | |
467 | | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful. | |
468 | ; | |
469 | ||
470 | problem: | |
471 | look reduce-nonassoc | |
472 | | look 'a' | |
473 | | look 'b' | |
474 | ; | |
475 | ||
476 | // For the state reached after shifting the 'a' in these productions, | |
477 | // lookahead sets are the same in both the first and second contexts. | |
478 | // Thus, canonical LR reuses the same state for both contexts. However, | |
479 | // the lookahead 'a' for the reduction "look: 'a'" later becomes an | |
480 | // error action only in the first context. In order to immediately | |
481 | // detect the syntax error on 'a' here for only the first context, this | |
482 | // canonical LR state would have to be split into two states, and the | |
483 | // 'a' lookahead would have to be removed from only one of the states. | |
484 | look: | |
485 | 'a' // Reduction lookahead set is always ['a', 'b']. | |
486 | | 'a' 'b' | |
487 | | 'a' 'c' // 'c' is forgotten as an expected token. | |
488 | ; | |
489 | ||
490 | reduce-nonassoc: %prec 'a'; | |
491 | ||
492 | %% | |
493 | ]AT_YYERROR_DEFINE[ | |
494 | ]AT_YYLEX_DEFINE(["aaa"])[ | |
495 | ||
496 | int | |
497 | main (void) | |
498 | { | |
499 | return yyparse (); | |
500 | } | |
501 | ]]) | |
502 | AT_BISON_OPTION_POPDEFS | |
503 | ||
504 | # Show canonical LR's failure. | |
505 | AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]], | |
506 | [[0]], [[]], | |
507 | [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr] | |
508 | ]]) | |
509 | AT_COMPILE([[input]]) | |
510 | AT_PARSER_CHECK([[./input]], [[1]], [[]], | |
511 | [[syntax error, unexpected 'a', expecting 'b' | |
512 | ]]) | |
513 | ||
514 | # It's corrected by LAC. | |
515 | AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \ | |
516 | -o input.c input.y]], [[0]], [[]], | |
517 | [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr] | |
518 | ]]) | |
519 | AT_COMPILE([[input]]) | |
520 | AT_PARSER_CHECK([[./input]], [[1]], [[]], | |
521 | [[syntax error, unexpected 'a', expecting 'b' or 'c' | |
522 | ]]) | |
523 | ||
524 | # IELR is sufficient when LAC is used. | |
525 | AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]], | |
526 | [[0]], [[]], | |
527 | [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr] | |
528 | ]]) | |
529 | AT_COMPILE([[input]]) | |
530 | AT_PARSER_CHECK([[./input]], [[1]], [[]], | |
531 | [[syntax error, unexpected 'a', expecting 'b' or 'c' | |
532 | ]]) | |
533 | ||
534 | AT_CLEANUP | |
535 | ||
536 | ## ------------------------- ## | |
537 | ## Unresolved SR Conflicts. ## | |
538 | ## ------------------------- ## | |
539 | ||
540 | AT_SETUP([Unresolved SR Conflicts]) | |
541 | ||
542 | AT_KEYWORDS([report]) | |
543 | ||
544 | AT_DATA([input.y], | |
545 | [[%token NUM OP | |
546 | %% | |
547 | exp: exp OP exp | NUM; | |
548 | ]]) | |
549 | ||
550 | AT_BISON_CHECK([-o input.c --report=all input.y], 0, [], | |
551 | [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr] | |
552 | ]]) | |
553 | ||
554 | # Check the contents of the report. | |
555 | AT_CHECK([cat input.output], [], | |
556 | [[State 5 conflicts: 1 shift/reduce | |
557 | ||
558 | ||
559 | Grammar | |
560 | ||
561 | 0 $accept: exp $end | |
562 | ||
563 | 1 exp: exp OP exp | |
564 | 2 | NUM | |
565 | ||
566 | ||
567 | Terminals, with rules where they appear | |
568 | ||
569 | $end (0) 0 | |
570 | error (256) | |
571 | NUM (258) 2 | |
572 | OP (259) 1 | |
573 | ||
574 | ||
575 | Nonterminals, with rules where they appear | |
576 | ||
577 | $accept (5) | |
578 | on left: 0 | |
579 | exp (6) | |
580 | on left: 1 2, on right: 0 1 | |
581 | ||
582 | ||
583 | State 0 | |
584 | ||
585 | 0 $accept: . exp $end | |
586 | 1 exp: . exp OP exp | |
587 | 2 | . NUM | |
588 | ||
589 | NUM shift, and go to state 1 | |
590 | ||
591 | exp go to state 2 | |
592 | ||
593 | ||
594 | State 1 | |
595 | ||
596 | 2 exp: NUM . | |
597 | ||
598 | $default reduce using rule 2 (exp) | |
599 | ||
600 | ||
601 | State 2 | |
602 | ||
603 | 0 $accept: exp . $end | |
604 | 1 exp: exp . OP exp | |
605 | ||
606 | $end shift, and go to state 3 | |
607 | OP shift, and go to state 4 | |
608 | ||
609 | ||
610 | State 3 | |
611 | ||
612 | 0 $accept: exp $end . | |
613 | ||
614 | $default accept | |
615 | ||
616 | ||
617 | State 4 | |
618 | ||
619 | 1 exp: . exp OP exp | |
620 | 1 | exp OP . exp | |
621 | 2 | . NUM | |
622 | ||
623 | NUM shift, and go to state 1 | |
624 | ||
625 | exp go to state 5 | |
626 | ||
627 | ||
628 | State 5 | |
629 | ||
630 | 1 exp: exp . OP exp | |
631 | 1 | exp OP exp . [$end, OP] | |
632 | ||
633 | OP shift, and go to state 4 | |
634 | ||
635 | OP [reduce using rule 1 (exp)] | |
636 | $default reduce using rule 1 (exp) | |
637 | ]]) | |
638 | ||
639 | AT_CLEANUP | |
640 | ||
641 | ||
642 | ||
643 | ## ----------------------- ## | |
644 | ## Resolved SR Conflicts. ## | |
645 | ## ----------------------- ## | |
646 | ||
647 | AT_SETUP([Resolved SR Conflicts]) | |
648 | ||
649 | AT_KEYWORDS([report]) | |
650 | ||
651 | AT_DATA([input.y], | |
652 | [[%token NUM OP | |
653 | %left OP | |
654 | %% | |
655 | exp: exp OP exp | NUM; | |
656 | ]]) | |
657 | ||
658 | AT_BISON_CHECK([-o input.c --report=all input.y]) | |
659 | ||
660 | # Check the contents of the report. | |
661 | AT_CHECK([cat input.output], [], | |
662 | [[Grammar | |
663 | ||
664 | 0 $accept: exp $end | |
665 | ||
666 | 1 exp: exp OP exp | |
667 | 2 | NUM | |
668 | ||
669 | ||
670 | Terminals, with rules where they appear | |
671 | ||
672 | $end (0) 0 | |
673 | error (256) | |
674 | NUM (258) 2 | |
675 | OP (259) 1 | |
676 | ||
677 | ||
678 | Nonterminals, with rules where they appear | |
679 | ||
680 | $accept (5) | |
681 | on left: 0 | |
682 | exp (6) | |
683 | on left: 1 2, on right: 0 1 | |
684 | ||
685 | ||
686 | State 0 | |
687 | ||
688 | 0 $accept: . exp $end | |
689 | 1 exp: . exp OP exp | |
690 | 2 | . NUM | |
691 | ||
692 | NUM shift, and go to state 1 | |
693 | ||
694 | exp go to state 2 | |
695 | ||
696 | ||
697 | State 1 | |
698 | ||
699 | 2 exp: NUM . | |
700 | ||
701 | $default reduce using rule 2 (exp) | |
702 | ||
703 | ||
704 | State 2 | |
705 | ||
706 | 0 $accept: exp . $end | |
707 | 1 exp: exp . OP exp | |
708 | ||
709 | $end shift, and go to state 3 | |
710 | OP shift, and go to state 4 | |
711 | ||
712 | ||
713 | State 3 | |
714 | ||
715 | 0 $accept: exp $end . | |
716 | ||
717 | $default accept | |
718 | ||
719 | ||
720 | State 4 | |
721 | ||
722 | 1 exp: . exp OP exp | |
723 | 1 | exp OP . exp | |
724 | 2 | . NUM | |
725 | ||
726 | NUM shift, and go to state 1 | |
727 | ||
728 | exp go to state 5 | |
729 | ||
730 | ||
731 | State 5 | |
732 | ||
733 | 1 exp: exp . OP exp | |
734 | 1 | exp OP exp . [$end, OP] | |
735 | ||
736 | $default reduce using rule 1 (exp) | |
737 | ||
738 | Conflict between rule 1 and token OP resolved as reduce (%left OP). | |
739 | ]]) | |
740 | ||
741 | AT_CLEANUP | |
742 | ||
743 | ||
744 | ## ---------------------- ## | |
745 | ## %precedence suffices. ## | |
746 | ## ---------------------- ## | |
747 | ||
748 | AT_SETUP([%precedence suffices]) | |
749 | ||
750 | AT_DATA([input.y], | |
751 | [[%precedence "then" | |
752 | %precedence "else" | |
753 | %% | |
754 | stmt: | |
755 | "if" cond "then" stmt | |
756 | | "if" cond "then" stmt "else" stmt | |
757 | | "stmt" | |
758 | ; | |
759 | ||
760 | cond: | |
761 | "exp" | |
762 | ; | |
763 | ]]) | |
764 | ||
765 | AT_BISON_CHECK([-o input.c input.y]) | |
766 | ||
767 | AT_CLEANUP | |
768 | ||
769 | ||
770 | ## ------------------------------ ## | |
771 | ## %precedence does not suffice. ## | |
772 | ## ------------------------------ ## | |
773 | ||
774 | AT_SETUP([%precedence does not suffice]) | |
775 | ||
776 | AT_DATA([input.y], | |
777 | [[%precedence "then" | |
778 | %precedence "else" | |
779 | %% | |
780 | stmt: | |
781 | "if" cond "then" stmt | |
782 | | "if" cond "then" stmt "else" stmt | |
783 | | "stmt" | |
784 | ; | |
785 | ||
786 | cond: | |
787 | "exp" | |
788 | | cond "then" cond | |
789 | ; | |
790 | ]]) | |
791 | ||
792 | AT_BISON_CHECK([-o input.c input.y], 0, [], | |
793 | [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr] | |
794 | input.y:12.3-18: warning: rule useless in parser due to conflicts: cond: cond "then" cond [-Wother] | |
795 | ]]) | |
796 | ||
797 | AT_CLEANUP | |
798 | ||
799 | ||
800 | ## -------------------------------- ## | |
801 | ## Defaulted Conflicted Reduction. ## | |
802 | ## -------------------------------- ## | |
803 | ||
804 | # When there are RR conflicts, some rules are disabled. Usually it is | |
805 | # simply displayed as: | |
806 | # | |
807 | # $end reduce using rule 3 (num) | |
808 | # $end [reduce using rule 4 (id)] | |
809 | # | |
810 | # But when `reduce 3' is the default action, we'd produce: | |
811 | # | |
812 | # $end [reduce using rule 4 (id)] | |
813 | # $default reduce using rule 3 (num) | |
814 | # | |
815 | # In this precise case (a reduction is masked by the default | |
816 | # reduction), we make the `reduce 3' explicit: | |
817 | # | |
818 | # $end reduce using rule 3 (num) | |
819 | # $end [reduce using rule 4 (id)] | |
820 | # $default reduce using rule 3 (num) | |
821 | # | |
822 | # Maybe that's not the best display, but then, please propose something | |
823 | # else. | |
824 | ||
825 | AT_SETUP([Defaulted Conflicted Reduction]) | |
826 | AT_KEYWORDS([report]) | |
827 | ||
828 | AT_DATA([input.y], | |
829 | [[%% | |
830 | exp: num | id; | |
831 | num: '0'; | |
832 | id : '0'; | |
833 | %% | |
834 | ]]) | |
835 | ||
836 | AT_BISON_CHECK([-o input.c --report=all input.y], 0, [], | |
837 | [[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr] | |
838 | input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0' [-Wother] | |
839 | ]]) | |
840 | ||
841 | # Check the contents of the report. | |
842 | AT_CHECK([cat input.output], [], | |
843 | [[Rules useless in parser due to conflicts | |
844 | ||
845 | 4 id: '0' | |
846 | ||
847 | ||
848 | State 1 conflicts: 1 reduce/reduce | |
849 | ||
850 | ||
851 | Grammar | |
852 | ||
853 | 0 $accept: exp $end | |
854 | ||
855 | 1 exp: num | |
856 | 2 | id | |
857 | ||
858 | 3 num: '0' | |
859 | ||
860 | 4 id: '0' | |
861 | ||
862 | ||
863 | Terminals, with rules where they appear | |
864 | ||
865 | $end (0) 0 | |
866 | '0' (48) 3 4 | |
867 | error (256) | |
868 | ||
869 | ||
870 | Nonterminals, with rules where they appear | |
871 | ||
872 | $accept (4) | |
873 | on left: 0 | |
874 | exp (5) | |
875 | on left: 1 2, on right: 0 | |
876 | num (6) | |
877 | on left: 3, on right: 1 | |
878 | id (7) | |
879 | on left: 4, on right: 2 | |
880 | ||
881 | ||
882 | State 0 | |
883 | ||
884 | 0 $accept: . exp $end | |
885 | 1 exp: . num | |
886 | 2 | . id | |
887 | 3 num: . '0' | |
888 | 4 id: . '0' | |
889 | ||
890 | '0' shift, and go to state 1 | |
891 | ||
892 | exp go to state 2 | |
893 | num go to state 3 | |
894 | id go to state 4 | |
895 | ||
896 | ||
897 | State 1 | |
898 | ||
899 | 3 num: '0' . [$end] | |
900 | 4 id: '0' . [$end] | |
901 | ||
902 | $end reduce using rule 3 (num) | |
903 | $end [reduce using rule 4 (id)] | |
904 | $default reduce using rule 3 (num) | |
905 | ||
906 | ||
907 | State 2 | |
908 | ||
909 | 0 $accept: exp . $end | |
910 | ||
911 | $end shift, and go to state 5 | |
912 | ||
913 | ||
914 | State 3 | |
915 | ||
916 | 1 exp: num . | |
917 | ||
918 | $default reduce using rule 1 (exp) | |
919 | ||
920 | ||
921 | State 4 | |
922 | ||
923 | 2 exp: id . | |
924 | ||
925 | $default reduce using rule 2 (exp) | |
926 | ||
927 | ||
928 | State 5 | |
929 | ||
930 | 0 $accept: exp $end . | |
931 | ||
932 | $default accept | |
933 | ]]) | |
934 | ||
935 | AT_CLEANUP | |
936 | ||
937 | ||
938 | ||
939 | ||
940 | ## -------------------- ## | |
941 | ## %expect not enough. ## | |
942 | ## -------------------- ## | |
943 | ||
944 | AT_SETUP([%expect not enough]) | |
945 | ||
946 | AT_DATA([input.y], | |
947 | [[%token NUM OP | |
948 | %expect 0 | |
949 | %% | |
950 | exp: exp OP exp | NUM; | |
951 | ]]) | |
952 | ||
953 | AT_BISON_CHECK([-o input.c input.y], 1, [], | |
954 | [[input.y: error: shift/reduce conflicts: 1 found, 0 expected | |
955 | ]]) | |
956 | AT_CLEANUP | |
957 | ||
958 | ||
959 | ## --------------- ## | |
960 | ## %expect right. ## | |
961 | ## --------------- ## | |
962 | ||
963 | AT_SETUP([%expect right]) | |
964 | ||
965 | AT_DATA([input.y], | |
966 | [[%token NUM OP | |
967 | %expect 1 | |
968 | %% | |
969 | exp: exp OP exp | NUM; | |
970 | ]]) | |
971 | ||
972 | AT_BISON_CHECK([-o input.c input.y]) | |
973 | AT_CLEANUP | |
974 | ||
975 | ||
976 | ## ------------------ ## | |
977 | ## %expect too much. ## | |
978 | ## ------------------ ## | |
979 | ||
980 | AT_SETUP([%expect too much]) | |
981 | ||
982 | AT_DATA([input.y], | |
983 | [[%token NUM OP | |
984 | %expect 2 | |
985 | %% | |
986 | exp: exp OP exp | NUM; | |
987 | ]]) | |
988 | ||
989 | AT_BISON_CHECK([-o input.c input.y], 1, [], | |
990 | [[input.y: error: shift/reduce conflicts: 1 found, 2 expected | |
991 | ]]) | |
992 | AT_CLEANUP | |
993 | ||
994 | ||
995 | ## ------------------------------- ## | |
996 | ## %expect with reduce conflicts. ## | |
997 | ## ------------------------------- ## | |
998 | ||
999 | AT_SETUP([%expect with reduce conflicts]) | |
1000 | ||
1001 | AT_DATA([input.y], | |
1002 | [[%expect 0 | |
1003 | %% | |
1004 | program: a 'a' | a a; | |
1005 | a: 'a'; | |
1006 | ]]) | |
1007 | ||
1008 | AT_BISON_CHECK([-o input.c input.y], 1, [], | |
1009 | [[input.y: error: reduce/reduce conflicts: 1 found, 0 expected | |
1010 | ]]) | |
1011 | AT_CLEANUP | |
1012 | ||
1013 | ||
1014 | ## ------------------------- ## | |
1015 | ## %prec with user strings. ## | |
1016 | ## ------------------------- ## | |
1017 | ||
1018 | AT_SETUP([%prec with user string]) | |
1019 | ||
1020 | AT_DATA([[input.y]], | |
1021 | [[%% | |
1022 | exp: | |
1023 | "foo" %prec "foo" | |
1024 | ; | |
1025 | ]]) | |
1026 | ||
1027 | AT_BISON_CHECK([-o input.c input.y]) | |
1028 | AT_CLEANUP | |
1029 | ||
1030 | ||
1031 | ## -------------------------------- ## | |
1032 | ## %no-default-prec without %prec. ## | |
1033 | ## -------------------------------- ## | |
1034 | ||
1035 | AT_SETUP([%no-default-prec without %prec]) | |
1036 | ||
1037 | AT_DATA([[input.y]], | |
1038 | [[%left '+' | |
1039 | %left '*' | |
1040 | ||
1041 | %% | |
1042 | ||
1043 | %no-default-prec; | |
1044 | ||
1045 | e: e '+' e | |
1046 | | e '*' e | |
1047 | | '0' | |
1048 | ; | |
1049 | ]]) | |
1050 | ||
1051 | AT_BISON_CHECK([-o input.c input.y], 0, [], | |
1052 | [[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr] | |
1053 | ]]) | |
1054 | AT_CLEANUP | |
1055 | ||
1056 | ||
1057 | ## ----------------------------- ## | |
1058 | ## %no-default-prec with %prec. ## | |
1059 | ## ----------------------------- ## | |
1060 | ||
1061 | AT_SETUP([%no-default-prec with %prec]) | |
1062 | ||
1063 | AT_DATA([[input.y]], | |
1064 | [[%left '+' | |
1065 | %left '*' | |
1066 | ||
1067 | %% | |
1068 | ||
1069 | %no-default-prec; | |
1070 | ||
1071 | e: e '+' e %prec '+' | |
1072 | | e '*' e %prec '*' | |
1073 | | '0' | |
1074 | ; | |
1075 | ]]) | |
1076 | ||
1077 | AT_BISON_CHECK([-o input.c input.y]) | |
1078 | AT_CLEANUP | |
1079 | ||
1080 | ||
1081 | ## --------------- ## | |
1082 | ## %default-prec. ## | |
1083 | ## --------------- ## | |
1084 | ||
1085 | AT_SETUP([%default-prec]) | |
1086 | ||
1087 | AT_DATA([[input.y]], | |
1088 | [[%left '+' | |
1089 | %left '*' | |
1090 | ||
1091 | %% | |
1092 | ||
1093 | %default-prec; | |
1094 | ||
1095 | e: e '+' e | |
1096 | | e '*' e | |
1097 | | '0' | |
1098 | ; | |
1099 | ]]) | |
1100 | ||
1101 | AT_BISON_CHECK([-o input.c input.y]) | |
1102 | AT_CLEANUP | |
1103 | ||
1104 | ||
1105 | ## ---------------------------------------------- ## | |
1106 | ## Unreachable States After Conflict Resolution. ## | |
1107 | ## ---------------------------------------------- ## | |
1108 | ||
1109 | AT_SETUP([[Unreachable States After Conflict Resolution]]) | |
1110 | ||
1111 | # If conflict resolution makes states unreachable, remove those states, report | |
1112 | # rules that are then unused, and don't report conflicts in those states. Test | |
1113 | # what happens when a nonterminal becomes useless as a result of state removal | |
1114 | # since that causes lalr.o's goto map to be rewritten. | |
1115 | ||
1116 | AT_DATA([[input.y]], | |
1117 | [[%output "input.c" | |
1118 | %left 'a' | |
1119 | ||
1120 | %% | |
1121 | ||
1122 | start: resolved_conflict 'a' reported_conflicts 'a' ; | |
1123 | ||
1124 | /* S/R conflict resolved as reduce, so the state with item | |
1125 | * (resolved_conflict: 'a' . unreachable1) and all it transition successors are | |
1126 | * unreachable, and the associated production is useless. */ | |
1127 | resolved_conflict: | |
1128 | 'a' unreachable1 | |
1129 | | %prec 'a' | |
1130 | ; | |
1131 | ||
1132 | /* S/R conflict that need not be reported since it is unreachable because of | |
1133 | * the previous conflict resolution. Nonterminal unreachable1 and all its | |
1134 | * productions are useless. */ | |
1135 | unreachable1: | |
1136 | 'a' unreachable2 | |
1137 | | | |
1138 | ; | |
1139 | ||
1140 | /* Likewise for a R/R conflict and nonterminal unreachable2. */ | |
1141 | unreachable2: | ; | |
1142 | ||
1143 | /* Make sure remaining S/R and R/R conflicts are still reported correctly even | |
1144 | * when their states are renumbered due to state removal. */ | |
1145 | reported_conflicts: | |
1146 | 'a' | |
1147 | | 'a' | |
1148 | | | |
1149 | ; | |
1150 | ||
1151 | ]]) | |
1152 | ||
1153 | AT_BISON_CHECK([[--report=all input.y]], 0, [], | |
1154 | [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr] | |
1155 | input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr] | |
1156 | input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1 [-Wother] | |
1157 | input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2 [-Wother] | |
1158 | input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother] | |
1159 | input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother] | |
1160 | input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother] | |
1161 | input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother] | |
1162 | input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother] | |
1163 | ]]) | |
1164 | ||
1165 | AT_CHECK([[cat input.output]], 0, | |
1166 | [[Rules useless in parser due to conflicts | |
1167 | ||
1168 | 2 resolved_conflict: 'a' unreachable1 | |
1169 | ||
1170 | 4 unreachable1: 'a' unreachable2 | |
1171 | 5 | /* empty */ | |
1172 | ||
1173 | 6 unreachable2: /* empty */ | |
1174 | 7 | /* empty */ | |
1175 | ||
1176 | 9 reported_conflicts: 'a' | |
1177 | 10 | /* empty */ | |
1178 | ||
1179 | ||
1180 | State 4 conflicts: 1 shift/reduce | |
1181 | State 5 conflicts: 1 reduce/reduce | |
1182 | ||
1183 | ||
1184 | Grammar | |
1185 | ||
1186 | 0 $accept: start $end | |
1187 | ||
1188 | 1 start: resolved_conflict 'a' reported_conflicts 'a' | |
1189 | ||
1190 | 2 resolved_conflict: 'a' unreachable1 | |
1191 | 3 | /* empty */ | |
1192 | ||
1193 | 4 unreachable1: 'a' unreachable2 | |
1194 | 5 | /* empty */ | |
1195 | ||
1196 | 6 unreachable2: /* empty */ | |
1197 | 7 | /* empty */ | |
1198 | ||
1199 | 8 reported_conflicts: 'a' | |
1200 | 9 | 'a' | |
1201 | 10 | /* empty */ | |
1202 | ||
1203 | ||
1204 | Terminals, with rules where they appear | |
1205 | ||
1206 | $end (0) 0 | |
1207 | 'a' (97) 1 2 4 8 9 | |
1208 | error (256) | |
1209 | ||
1210 | ||
1211 | Nonterminals, with rules where they appear | |
1212 | ||
1213 | $accept (4) | |
1214 | on left: 0 | |
1215 | start (5) | |
1216 | on left: 1, on right: 0 | |
1217 | resolved_conflict (6) | |
1218 | on left: 2 3, on right: 1 | |
1219 | unreachable1 (7) | |
1220 | on left: 4 5, on right: 2 | |
1221 | unreachable2 (8) | |
1222 | on left: 6 7, on right: 4 | |
1223 | reported_conflicts (9) | |
1224 | on left: 8 9 10, on right: 1 | |
1225 | ||
1226 | ||
1227 | State 0 | |
1228 | ||
1229 | 0 $accept: . start $end | |
1230 | 1 start: . resolved_conflict 'a' reported_conflicts 'a' | |
1231 | 2 resolved_conflict: . 'a' unreachable1 | |
1232 | 3 | . ['a'] | |
1233 | ||
1234 | $default reduce using rule 3 (resolved_conflict) | |
1235 | ||
1236 | start go to state 1 | |
1237 | resolved_conflict go to state 2 | |
1238 | ||
1239 | Conflict between rule 3 and token 'a' resolved as reduce (%left 'a'). | |
1240 | ||
1241 | ||
1242 | State 1 | |
1243 | ||
1244 | 0 $accept: start . $end | |
1245 | ||
1246 | $end shift, and go to state 3 | |
1247 | ||
1248 | ||
1249 | State 2 | |
1250 | ||
1251 | 1 start: resolved_conflict . 'a' reported_conflicts 'a' | |
1252 | ||
1253 | 'a' shift, and go to state 4 | |
1254 | ||
1255 | ||
1256 | State 3 | |
1257 | ||
1258 | 0 $accept: start $end . | |
1259 | ||
1260 | $default accept | |
1261 | ||
1262 | ||
1263 | State 4 | |
1264 | ||
1265 | 1 start: resolved_conflict 'a' . reported_conflicts 'a' | |
1266 | 8 reported_conflicts: . 'a' | |
1267 | 9 | . 'a' | |
1268 | 10 | . ['a'] | |
1269 | ||
1270 | 'a' shift, and go to state 5 | |
1271 | ||
1272 | 'a' [reduce using rule 10 (reported_conflicts)] | |
1273 | ||
1274 | reported_conflicts go to state 6 | |
1275 | ||
1276 | ||
1277 | State 5 | |
1278 | ||
1279 | 8 reported_conflicts: 'a' . ['a'] | |
1280 | 9 | 'a' . ['a'] | |
1281 | ||
1282 | 'a' reduce using rule 8 (reported_conflicts) | |
1283 | 'a' [reduce using rule 9 (reported_conflicts)] | |
1284 | $default reduce using rule 8 (reported_conflicts) | |
1285 | ||
1286 | ||
1287 | State 6 | |
1288 | ||
1289 | 1 start: resolved_conflict 'a' reported_conflicts . 'a' | |
1290 | ||
1291 | 'a' shift, and go to state 7 | |
1292 | ||
1293 | ||
1294 | State 7 | |
1295 | ||
1296 | 1 start: resolved_conflict 'a' reported_conflicts 'a' . | |
1297 | ||
1298 | $default reduce using rule 1 (start) | |
1299 | ]]) | |
1300 | ||
1301 | AT_DATA([[input-keep.y]], | |
1302 | [[%define lr.keep-unreachable-state | |
1303 | ]]) | |
1304 | AT_CHECK([[cat input.y >> input-keep.y]]) | |
1305 | ||
1306 | AT_BISON_CHECK([[input-keep.y]], 0, [], | |
1307 | [[input-keep.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr] | |
1308 | input-keep.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr] | |
1309 | input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother] | |
1310 | input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother] | |
1311 | input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother] | |
1312 | input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother] | |
1313 | ]]) | |
1314 | ||
1315 | AT_CLEANUP | |
1316 | ||
1317 | ||
1318 | ## ------------------------------------------------------------ ## | |
1319 | ## Solved conflicts report for multiple reductions in a state. ## | |
1320 | ## ------------------------------------------------------------ ## | |
1321 | ||
1322 | AT_SETUP([[Solved conflicts report for multiple reductions in a state]]) | |
1323 | ||
1324 | # Used to lose earlier solved conflict messages even within a single S/R/R. | |
1325 | ||
1326 | AT_DATA([[input.y]], | |
1327 | [[%left 'a' | |
1328 | %right 'b' | |
1329 | %right 'c' | |
1330 | %right 'd' | |
1331 | %% | |
1332 | start: | |
1333 | 'a' | |
1334 | | empty_a 'a' | |
1335 | | 'b' | |
1336 | | empty_b 'b' | |
1337 | | 'c' | |
1338 | | empty_c1 'c' | |
1339 | | empty_c2 'c' | |
1340 | | empty_c3 'c' | |
1341 | ; | |
1342 | empty_a: %prec 'a' ; | |
1343 | empty_b: %prec 'b' ; | |
1344 | empty_c1: %prec 'c' ; | |
1345 | empty_c2: %prec 'c' ; | |
1346 | empty_c3: %prec 'd' ; | |
1347 | ]]) | |
1348 | AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore]) | |
1349 | AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0, | |
1350 | [[State 0 | |
1351 | ||
1352 | 0 $accept: . start $end | |
1353 | 1 start: . 'a' | |
1354 | 2 | . empty_a 'a' | |
1355 | 3 | . 'b' | |
1356 | 4 | . empty_b 'b' | |
1357 | 5 | . 'c' | |
1358 | 6 | . empty_c1 'c' | |
1359 | 7 | . empty_c2 'c' | |
1360 | 8 | . empty_c3 'c' | |
1361 | 9 empty_a: . ['a'] | |
1362 | 10 empty_b: . [] | |
1363 | 11 empty_c1: . [] | |
1364 | 12 empty_c2: . [] | |
1365 | 13 empty_c3: . ['c'] | |
1366 | ||
1367 | 'b' shift, and go to state 1 | |
1368 | ||
1369 | 'c' reduce using rule 13 (empty_c3) | |
1370 | $default reduce using rule 9 (empty_a) | |
1371 | ||
1372 | start go to state 2 | |
1373 | empty_a go to state 3 | |
1374 | empty_b go to state 4 | |
1375 | empty_c1 go to state 5 | |
1376 | empty_c2 go to state 6 | |
1377 | empty_c3 go to state 7 | |
1378 | ||
1379 | Conflict between rule 9 and token 'a' resolved as reduce (%left 'a'). | |
1380 | Conflict between rule 10 and token 'b' resolved as shift (%right 'b'). | |
1381 | Conflict between rule 11 and token 'c' resolved as shift (%right 'c'). | |
1382 | Conflict between rule 12 and token 'c' resolved as shift (%right 'c'). | |
1383 | Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd'). | |
1384 | ||
1385 | ||
1386 | State 1 | |
1387 | ]]) | |
1388 | ||
1389 | AT_CLEANUP | |
1390 | ||
1391 | ||
1392 | ## ------------------------------------------------------------ ## | |
1393 | ## %nonassoc error actions for multiple reductions in a state. ## | |
1394 | ## ------------------------------------------------------------ ## | |
1395 | ||
1396 | # Used to abort when trying to resolve conflicts as %nonassoc error actions for | |
1397 | # multiple reductions in a state. | |
1398 | ||
1399 | # For a %nonassoc error action token, used to print the first remaining | |
1400 | # reduction on that token without brackets. | |
1401 | ||
1402 | AT_SETUP([[%nonassoc error actions for multiple reductions in a state]]) | |
1403 | ||
1404 | AT_DATA([[input.y]], | |
1405 | [[%nonassoc 'a' 'b' 'c' | |
1406 | %% | |
1407 | start: | |
1408 | 'a' | |
1409 | | empty_a 'a' | |
1410 | | 'b' | |
1411 | | empty_b 'b' | |
1412 | | 'c' | |
1413 | | empty_c1 'c' | |
1414 | | empty_c2 'c' | |
1415 | | empty_c3 'c' | |
1416 | ; | |
1417 | empty_a: %prec 'a' ; | |
1418 | empty_b: %prec 'b' ; | |
1419 | empty_c1: %prec 'c' ; | |
1420 | empty_c2: %prec 'c' ; | |
1421 | empty_c3: %prec 'c' ; | |
1422 | ]]) | |
1423 | ||
1424 | AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore]) | |
1425 | AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0, | |
1426 | [[State 0 | |
1427 | ||
1428 | 0 $accept: . start $end | |
1429 | 1 start: . 'a' | |
1430 | 2 | . empty_a 'a' | |
1431 | 3 | . 'b' | |
1432 | 4 | . empty_b 'b' | |
1433 | 5 | . 'c' | |
1434 | 6 | . empty_c1 'c' | |
1435 | 7 | . empty_c2 'c' | |
1436 | 8 | . empty_c3 'c' | |
1437 | 9 empty_a: . [] | |
1438 | 10 empty_b: . [] | |
1439 | 11 empty_c1: . [] | |
1440 | 12 empty_c2: . ['c'] | |
1441 | 13 empty_c3: . ['c'] | |
1442 | ||
1443 | 'a' error (nonassociative) | |
1444 | 'b' error (nonassociative) | |
1445 | 'c' error (nonassociative) | |
1446 | ||
1447 | 'c' [reduce using rule 12 (empty_c2)] | |
1448 | 'c' [reduce using rule 13 (empty_c3)] | |
1449 | ||
1450 | start go to state 1 | |
1451 | empty_a go to state 2 | |
1452 | empty_b go to state 3 | |
1453 | empty_c1 go to state 4 | |
1454 | empty_c2 go to state 5 | |
1455 | empty_c3 go to state 6 | |
1456 | ||
1457 | Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a'). | |
1458 | Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b'). | |
1459 | Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c'). | |
1460 | ||
1461 | ||
1462 | State 1 | |
1463 | ]]) | |
1464 | AT_CLEANUP | |
1465 | ||
1466 | ||
1467 | ## -------------------- ## | |
1468 | ## %expect-rr non GLR. ## | |
1469 | ## -------------------- ## | |
1470 | ||
1471 | AT_SETUP([[%expect-rr non GLR]]) | |
1472 | ||
1473 | AT_DATA([[1.y]], | |
1474 | [[%expect-rr 0 | |
1475 | %% | |
1476 | exp: 'a' | |
1477 | ]]) | |
1478 | ||
1479 | AT_BISON_CHECK([[1.y]], [[0]], [], | |
1480 | [[1.y: warning: %expect-rr applies only to GLR parsers [-Wother] | |
1481 | ]]) | |
1482 | ||
1483 | AT_DATA([[2.y]], | |
1484 | [[%expect-rr 1 | |
1485 | %% | |
1486 | exp: 'a' | 'a'; | |
1487 | ]]) | |
1488 | ||
1489 | AT_BISON_CHECK([[2.y]], [[0]], [], | |
1490 | [[2.y: warning: %expect-rr applies only to GLR parsers [-Wother] | |
1491 | 2.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr] | |
1492 | 2.y:3.12-14: warning: rule useless in parser due to conflicts: exp: 'a' [-Wother] | |
1493 | ]]) | |
1494 | ||
1495 | AT_CLEANUP | |
1496 | ||
1497 | ||
1498 | ## ---------------------------------- ## | |
1499 | ## -W versus %expect and %expect-rr. ## | |
1500 | ## ---------------------------------- ## | |
1501 | ||
1502 | AT_SETUP([[-W versus %expect and %expect-rr]]) | |
1503 | ||
1504 | AT_DATA([[sr-rr.y]], | |
1505 | [[%glr-parser | |
1506 | %% | |
1507 | start: 'a' | A 'a' | B 'a' ; | |
1508 | A: ; | |
1509 | B: ; | |
1510 | ]]) | |
1511 | AT_DATA([[sr.y]], | |
1512 | [[%glr-parser | |
1513 | %% | |
1514 | start: 'a' | A 'a' ; | |
1515 | A: ; | |
1516 | ]]) | |
1517 | AT_DATA([[rr.y]], | |
1518 | [[%glr-parser | |
1519 | %% | |
1520 | start: A | B ; | |
1521 | A: ; | |
1522 | B: ; | |
1523 | ]]) | |
1524 | ||
1525 | AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]], | |
1526 | [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr] | |
1527 | sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr] | |
1528 | ]]) | |
1529 | AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]], | |
1530 | [[sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr] | |
1531 | ]]) | |
1532 | AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]], | |
1533 | [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr] | |
1534 | ]]) | |
1535 | ||
1536 | [ | |
1537 | # This is piece of code is rather complex for a simple task: try every | |
1538 | # combinaison of (0 or 1 real SR) x (0 or 1 real RR) x (don't %expect | |
1539 | # or %expect 0, 1, or 2 SR) x (don't %expect-rr or %expect-rr 0, 1, or 2 | |
1540 | # RR). | |
1541 | ||
1542 | # Number and types of genuine conflicts in the grammar. | |
1543 | for gram in sr-rr sr rr; do | |
1544 | # Number of expected s/r conflicts. | |
1545 | for sr_exp_i in '' 0 1 2; do | |
1546 | # Number of expected r/r conflicts. | |
1547 | for rr_exp_i in '' 0 1 2; do | |
1548 | test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue | |
1549 | ||
1550 | # Build grammar file. | |
1551 | sr_exp=0 | |
1552 | rr_exp=0 | |
1553 | file=$gram | |
1554 | directives= | |
1555 | if test -n "$sr_exp_i"; then | |
1556 | sr_exp=$sr_exp_i | |
1557 | file=$file-expect-$sr_exp | |
1558 | directives="%expect $sr_exp" | |
1559 | fi | |
1560 | if test -n "$rr_exp_i"; then | |
1561 | rr_exp=$rr_exp_i | |
1562 | file=$file-expect-rr-$rr_exp | |
1563 | directives="$directives %expect-rr $rr_exp" | |
1564 | fi | |
1565 | file=$file.y | |
1566 | echo "$directives" > $file | |
1567 | cat $gram.y >> $file | |
1568 | ||
1569 | # Number of found conflicts. | |
1570 | case $gram in | |
1571 | (sr) sr_count=1; rr_count=0;; | |
1572 | (rr) sr_count=0; rr_count=1;; | |
1573 | (sr-rr) sr_count=1; rr_count=1;; | |
1574 | esac | |
1575 | ||
1576 | # Update number of expected conflicts: if %expect is given then | |
1577 | # %expect-rr defaults to 0, and vice-versa. Leave empty if | |
1578 | # nothing expected. | |
1579 | case $sr_exp_i:$rr_exp_i in | |
1580 | ?:) rr_exp_i=0;; | |
1581 | :?) sr_exp_i=0;; | |
1582 | esac | |
1583 | ||
1584 | # Run tests. | |
1585 | if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then | |
1586 | ]AT_BISON_CHECK([[-Wnone $file]])[ | |
1587 | ]AT_BISON_CHECK([[-Werror $file]])[ | |
1588 | else | |
1589 | { | |
1590 | if test -z "$sr_exp_i" && test "$sr_count" -ne 0; then | |
1591 | echo "warning: $sr_count shift/reduce conflicts" | |
1592 | elif test "$sr_exp_i" -ne "$sr_count"; then | |
1593 | echo "error: shift/reduce conflicts: $sr_count found, $sr_exp_i expected" | |
1594 | fi | |
1595 | if test -z "$rr_exp_i" && test "$rr_count" -ne 0; then | |
1596 | echo "warning: $rr_count reduce/reduce conflicts" | |
1597 | elif test "$rr_exp_i" -ne "$rr_count"; then | |
1598 | echo "error: reduce/reduce conflicts: $rr_count found, $rr_exp_i expected" | |
1599 | fi | |
1600 | } | sed -e "s/^/$file: /" > experr | |
1601 | ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[ | |
1602 | ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[ | |
1603 | fi | |
1604 | done | |
1605 | done | |
1606 | done] | |
1607 | ||
1608 | AT_CLEANUP |