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