1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002, 2003, 2004 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],
60 #define YYERROR_VERBOSE 1
62 yyerror (const char *msg)
64 fprintf (stderr, "%s\n", msg);
68 /* The current argument. */
69 static const char *input = NULL;
74 /* No token stands for end of file. */
92 main (int argc, const char *argv[])
100 # Specify the output files to avoid problems on different file systems.
101 AT_CHECK([bison -o input.c input.y])
104 AT_PARSER_CHECK([./input '0<0'])
105 # FIXME: This is an actual bug, but a new one, in the sense that
106 # no one has ever spotted it! The messages are *wrong*: there should
107 # be nothing there, it should be expected eof.
108 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
109 [syntax error, unexpected '<', expecting '<' or '>'
112 AT_PARSER_CHECK([./input '0>0'])
113 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
114 [syntax error, unexpected '>', expecting '<' or '>'
117 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
118 [syntax error, unexpected '>', expecting '<' or '>'
125 ## ------------------------- ##
126 ## Unresolved SR Conflicts. ##
127 ## ------------------------- ##
129 AT_SETUP([Unresolved SR Conflicts])
131 AT_KEYWORDS([report])
136 exp: exp OP exp | NUM;
139 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
140 [input.y: conflicts: 1 shift/reduce
143 # Check the contents of the report.
144 AT_CHECK([cat input.output], [],
145 [[State 5 conflicts: 1 shift/reduce
156 Terminals, with rules where they appear
164 Nonterminals, with rules where they appear
169 on left: 1 2, on right: 0 1
174 0 $accept: . exp $end
178 NUM shift, and go to state 1
187 $default reduce using rule 2 (exp)
192 0 $accept: exp . $end
195 $end shift, and go to state 3
196 OP shift, and go to state 4
201 0 $accept: exp $end .
212 NUM shift, and go to state 1
219 1 exp: exp . OP exp [$end, OP]
220 1 | exp OP exp . [$end, OP]
222 OP shift, and go to state 4
224 OP [reduce using rule 1 (exp)]
225 $default reduce using rule 1 (exp)
232 ## ----------------------- ##
233 ## Resolved SR Conflicts. ##
234 ## ----------------------- ##
236 AT_SETUP([Resolved SR Conflicts])
238 AT_KEYWORDS([report])
244 exp: exp OP exp | NUM;
247 AT_CHECK([bison -o input.c --report=all input.y])
249 # Check the contents of the report.
250 AT_CHECK([cat input.output], [],
259 Terminals, with rules where they appear
267 Nonterminals, with rules where they appear
272 on left: 1 2, on right: 0 1
277 0 $accept: . exp $end
281 NUM shift, and go to state 1
290 $default reduce using rule 2 (exp)
295 0 $accept: exp . $end
298 $end shift, and go to state 3
299 OP shift, and go to state 4
304 0 $accept: exp $end .
315 NUM shift, and go to state 1
322 1 exp: exp . OP exp [$end, OP]
323 1 | exp OP exp . [$end, OP]
325 $default reduce using rule 1 (exp)
327 Conflict between rule 1 and token OP resolved as reduce (%left OP).
333 ## -------------------------------- ##
334 ## Defaulted Conflicted Reduction. ##
335 ## -------------------------------- ##
337 # When there are RR conflicts, some rules are disabled. Usually it is
338 # simply displayed as:
340 # $end reduce using rule 3 (num)
341 # $end [reduce using rule 4 (id)]
343 # But when `reduce 3' is the default action, we'd produce:
345 # $end [reduce using rule 4 (id)]
346 # $default reduce using rule 3 (num)
348 # In this precise case (a reduction is masked by the default
349 # reduction), we make the `reduce 3' explicit:
351 # $end reduce using rule 3 (num)
352 # $end [reduce using rule 4 (id)]
353 # $default reduce using rule 3 (num)
355 # Maybe that's not the best display, but then, please propose something
358 AT_SETUP([Defaulted Conflicted Reduction])
359 AT_KEYWORDS([report])
369 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
370 [[input.y: conflicts: 1 reduce/reduce
371 input.y:4.6-8: warning: rule never reduced because of conflicts: id: '0'
374 # Check the contents of the report.
375 AT_CHECK([cat input.output], [],
376 [[Rules never reduced
381 State 1 conflicts: 1 reduce/reduce
396 Terminals, with rules where they appear
403 Nonterminals, with rules where they appear
408 on left: 1 2, on right: 0
410 on left: 3, on right: 1
412 on left: 4, on right: 2
417 0 $accept: . exp $end
423 '0' shift, and go to state 1
435 $end reduce using rule 3 (num)
436 $end [reduce using rule 4 (id)]
437 $default reduce using rule 3 (num)
442 0 $accept: exp . $end
444 $end shift, and go to state 5
451 $default reduce using rule 1 (exp)
458 $default reduce using rule 2 (exp)
463 0 $accept: exp $end .
473 ## -------------------- ##
474 ## %expect not enough. ##
475 ## -------------------- ##
477 AT_SETUP([%expect not enough])
483 exp: exp OP exp | NUM;
486 AT_CHECK([bison -o input.c input.y], 0, [],
487 [input.y: conflicts: 1 shift/reduce
488 input.y: warning: expected 0 shift/reduce conflicts
493 ## --------------- ##
495 ## --------------- ##
497 AT_SETUP([%expect right])
503 exp: exp OP exp | NUM;
506 AT_CHECK([bison -o input.c input.y])
510 ## ------------------ ##
511 ## %expect too much. ##
512 ## ------------------ ##
514 AT_SETUP([%expect too much])
520 exp: exp OP exp | NUM;
523 AT_CHECK([bison -o input.c input.y], 0, [],
524 [input.y: conflicts: 1 shift/reduce
525 input.y: warning: expected 2 shift/reduce conflicts
530 ## ------------------------------ ##
531 ## %expect with reduce conflicts ##
532 ## ------------------------------ ##
534 AT_SETUP([%expect with reduce conflicts])
539 program: a 'a' | a a;
543 AT_CHECK([bison -o input.c input.y], 0, [],
544 [input.y: conflicts: 1 reduce/reduce
545 input.y: warning: expected 0 reduce/reduce conflicts
550 ## ------------------------------- ##
551 ## %no-default-prec without %prec ##
552 ## ------------------------------- ##
554 AT_SETUP([%no-default-prec without %prec])
570 AT_CHECK([bison -o input.c input.y], 0, [],
571 [[input.y: conflicts: 4 shift/reduce
576 ## ---------------------------- ##
577 ## %no-default-prec with %prec ##
578 ## ---------------------------- ##
580 AT_SETUP([%no-default-prec with %prec])
596 AT_CHECK([bison -o input.c input.y])
600 ## ---------------- ##
602 ## ---------------- ##
604 AT_SETUP([%default-prec])
620 AT_CHECK([bison -o input.c input.y])