1 # Exercising Bison on conflicts. -*- Autotest -*-
2 # Copyright (C) 2002 Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 AT_BANNER([[Conflicts.]])
22 ## ---------------- ##
24 ## ---------------- ##
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.
31 AT_SETUP([S/R in initial])
37 e: 'e' | /* Nothing. */;
40 AT_CHECK([bison -o input.c input.y], 0, [],
41 [[input.y:4.8: warning: rule never reduced because of conflicts: e: /* empty */
47 ## ------------------- ##
48 ## %nonassoc and eof. ##
49 ## ------------------- ##
51 AT_SETUP([%nonassoc and eof])
57 /* We don't need perfect functions for these tests. */
67 #define YYERROR_VERBOSE 1
69 yyerror (const char *msg)
71 fprintf (stderr, "%s\n", msg);
75 /* The current argument. */
76 static const char *input = NULL;
81 /* No token stands for end of file. */
99 main (int argc, const char *argv[])
107 # Specify the output files to avoid problems on different file systems.
108 AT_CHECK([bison -o input.c input.y])
111 AT_PARSER_CHECK([./input '0<0'])
112 # FIXME: This is an actual bug, but a new one, in the sense that
113 # no one has ever spotted it! The messages are *wrong*: there should
114 # be nothing there, it should be expected eof.
115 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
116 [parse error, unexpected '<', expecting '<' or '>'
119 AT_PARSER_CHECK([./input '0>0'])
120 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
121 [parse error, unexpected '>', expecting '<' or '>'
124 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
125 [parse error, unexpected '>', expecting '<' or '>'
132 ## ------------------------- ##
133 ## Unresolved SR Conflicts. ##
134 ## ------------------------- ##
136 AT_SETUP([Unresolved SR Conflicts])
138 AT_KEYWORDS([report])
143 exp: exp OP exp | NUM;
146 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
147 [input.y: warning: 1 shift/reduce conflict
150 # Check the contents of the report.
151 AT_CHECK([cat input.output], [],
152 [[State 5 contains 1 shift/reduce conflict.
163 Terminals, with rules where they appear
171 Nonterminals, with rules where they appear
176 on left: 1 2, on right: 0 1
181 0 $accept: . exp $end
185 NUM shift, and go to state 1
194 $default reduce using rule 2 (exp)
199 0 $accept: exp . $end
202 $end shift, and go to state 3
203 OP shift, and go to state 4
208 0 $accept: exp $end .
219 NUM shift, and go to state 1
226 1 exp: exp . OP exp [$end, OP]
227 1 | exp OP exp . [$end, OP]
229 OP shift, and go to state 4
231 OP [reduce using rule 1 (exp)]
232 $default reduce using rule 1 (exp)
239 ## ----------------------- ##
240 ## Resolved SR Conflicts. ##
241 ## ----------------------- ##
243 AT_SETUP([Resolved SR Conflicts])
245 AT_KEYWORDS([report])
251 exp: exp OP exp | NUM;
254 AT_CHECK([bison -o input.c --report=all input.y])
256 # Check the contents of the report.
257 AT_CHECK([cat input.output], [],
266 Terminals, with rules where they appear
274 Nonterminals, with rules where they appear
279 on left: 1 2, on right: 0 1
284 0 $accept: . exp $end
288 NUM shift, and go to state 1
297 $default reduce using rule 2 (exp)
302 0 $accept: exp . $end
305 $end shift, and go to state 3
306 OP shift, and go to state 4
311 0 $accept: exp $end .
322 NUM shift, and go to state 1
329 1 exp: exp . OP exp [$end, OP]
330 1 | exp OP exp . [$end, OP]
332 $default reduce using rule 1 (exp)
334 Conflict between rule 1 and token OP resolved as reduce (%left OP).
340 ## -------------------------------- ##
341 ## Defaulted Conflicted Reduction. ##
342 ## -------------------------------- ##
344 # When there are RR conflicts, some rules are disabled. Usually it is
345 # simply displayed as:
347 # $end reduce using rule 3 (num)
348 # $end [reduce using rule 4 (id)]
350 # But when `reduce 3' is the default action, we'd produce:
352 # $end [reduce using rule 4 (id)]
353 # $default reduce using rule 3 (num)
355 # In this precise case (a reduction is masked by the default
356 # reduction), we make the `reduce 3' explicit:
358 # $end reduce using rule 3 (num)
359 # $end [reduce using rule 4 (id)]
360 # $default reduce using rule 3 (num)
362 # Maybe that's not the best display, but then, please propose something
365 AT_SETUP([Defaulted Conflicted Reduction])
366 AT_KEYWORDS([report])
376 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
377 [[input.y: warning: 1 reduce/reduce conflict
378 input.y:4.4-8: warning: rule never reduced because of conflicts: id: '0'
381 # Check the contents of the report.
382 AT_CHECK([cat input.output], [],
383 [[Rules never reduced
388 State 1 contains 1 reduce/reduce conflict.
403 Terminals, with rules where they appear
410 Nonterminals, with rules where they appear
415 on left: 1 2, on right: 0
417 on left: 3, on right: 1
419 on left: 4, on right: 2
424 0 $accept: . exp $end
430 '0' shift, and go to state 1
442 $end reduce using rule 3 (num)
443 $end [reduce using rule 4 (id)]
444 $default reduce using rule 3 (num)
449 0 $accept: exp . $end
451 $end shift, and go to state 5
458 $default reduce using rule 1 (exp)
465 $default reduce using rule 2 (exp)
470 0 $accept: exp $end .
480 ## -------------------- ##
481 ## %expect not enough. ##
482 ## -------------------- ##
484 AT_SETUP([%expect not enough])
490 exp: exp OP exp | NUM;
493 AT_CHECK([bison -o input.c input.y], 1, [],
494 [input.y: warning: 1 shift/reduce conflict
495 input.y: expected 0 shift/reduce conflicts
500 ## --------------- ##
502 ## --------------- ##
504 AT_SETUP([%expect right])
510 exp: exp OP exp | NUM;
513 AT_CHECK([bison -o input.c input.y])
517 ## ------------------ ##
518 ## %expect too much. ##
519 ## ------------------ ##
521 AT_SETUP([%expect too much])
527 exp: exp OP exp | NUM;
530 AT_CHECK([bison -o input.c input.y], 1, [],
531 [input.y: warning: 1 shift/reduce conflict
532 input.y: expected 2 shift/reduce conflicts