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