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 input.y -o input.c])
45 ## ------------------- ##
46 ## %nonassoc and eof. ##
47 ## ------------------- ##
49 AT_SETUP([%nonassoc and eof])
55 /* We don't need a perfect malloc for these tests. */
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 input.y -o input.c])
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 [parse error, unexpected '<', expecting '<' or '>'
115 AT_PARSER_CHECK([./input '0>0'])
116 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
117 [parse error, unexpected '>', expecting '<' or '>'
120 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
121 [parse 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 input.y -o input.c --report=all], 0, [],
143 [input.y contains 1 shift/reduce conflict.
146 # Check the contents of the report.
147 AT_CHECK([cat input.output], [],
148 [[State 5 contains 1 shift/reduce conflict.
159 Terminals, with rules where they appear
167 Nonterminals, with rules where they appear
172 on left: 1 2, on right: 0 1
181 NUM shift, and go to state 1
190 $default reduce using rule 2 (exp)
198 $ shift, and go to state 3
199 OP shift, and go to state 4
215 NUM shift, and go to state 1
222 1 exp: exp . OP exp [$, OP]
223 1 | exp OP exp . [$, 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 input.y -o input.c --report=all])
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
284 NUM shift, and go to state 1
293 $default reduce using rule 2 (exp)
301 $ shift, and go to state 3
302 OP shift, and go to state 4
318 NUM shift, and go to state 1
325 1 exp: exp . OP exp [$, OP]
326 1 | exp OP exp . [$, OP]
328 $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 # $ reduce using rule 3 (num)
343 # $ [reduce using rule 4 (id)]
345 # But when `reduce 3' is the default action, we'd produce:
347 # $ [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 # $ reduce using rule 3 (num)
354 # $ [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 input.y -o input.c --report=all], 0, [],
372 [input.y contains 1 reduce/reduce conflict.
375 # Check the contents of the report.
376 AT_CHECK([cat input.output], [],
377 [[State 1 contains 1 reduce/reduce conflict.
392 Terminals, with rules where they appear
399 Nonterminals, with rules where they appear
404 on left: 1 2, on right: 0
406 on left: 3, on right: 1
408 on left: 4, on right: 2
419 '0' shift, and go to state 1
431 $ reduce using rule 3 (num)
432 $ [reduce using rule 4 (id)]
433 $default reduce using rule 3 (num)
440 $ shift, and go to state 5
447 $default reduce using rule 1 (exp)
454 $default reduce using rule 2 (exp)
469 ## -------------------- ##
470 ## %expect not enough. ##
471 ## -------------------- ##
473 AT_SETUP([%expect not enough])
479 exp: exp OP exp | NUM;
482 AT_CHECK([bison input.y -o input.c], 1, [],
483 [input.y contains 1 shift/reduce conflict.
484 expected 0 shift/reduce conflicts
489 ## --------------- ##
491 ## --------------- ##
493 AT_SETUP([%expect right])
499 exp: exp OP exp | NUM;
502 AT_CHECK([bison input.y -o input.c])
506 ## ------------------ ##
507 ## %expect too much. ##
508 ## ------------------ ##
510 AT_SETUP([%expect too much])
516 exp: exp OP exp | NUM;
519 AT_CHECK([bison input.y -o input.c], 1, [],
520 [input.y contains 1 shift/reduce conflict.
521 expected 2 shift/reduce conflicts