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