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