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],
61 #define YYERROR_VERBOSE 1
63 yyerror (const char *msg)
65 fprintf (stderr, "%s\n", msg);
68 /* The current argument. */
69 static const char *input;
75 if (! (toknum <= strlen (input)))
77 return input[toknum++];
91 main (int argc, const char *argv[])
93 input = argc <= 1 ? "" : argv[1];
98 # Specify the output files to avoid problems on different file systems.
99 AT_CHECK([bison -o input.c input.y])
102 AT_PARSER_CHECK([./input '0<0'])
103 # FIXME: This is an actual bug, but a new one, in the sense that
104 # no one has ever spotted it! The messages are *wrong*: there should
105 # be nothing there, it should be expected eof.
106 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
107 [syntax error, unexpected '<', expecting '<' or '>'
110 AT_PARSER_CHECK([./input '0>0'])
111 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
112 [syntax error, unexpected '>', expecting '<' or '>'
115 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
116 [syntax error, unexpected '>', expecting '<' or '>'
123 ## ------------------------- ##
124 ## Unresolved SR Conflicts. ##
125 ## ------------------------- ##
127 AT_SETUP([Unresolved SR Conflicts])
129 AT_KEYWORDS([report])
134 exp: exp OP exp | NUM;
137 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
138 [input.y: conflicts: 1 shift/reduce
141 # Check the contents of the report.
142 AT_CHECK([cat input.output], [],
143 [[State 5 conflicts: 1 shift/reduce
154 Terminals, with rules where they appear
162 Nonterminals, with rules where they appear
167 on left: 1 2, on right: 0 1
172 0 $accept: . exp $end
176 NUM shift, and go to state 1
185 $default reduce using rule 2 (exp)
190 0 $accept: exp . $end
193 $end shift, and go to state 3
194 OP shift, and go to state 4
199 0 $accept: exp $end .
210 NUM shift, and go to state 1
217 1 exp: exp . OP exp [$end, OP]
218 1 | exp OP exp . [$end, OP]
220 OP shift, and go to state 4
222 OP [reduce using rule 1 (exp)]
223 $default reduce using rule 1 (exp)
230 ## ----------------------- ##
231 ## Resolved SR Conflicts. ##
232 ## ----------------------- ##
234 AT_SETUP([Resolved SR Conflicts])
236 AT_KEYWORDS([report])
242 exp: exp OP exp | NUM;
245 AT_CHECK([bison -o input.c --report=all input.y])
247 # Check the contents of the report.
248 AT_CHECK([cat input.output], [],
257 Terminals, with rules where they appear
265 Nonterminals, with rules where they appear
270 on left: 1 2, on right: 0 1
275 0 $accept: . exp $end
279 NUM shift, and go to state 1
288 $default reduce using rule 2 (exp)
293 0 $accept: exp . $end
296 $end shift, and go to state 3
297 OP shift, and go to state 4
302 0 $accept: exp $end .
313 NUM shift, and go to state 1
320 1 exp: exp . OP exp [$end, OP]
321 1 | exp OP exp . [$end, OP]
323 $default reduce using rule 1 (exp)
325 Conflict between rule 1 and token OP resolved as reduce (%left OP).
331 ## -------------------------------- ##
332 ## Defaulted Conflicted Reduction. ##
333 ## -------------------------------- ##
335 # When there are RR conflicts, some rules are disabled. Usually it is
336 # simply displayed as:
338 # $end reduce using rule 3 (num)
339 # $end [reduce using rule 4 (id)]
341 # But when `reduce 3' is the default action, we'd produce:
343 # $end [reduce using rule 4 (id)]
344 # $default reduce using rule 3 (num)
346 # In this precise case (a reduction is masked by the default
347 # reduction), we make the `reduce 3' explicit:
349 # $end reduce using rule 3 (num)
350 # $end [reduce using rule 4 (id)]
351 # $default reduce using rule 3 (num)
353 # Maybe that's not the best display, but then, please propose something
356 AT_SETUP([Defaulted Conflicted Reduction])
357 AT_KEYWORDS([report])
367 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
368 [[input.y: conflicts: 1 reduce/reduce
369 input.y:4.6-8: warning: rule never reduced because of conflicts: id: '0'
372 # Check the contents of the report.
373 AT_CHECK([cat input.output], [],
374 [[Rules never reduced
379 State 1 conflicts: 1 reduce/reduce
394 Terminals, with rules where they appear
401 Nonterminals, with rules where they appear
406 on left: 1 2, on right: 0
408 on left: 3, on right: 1
410 on left: 4, on right: 2
415 0 $accept: . exp $end
421 '0' shift, and go to state 1
433 $end reduce using rule 3 (num)
434 $end [reduce using rule 4 (id)]
435 $default reduce using rule 3 (num)
440 0 $accept: exp . $end
442 $end shift, and go to state 5
449 $default reduce using rule 1 (exp)
456 $default reduce using rule 2 (exp)
461 0 $accept: exp $end .
471 ## -------------------- ##
472 ## %expect not enough. ##
473 ## -------------------- ##
475 AT_SETUP([%expect not enough])
481 exp: exp OP exp | NUM;
484 AT_CHECK([bison -o input.c input.y], 1, [],
485 [input.y: conflicts: 1 shift/reduce
486 input.y: expected 0 shift/reduce conflicts
491 ## --------------- ##
493 ## --------------- ##
495 AT_SETUP([%expect right])
501 exp: exp OP exp | NUM;
504 AT_CHECK([bison -o input.c input.y])
508 ## ------------------ ##
509 ## %expect too much. ##
510 ## ------------------ ##
512 AT_SETUP([%expect too much])
518 exp: exp OP exp | NUM;
521 AT_CHECK([bison -o input.c input.y], 1, [],
522 [input.y: conflicts: 1 shift/reduce
523 input.y: expected 2 shift/reduce conflicts
528 ## ------------------------------ ##
529 ## %expect with reduce conflicts ##
530 ## ------------------------------ ##
532 AT_SETUP([%expect with reduce conflicts])
537 program: a 'a' | a a;
541 AT_CHECK([bison -o input.c input.y], 1, [],
542 [input.y: conflicts: 1 reduce/reduce
543 input.y: expected 0 reduce/reduce conflicts
548 ## ------------------------------- ##
549 ## %no-default-prec without %prec ##
550 ## ------------------------------- ##
552 AT_SETUP([%no-default-prec without %prec])
568 AT_CHECK([bison -o input.c input.y], 0, [],
569 [[input.y: conflicts: 4 shift/reduce
574 ## ---------------------------- ##
575 ## %no-default-prec with %prec ##
576 ## ---------------------------- ##
578 AT_SETUP([%no-default-prec with %prec])
594 AT_CHECK([bison -o input.c input.y])
598 ## ---------------- ##
600 ## ---------------- ##
602 AT_SETUP([%default-prec])
618 AT_CHECK([bison -o input.c input.y])