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 a perfect malloc for these tests. */
65 #define YYERROR_VERBOSE 1
67 yyerror (const char *msg)
69 fprintf (stderr, "%s\n", msg);
73 /* The current argument. */
74 static const char *input = NULL;
79 /* No token stands for end of file. */
97 main (int argc, const char *argv[])
105 # Specify the output files to avoid problems on different file systems.
106 AT_CHECK([bison -o input.c input.y])
109 AT_PARSER_CHECK([./input '0<0'])
110 # FIXME: This is an actual bug, but a new one, in the sense that
111 # no one has ever spotted it! The messages are *wrong*: there should
112 # be nothing there, it should be expected eof.
113 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
114 [parse error, unexpected '<', expecting '<' or '>'
117 AT_PARSER_CHECK([./input '0>0'])
118 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
119 [parse error, unexpected '>', expecting '<' or '>'
122 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
123 [parse error, unexpected '>', expecting '<' or '>'
130 ## ------------------------- ##
131 ## Unresolved SR Conflicts. ##
132 ## ------------------------- ##
134 AT_SETUP([Unresolved SR Conflicts])
136 AT_KEYWORDS([report])
141 exp: exp OP exp | NUM;
144 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
145 [input.y: warning: 1 shift/reduce conflict
148 # Check the contents of the report.
149 AT_CHECK([cat input.output], [],
150 [[State 5 contains 1 shift/reduce conflict.
161 Terminals, with rules where they appear
169 Nonterminals, with rules where they appear
174 on left: 1 2, on right: 0 1
179 0 $accept: . exp $end
183 NUM shift, and go to state 1
192 $default reduce using rule 2 (exp)
197 0 $accept: exp . $end
200 $end shift, and go to state 3
201 OP shift, and go to state 4
206 0 $accept: exp $end .
217 NUM shift, and go to state 1
224 1 exp: exp . OP exp [$end, OP]
225 1 | exp OP exp . [$end, OP]
227 OP shift, and go to state 4
229 OP [reduce using rule 1 (exp)]
230 $default reduce using rule 1 (exp)
237 ## ----------------------- ##
238 ## Resolved SR Conflicts. ##
239 ## ----------------------- ##
241 AT_SETUP([Resolved SR Conflicts])
243 AT_KEYWORDS([report])
249 exp: exp OP exp | NUM;
252 AT_CHECK([bison -o input.c --report=all input.y])
254 # Check the contents of the report.
255 AT_CHECK([cat input.output], [],
264 Terminals, with rules where they appear
272 Nonterminals, with rules where they appear
277 on left: 1 2, on right: 0 1
282 0 $accept: . exp $end
286 NUM shift, and go to state 1
295 $default reduce using rule 2 (exp)
300 0 $accept: exp . $end
303 $end shift, and go to state 3
304 OP shift, and go to state 4
309 0 $accept: exp $end .
320 NUM shift, and go to state 1
327 1 exp: exp . OP exp [$end, OP]
328 1 | exp OP exp . [$end, OP]
330 $default reduce using rule 1 (exp)
332 Conflict between rule 1 and token OP resolved as reduce (%left OP).
338 ## -------------------------------- ##
339 ## Defaulted Conflicted Reduction. ##
340 ## -------------------------------- ##
342 # When there are RR conflicts, some rules are disabled. Usually it is
343 # simply displayed as:
345 # $end reduce using rule 3 (num)
346 # $end [reduce using rule 4 (id)]
348 # But when `reduce 3' is the default action, we'd produce:
350 # $end [reduce using rule 4 (id)]
351 # $default reduce using rule 3 (num)
353 # In this precise case (a reduction is masked by the default
354 # reduction), we make the `reduce 3' explicit:
356 # $end reduce using rule 3 (num)
357 # $end [reduce using rule 4 (id)]
358 # $default reduce using rule 3 (num)
360 # Maybe that's not the best display, but then, please propose something
363 AT_SETUP([Defaulted Conflicted Reduction])
364 AT_KEYWORDS([report])
374 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
375 [[input.y: warning: 1 reduce/reduce conflict
376 input.y:4.4-8: warning: rule never reduced because of conflicts: id: '0'
379 # Check the contents of the report.
380 AT_CHECK([cat input.output], [],
381 [[Rules never reduced
386 State 1 contains 1 reduce/reduce conflict.
401 Terminals, with rules where they appear
408 Nonterminals, with rules where they appear
413 on left: 1 2, on right: 0
415 on left: 3, on right: 1
417 on left: 4, on right: 2
422 0 $accept: . exp $end
428 '0' shift, and go to state 1
440 $end reduce using rule 3 (num)
441 $end [reduce using rule 4 (id)]
442 $default reduce using rule 3 (num)
447 0 $accept: exp . $end
449 $end shift, and go to state 5
456 $default reduce using rule 1 (exp)
463 $default reduce using rule 2 (exp)
468 0 $accept: exp $end .
478 ## -------------------- ##
479 ## %expect not enough. ##
480 ## -------------------- ##
482 AT_SETUP([%expect not enough])
488 exp: exp OP exp | NUM;
491 AT_CHECK([bison -o input.c input.y], 1, [],
492 [input.y: warning: 1 shift/reduce conflict
493 input.y: expected 0 shift/reduce conflicts
498 ## --------------- ##
500 ## --------------- ##
502 AT_SETUP([%expect right])
508 exp: exp OP exp | NUM;
511 AT_CHECK([bison -o input.c input.y])
515 ## ------------------ ##
516 ## %expect too much. ##
517 ## ------------------ ##
519 AT_SETUP([%expect too much])
525 exp: exp OP exp | NUM;
528 AT_CHECK([bison -o input.c input.y], 1, [],
529 [input.y: warning: 1 shift/reduce conflict
530 input.y: expected 2 shift/reduce conflicts