1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002, 2003 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 AT_BANNER([[Conflicts.]])
23 ## ---------------- ##
25 ## ---------------- ##
27 # I once hacked Bison in such a way that it lost its reductions on the
28 # initial state (because it was confusing it with the last state). It
29 # took me a while to strip down my failures to this simple case. So
30 # make sure it finds the s/r conflict below.
32 AT_SETUP([S/R in initial])
38 e: 'e' | /* Nothing. */;
41 AT_CHECK([bison -o input.c input.y], 0, [],
42 [[input.y:4.9: warning: rule never reduced because of conflicts: e: /* empty */
48 ## ------------------- ##
49 ## %nonassoc and eof. ##
50 ## ------------------- ##
52 AT_SETUP([%nonassoc and eof])
54 AT_DATA_GRAMMAR([input.y],
63 #define YYERROR_VERBOSE 1
65 yyerror (const char *msg)
67 fprintf (stderr, "%s\n", msg);
71 /* The current argument. */
72 static const char *input = NULL;
77 /* No token stands for end of file. */
95 main (int argc, const char *argv[])
103 # Specify the output files to avoid problems on different file systems.
104 AT_CHECK([bison -o input.c input.y])
107 AT_PARSER_CHECK([./input '0<0'])
108 # FIXME: This is an actual bug, but a new one, in the sense that
109 # no one has ever spotted it! The messages are *wrong*: there should
110 # be nothing there, it should be expected eof.
111 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
112 [syntax error, unexpected '<', expecting '<' or '>'
115 AT_PARSER_CHECK([./input '0>0'])
116 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
117 [syntax error, unexpected '>', expecting '<' or '>'
120 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
121 [syntax error, unexpected '>', expecting '<' or '>'
128 ## ------------------------- ##
129 ## Unresolved SR Conflicts. ##
130 ## ------------------------- ##
132 AT_SETUP([Unresolved SR Conflicts])
134 AT_KEYWORDS([report])
139 exp: exp OP exp | NUM;
142 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
143 [input.y: conflicts: 1 shift/reduce
146 # Check the contents of the report.
147 AT_CHECK([cat input.output], [],
148 [[State 5 conflicts: 1 shift/reduce
159 Terminals, with rules where they appear
167 Nonterminals, with rules where they appear
172 on left: 1 2, on right: 0 1
177 0 $accept: . exp $end
181 NUM shift, and go to state 1
190 $default reduce using rule 2 (exp)
195 0 $accept: exp . $end
198 $end shift, and go to state 3
199 OP shift, and go to state 4
204 0 $accept: exp $end .
215 NUM shift, and go to state 1
222 1 exp: exp . OP exp [$end, OP]
223 1 | exp OP exp . [$end, OP]
225 OP shift, and go to state 4
227 OP [reduce using rule 1 (exp)]
228 $default reduce using rule 1 (exp)
235 ## ----------------------- ##
236 ## Resolved SR Conflicts. ##
237 ## ----------------------- ##
239 AT_SETUP([Resolved SR Conflicts])
241 AT_KEYWORDS([report])
247 exp: exp OP exp | NUM;
250 AT_CHECK([bison -o input.c --report=all input.y])
252 # Check the contents of the report.
253 AT_CHECK([cat input.output], [],
262 Terminals, with rules where they appear
270 Nonterminals, with rules where they appear
275 on left: 1 2, on right: 0 1
280 0 $accept: . exp $end
284 NUM shift, and go to state 1
293 $default reduce using rule 2 (exp)
298 0 $accept: exp . $end
301 $end shift, and go to state 3
302 OP shift, and go to state 4
307 0 $accept: exp $end .
318 NUM shift, and go to state 1
325 1 exp: exp . OP exp [$end, OP]
326 1 | exp OP exp . [$end, OP]
328 $default reduce using rule 1 (exp)
330 Conflict between rule 1 and token OP resolved as reduce (%left OP).
336 ## -------------------------------- ##
337 ## Defaulted Conflicted Reduction. ##
338 ## -------------------------------- ##
340 # When there are RR conflicts, some rules are disabled. Usually it is
341 # simply displayed as:
343 # $end reduce using rule 3 (num)
344 # $end [reduce using rule 4 (id)]
346 # But when `reduce 3' is the default action, we'd produce:
348 # $end [reduce using rule 4 (id)]
349 # $default reduce using rule 3 (num)
351 # In this precise case (a reduction is masked by the default
352 # reduction), we make the `reduce 3' explicit:
354 # $end reduce using rule 3 (num)
355 # $end [reduce using rule 4 (id)]
356 # $default reduce using rule 3 (num)
358 # Maybe that's not the best display, but then, please propose something
361 AT_SETUP([Defaulted Conflicted Reduction])
362 AT_KEYWORDS([report])
372 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
373 [[input.y: conflicts: 1 reduce/reduce
374 input.y:4.6-8: warning: rule never reduced because of conflicts: id: '0'
377 # Check the contents of the report.
378 AT_CHECK([cat input.output], [],
379 [[Rules never reduced
384 State 1 conflicts: 1 reduce/reduce
399 Terminals, with rules where they appear
406 Nonterminals, with rules where they appear
411 on left: 1 2, on right: 0
413 on left: 3, on right: 1
415 on left: 4, on right: 2
420 0 $accept: . exp $end
426 '0' shift, and go to state 1
438 $end reduce using rule 3 (num)
439 $end [reduce using rule 4 (id)]
440 $default reduce using rule 3 (num)
445 0 $accept: exp . $end
447 $end shift, and go to state 5
454 $default reduce using rule 1 (exp)
461 $default reduce using rule 2 (exp)
466 0 $accept: exp $end .
476 ## -------------------- ##
477 ## %expect not enough. ##
478 ## -------------------- ##
480 AT_SETUP([%expect not enough])
486 exp: exp OP exp | NUM;
489 AT_CHECK([bison -o input.c input.y], 0, [],
490 [input.y: conflicts: 1 shift/reduce
491 input.y: warning: expected 0 shift/reduce conflicts
496 ## --------------- ##
498 ## --------------- ##
500 AT_SETUP([%expect right])
506 exp: exp OP exp | NUM;
509 AT_CHECK([bison -o input.c input.y])
513 ## ------------------ ##
514 ## %expect too much. ##
515 ## ------------------ ##
517 AT_SETUP([%expect too much])
523 exp: exp OP exp | NUM;
526 AT_CHECK([bison -o input.c input.y], 0, [],
527 [input.y: conflicts: 1 shift/reduce
528 input.y: warning: expected 2 shift/reduce conflicts
533 ## ------------------------------ ##
534 ## %expect with reduce conflicts ##
535 ## ------------------------------ ##
537 AT_SETUP([%expect with reduce conflicts])
542 program: a 'a' | a a;
546 AT_CHECK([bison -o input.c input.y], 0, [],
547 [input.y: conflicts: 1 reduce/reduce
548 input.y: warning: expected 0 reduce/reduce conflicts
553 ## ------------------------------- ##
554 ## %no-default-prec without %prec ##
555 ## ------------------------------- ##
557 AT_SETUP([%no-default-prec without %prec])
573 AT_CHECK([bison -o input.c input.y], 0, [],
574 [[input.y: conflicts: 4 shift/reduce
579 ## ---------------------------- ##
580 ## %no-default-prec with %prec ##
581 ## ---------------------------- ##
583 AT_SETUP([%no-default-prec with %prec])
599 AT_CHECK([bison -o input.c input.y])
603 ## ---------------- ##
605 ## ---------------- ##
607 AT_SETUP([%default-prec])
623 AT_CHECK([bison -o input.c input.y])