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