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])
53 AT_DATA_GRAMMAR([input.y],
62 #define YYERROR_VERBOSE 1
64 yyerror (const char *msg)
66 fprintf (stderr, "%s\n", msg);
70 /* The current argument. */
71 static const char *input = NULL;
76 /* No token stands for end of file. */
94 main (int argc, const char *argv[])
102 # Specify the output files to avoid problems on different file systems.
103 AT_CHECK([bison -o input.c input.y])
106 AT_PARSER_CHECK([./input '0<0'])
107 # FIXME: This is an actual bug, but a new one, in the sense that
108 # no one has ever spotted it! The messages are *wrong*: there should
109 # be nothing there, it should be expected eof.
110 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
111 [syntax error, unexpected '<', expecting '<' or '>'
114 AT_PARSER_CHECK([./input '0>0'])
115 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
116 [syntax error, unexpected '>', expecting '<' or '>'
119 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
120 [syntax error, unexpected '>', expecting '<' or '>'
127 ## ------------------------- ##
128 ## Unresolved SR Conflicts. ##
129 ## ------------------------- ##
131 AT_SETUP([Unresolved SR Conflicts])
133 AT_KEYWORDS([report])
138 exp: exp OP exp | NUM;
141 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
142 [input.y: warning: 1 shift/reduce conflict
145 # Check the contents of the report.
146 AT_CHECK([cat input.output], [],
147 [[State 5 contains 1 shift/reduce conflict.
158 Terminals, with rules where they appear
166 Nonterminals, with rules where they appear
171 on left: 1 2, on right: 0 1
176 0 $accept: . exp $end
180 NUM shift, and go to state 1
189 $default reduce using rule 2 (exp)
194 0 $accept: exp . $end
197 $end shift, and go to state 3
198 OP shift, and go to state 4
203 0 $accept: exp $end .
214 NUM shift, and go to state 1
221 1 exp: exp . OP exp [$end, OP]
222 1 | exp OP exp . [$end, OP]
224 OP shift, and go to state 4
226 OP [reduce using rule 1 (exp)]
227 $default reduce using rule 1 (exp)
234 ## ----------------------- ##
235 ## Resolved SR Conflicts. ##
236 ## ----------------------- ##
238 AT_SETUP([Resolved SR Conflicts])
240 AT_KEYWORDS([report])
246 exp: exp OP exp | NUM;
249 AT_CHECK([bison -o input.c --report=all input.y])
251 # Check the contents of the report.
252 AT_CHECK([cat input.output], [],
261 Terminals, with rules where they appear
269 Nonterminals, with rules where they appear
274 on left: 1 2, on right: 0 1
279 0 $accept: . exp $end
283 NUM shift, and go to state 1
292 $default reduce using rule 2 (exp)
297 0 $accept: exp . $end
300 $end shift, and go to state 3
301 OP shift, and go to state 4
306 0 $accept: exp $end .
317 NUM shift, and go to state 1
324 1 exp: exp . OP exp [$end, OP]
325 1 | exp OP exp . [$end, OP]
327 $default reduce using rule 1 (exp)
329 Conflict between rule 1 and token OP resolved as reduce (%left OP).
335 ## -------------------------------- ##
336 ## Defaulted Conflicted Reduction. ##
337 ## -------------------------------- ##
339 # When there are RR conflicts, some rules are disabled. Usually it is
340 # simply displayed as:
342 # $end reduce using rule 3 (num)
343 # $end [reduce using rule 4 (id)]
345 # But when `reduce 3' is the default action, we'd produce:
347 # $end [reduce using rule 4 (id)]
348 # $default reduce using rule 3 (num)
350 # In this precise case (a reduction is masked by the default
351 # reduction), we make the `reduce 3' explicit:
353 # $end reduce using rule 3 (num)
354 # $end [reduce using rule 4 (id)]
355 # $default reduce using rule 3 (num)
357 # Maybe that's not the best display, but then, please propose something
360 AT_SETUP([Defaulted Conflicted Reduction])
361 AT_KEYWORDS([report])
371 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
372 [[input.y: warning: 1 reduce/reduce conflict
373 input.y:4.4-8: warning: rule never reduced because of conflicts: id: '0'
376 # Check the contents of the report.
377 AT_CHECK([cat input.output], [],
378 [[Rules never reduced
383 State 1 contains 1 reduce/reduce conflict.
398 Terminals, with rules where they appear
405 Nonterminals, with rules where they appear
410 on left: 1 2, on right: 0
412 on left: 3, on right: 1
414 on left: 4, on right: 2
419 0 $accept: . exp $end
425 '0' shift, and go to state 1
437 $end reduce using rule 3 (num)
438 $end [reduce using rule 4 (id)]
439 $default reduce using rule 3 (num)
444 0 $accept: exp . $end
446 $end shift, and go to state 5
453 $default reduce using rule 1 (exp)
460 $default reduce using rule 2 (exp)
465 0 $accept: exp $end .
475 ## -------------------- ##
476 ## %expect not enough. ##
477 ## -------------------- ##
479 AT_SETUP([%expect not enough])
485 exp: exp OP exp | NUM;
488 AT_CHECK([bison -o input.c input.y], 1, [],
489 [input.y: warning: 1 shift/reduce conflict
490 input.y: expected 0 shift/reduce conflicts
495 ## --------------- ##
497 ## --------------- ##
499 AT_SETUP([%expect right])
505 exp: exp OP exp | NUM;
508 AT_CHECK([bison -o input.c input.y])
512 ## ------------------ ##
513 ## %expect too much. ##
514 ## ------------------ ##
516 AT_SETUP([%expect too much])
522 exp: exp OP exp | NUM;
525 AT_CHECK([bison -o input.c input.y], 1, [],
526 [input.y: warning: 1 shift/reduce conflict
527 input.y: expected 2 shift/reduce conflicts