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 input.y -o input.c], 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 input.y -o input.c])
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 input.y -o input.c --report=all], 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 input.y -o input.c --report=all])
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)
331 Conflict between rule 1 and token OP resolved as reduce (%left OP).
337 ## -------------------------------- ##
338 ## Defaulted Conflicted Reduction. ##
339 ## -------------------------------- ##
341 # When there are RR conflicts, some rules are disabled. Usually it is
342 # simply displayed as:
344 # $end reduce using rule 3 (num)
345 # $end [reduce using rule 4 (id)]
347 # But when `reduce 3' is the default action, we'd produce:
349 # $end [reduce using rule 4 (id)]
350 # $default reduce using rule 3 (num)
352 # In this precise case (a reduction is masked by the default
353 # reduction), we make the `reduce 3' explicit:
355 # $end reduce using rule 3 (num)
356 # $end [reduce using rule 4 (id)]
357 # $default reduce using rule 3 (num)
359 # Maybe that's not the best display, but then, please propose something
362 AT_SETUP([Defaulted Conflicted Reduction])
363 AT_KEYWORDS([report])
373 AT_CHECK([bison input.y -o input.c --report=all], 0, [],
374 [[input.y: warning: 1 reduce/reduce conflict
375 input.y:4.4-8: warning: rule never reduced because of conflicts: id: '0'
378 # Check the contents of the report.
379 AT_CHECK([cat input.output], [],
380 [[State 1 contains 1 reduce/reduce conflict.
395 Terminals, with rules where they appear
402 Nonterminals, with rules where they appear
407 on left: 1 2, on right: 0
409 on left: 3, on right: 1
411 on left: 4, on right: 2
416 0 $accept: . exp $end
422 '0' shift, and go to state 1
434 $end reduce using rule 3 (num)
435 $end [reduce using rule 4 (id)]
436 $default reduce using rule 3 (num)
441 0 $accept: exp . $end
443 $end shift, and go to state 5
450 $default reduce using rule 1 (exp)
457 $default reduce using rule 2 (exp)
462 0 $accept: exp $end .
472 ## -------------------- ##
473 ## %expect not enough. ##
474 ## -------------------- ##
476 AT_SETUP([%expect not enough])
482 exp: exp OP exp | NUM;
485 AT_CHECK([bison input.y -o input.c], 1, [],
486 [input.y: warning: 1 shift/reduce conflict
487 input.y: expected 0 shift/reduce conflicts
492 ## --------------- ##
494 ## --------------- ##
496 AT_SETUP([%expect right])
502 exp: exp OP exp | NUM;
505 AT_CHECK([bison input.y -o input.c])
509 ## ------------------ ##
510 ## %expect too much. ##
511 ## ------------------ ##
513 AT_SETUP([%expect too much])
519 exp: exp OP exp | NUM;
522 AT_CHECK([bison input.y -o input.c], 1, [],
523 [input.y: warning: 1 shift/reduce conflict
524 input.y: expected 2 shift/reduce conflicts