1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002, 2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor, 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],
60 #define YYERROR_VERBOSE 1
62 yyerror (const char *msg)
64 fprintf (stderr, "%s\n", msg);
67 /* The current argument. */
68 static const char *input = NULL;
73 /* No token stands for end of file. */
91 main (int argc, const char *argv[])
99 # Specify the output files to avoid problems on different file systems.
100 AT_CHECK([bison -o input.c input.y])
103 AT_PARSER_CHECK([./input '0<0'])
104 # FIXME: This is an actual bug, but a new one, in the sense that
105 # no one has ever spotted it! The messages are *wrong*: there should
106 # be nothing there, it should be expected eof.
107 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
108 [syntax error, unexpected '<', expecting '<' or '>'
111 AT_PARSER_CHECK([./input '0>0'])
112 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
113 [syntax error, unexpected '>', expecting '<' or '>'
116 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
117 [syntax error, unexpected '>', expecting '<' or '>'
124 ## ------------------------- ##
125 ## Unresolved SR Conflicts. ##
126 ## ------------------------- ##
128 AT_SETUP([Unresolved SR Conflicts])
130 AT_KEYWORDS([report])
135 exp: exp OP exp | NUM;
138 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
139 [input.y: conflicts: 1 shift/reduce
142 # Check the contents of the report.
143 AT_CHECK([cat input.output], [],
144 [[State 5 conflicts: 1 shift/reduce
155 Terminals, with rules where they appear
163 Nonterminals, with rules where they appear
168 on left: 1 2, on right: 0 1
173 0 $accept: . exp $end
177 NUM shift, and go to state 1
186 $default reduce using rule 2 (exp)
191 0 $accept: exp . $end
194 $end shift, and go to state 3
195 OP shift, and go to state 4
200 0 $accept: exp $end .
211 NUM shift, and go to state 1
218 1 exp: exp . OP exp [$end, OP]
219 1 | exp OP exp . [$end, OP]
221 OP shift, and go to state 4
223 OP [reduce using rule 1 (exp)]
224 $default reduce using rule 1 (exp)
231 ## ----------------------- ##
232 ## Resolved SR Conflicts. ##
233 ## ----------------------- ##
235 AT_SETUP([Resolved SR Conflicts])
237 AT_KEYWORDS([report])
243 exp: exp OP exp | NUM;
246 AT_CHECK([bison -o input.c --report=all input.y])
248 # Check the contents of the report.
249 AT_CHECK([cat input.output], [],
258 Terminals, with rules where they appear
266 Nonterminals, with rules where they appear
271 on left: 1 2, on right: 0 1
276 0 $accept: . exp $end
280 NUM shift, and go to state 1
289 $default reduce using rule 2 (exp)
294 0 $accept: exp . $end
297 $end shift, and go to state 3
298 OP shift, and go to state 4
303 0 $accept: exp $end .
314 NUM shift, and go to state 1
321 1 exp: exp . OP exp [$end, OP]
322 1 | exp OP exp . [$end, OP]
324 $default reduce using rule 1 (exp)
326 Conflict between rule 1 and token OP resolved as reduce (%left OP).
332 ## -------------------------------- ##
333 ## Defaulted Conflicted Reduction. ##
334 ## -------------------------------- ##
336 # When there are RR conflicts, some rules are disabled. Usually it is
337 # simply displayed as:
339 # $end reduce using rule 3 (num)
340 # $end [reduce using rule 4 (id)]
342 # But when `reduce 3' is the default action, we'd produce:
344 # $end [reduce using rule 4 (id)]
345 # $default reduce using rule 3 (num)
347 # In this precise case (a reduction is masked by the default
348 # reduction), we make the `reduce 3' explicit:
350 # $end reduce using rule 3 (num)
351 # $end [reduce using rule 4 (id)]
352 # $default reduce using rule 3 (num)
354 # Maybe that's not the best display, but then, please propose something
357 AT_SETUP([Defaulted Conflicted Reduction])
358 AT_KEYWORDS([report])
368 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
369 [[input.y: conflicts: 1 reduce/reduce
370 input.y:4.6-8: warning: rule never reduced because of conflicts: id: '0'
373 # Check the contents of the report.
374 AT_CHECK([cat input.output], [],
375 [[Rules never reduced
380 State 1 conflicts: 1 reduce/reduce
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 -o input.c input.y], 1, [],
486 [input.y: conflicts: 1 shift/reduce
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 -o input.c input.y])
509 ## ------------------ ##
510 ## %expect too much. ##
511 ## ------------------ ##
513 AT_SETUP([%expect too much])
519 exp: exp OP exp | NUM;
522 AT_CHECK([bison -o input.c input.y], 1, [],
523 [input.y: conflicts: 1 shift/reduce
524 input.y: expected 2 shift/reduce conflicts
529 ## ------------------------------ ##
530 ## %expect with reduce conflicts ##
531 ## ------------------------------ ##
533 AT_SETUP([%expect with reduce conflicts])
538 program: a 'a' | a a;
542 AT_CHECK([bison -o input.c input.y], 1, [],
543 [input.y: conflicts: 1 reduce/reduce
544 input.y: expected 0 reduce/reduce conflicts
549 ## ------------------------------- ##
550 ## %no-default-prec without %prec ##
551 ## ------------------------------- ##
553 AT_SETUP([%no-default-prec without %prec])
569 AT_CHECK([bison -o input.c input.y], 0, [],
570 [[input.y: conflicts: 4 shift/reduce
575 ## ---------------------------- ##
576 ## %no-default-prec with %prec ##
577 ## ---------------------------- ##
579 AT_SETUP([%no-default-prec with %prec])
595 AT_CHECK([bison -o input.c input.y])
599 ## ---------------- ##
601 ## ---------------- ##
603 AT_SETUP([%default-prec])
619 AT_CHECK([bison -o input.c input.y])