]> git.saurik.com Git - bison.git/blame - tests/conflicts.at
(%expect with reduce conflicts): New test.
[bison.git] / tests / conflicts.at
CommitLineData
3c31a486 1# Exercising Bison on conflicts. -*- Autotest -*-
643a5994 2# Copyright (C) 2002 Free Software Foundation, Inc.
3c31a486
AD
3
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)
7# any later version.
8
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.
13
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
17# 02111-1307, USA.
18
19AT_BANNER([[Conflicts.]])
20
21
643a5994
AD
22## ---------------- ##
23## S/R in initial. ##
24## ---------------- ##
25
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.
30
31AT_SETUP([S/R in initial])
32
33AT_DATA([[input.y]],
34[[%expect 1
35%%
36exp: e 'e';
37e: 'e' | /* Nothing. */;
38]])
39
b56471a6 40AT_CHECK([bison -o input.c input.y], 0, [],
2bf21a83 41[[input.y:4.9: warning: rule never reduced because of conflicts: e: /* empty */
e8832397 42]])
643a5994
AD
43
44AT_CLEANUP
45
bc933ef1 46
3c31a486
AD
47## ------------------- ##
48## %nonassoc and eof. ##
49## ------------------- ##
50
51AT_SETUP([%nonassoc and eof])
52
9501dc6e 53AT_DATA_GRAMMAR([input.y],
3c31a486
AD
54[[
55%{
56#include <stdio.h>
1207eeac
AD
57
58#if STDC_HEADERS
59# include <stdlib.h>
60#endif
61
3c31a486 62#define YYERROR_VERBOSE 1
1207eeac
AD
63static void
64yyerror (const char *msg)
65{
66 fprintf (stderr, "%s\n", msg);
67 exit (1);
68}
3c31a486
AD
69
70/* The current argument. */
71static const char *input = NULL;
72
73static int
74yylex (void)
75{
76 /* No token stands for end of file. */
77 if (input && *input)
78 return *input++;
79 else
80 return 0;
81}
82
83%}
84
85%nonassoc '<' '>'
86
87%%
88expr: expr '<' expr
89 | expr '>' expr
90 | '0'
91 ;
92%%
93int
94main (int argc, const char *argv[])
95{
96 if (argc > 1)
97 input = argv[1];
98 return yyparse ();
99}
100]])
101
102# Specify the output files to avoid problems on different file systems.
b56471a6 103AT_CHECK([bison -o input.c input.y])
1154cced 104AT_COMPILE([input])
3c31a486 105
1154cced 106AT_PARSER_CHECK([./input '0<0'])
3c31a486
AD
107# FIXME: This is an actual bug, but a new one, in the sense that
108# no one has ever spotted it! The messages are *wrong*: there should
109# be nothing there, it should be expected eof.
1154cced 110AT_PARSER_CHECK([./input '0<0<0'], [1], [],
6e649e65 111 [syntax error, unexpected '<', expecting '<' or '>'
3c31a486
AD
112])
113
1154cced
AD
114AT_PARSER_CHECK([./input '0>0'])
115AT_PARSER_CHECK([./input '0>0>0'], [1], [],
6e649e65 116 [syntax error, unexpected '>', expecting '<' or '>'
3c31a486
AD
117])
118
1154cced 119AT_PARSER_CHECK([./input '0<0>0'], [1], [],
6e649e65 120 [syntax error, unexpected '>', expecting '<' or '>'
3c31a486
AD
121])
122
123AT_CLEANUP
124
125
126
127## ------------------------- ##
128## Unresolved SR Conflicts. ##
129## ------------------------- ##
130
131AT_SETUP([Unresolved SR Conflicts])
132
6b98e4b5
AD
133AT_KEYWORDS([report])
134
3c31a486
AD
135AT_DATA([input.y],
136[[%token NUM OP
137%%
138exp: exp OP exp | NUM;
139]])
140
b56471a6 141AT_CHECK([bison -o input.c --report=all input.y], 0, [],
52489d44 142[input.y: warning: 1 shift/reduce conflict
3c31a486
AD
143])
144
145# Check the contents of the report.
146AT_CHECK([cat input.output], [],
147[[State 5 contains 1 shift/reduce conflict.
148
149
150Grammar
151
88bce5a2 152 0 $accept: exp $end
6b98e4b5
AD
153
154 1 exp: exp OP exp
155 2 | NUM
3c31a486
AD
156
157
158Terminals, with rules where they appear
159
88bce5a2 160$end (0) 0
3c31a486 161error (256)
007a50a4
AD
162NUM (258) 2
163OP (259) 1
3c31a486
AD
164
165
166Nonterminals, with rules where they appear
167
88bce5a2 168$accept (5)
3c31a486
AD
169 on left: 0
170exp (6)
171 on left: 1 2, on right: 0 1
172
173
174state 0
175
88bce5a2 176 0 $accept: . exp $end
ce4ccb4b
AD
177 1 exp: . exp OP exp
178 2 | . NUM
643a5994 179
87675353 180 NUM shift, and go to state 1
3c31a486 181
87675353 182 exp go to state 2
3c31a486
AD
183
184
185state 1
186
ce4ccb4b 187 2 exp: NUM .
3c31a486 188
87675353 189 $default reduce using rule 2 (exp)
3c31a486
AD
190
191
192state 2
193
88bce5a2 194 0 $accept: exp . $end
ce4ccb4b 195 1 exp: exp . OP exp
3c31a486 196
88bce5a2
AD
197 $end shift, and go to state 3
198 OP shift, and go to state 4
3c31a486
AD
199
200
201state 3
202
88bce5a2 203 0 $accept: exp $end .
3c31a486 204
e8832397 205 $default accept
3c31a486
AD
206
207
208state 4
209
ce4ccb4b
AD
210 1 exp: . exp OP exp
211 1 | exp OP . exp
212 2 | . NUM
3c31a486 213
87675353 214 NUM shift, and go to state 1
3c31a486 215
87675353 216 exp go to state 5
3c31a486
AD
217
218
219state 5
220
88bce5a2
AD
221 1 exp: exp . OP exp [$end, OP]
222 1 | exp OP exp . [$end, OP]
3c31a486 223
87675353 224 OP shift, and go to state 4
3c31a486 225
87675353
AD
226 OP [reduce using rule 1 (exp)]
227 $default reduce using rule 1 (exp)
3c31a486
AD
228]])
229
230AT_CLEANUP
231
232
3c31a486 233
ce4ccb4b
AD
234## ----------------------- ##
235## Resolved SR Conflicts. ##
236## ----------------------- ##
237
238AT_SETUP([Resolved SR Conflicts])
3c31a486 239
6b98e4b5
AD
240AT_KEYWORDS([report])
241
3c31a486
AD
242AT_DATA([input.y],
243[[%token NUM OP
ce4ccb4b 244%left OP
3c31a486
AD
245%%
246exp: exp OP exp | NUM;
247]])
248
b56471a6 249AT_CHECK([bison -o input.c --report=all input.y])
3c31a486
AD
250
251# Check the contents of the report.
252AT_CHECK([cat input.output], [],
ce4ccb4b 253[[Grammar
3c31a486 254
88bce5a2 255 0 $accept: exp $end
6b98e4b5
AD
256
257 1 exp: exp OP exp
258 2 | NUM
3c31a486
AD
259
260
261Terminals, with rules where they appear
262
88bce5a2 263$end (0) 0
3c31a486 264error (256)
007a50a4
AD
265NUM (258) 2
266OP (259) 1
3c31a486
AD
267
268
269Nonterminals, with rules where they appear
270
88bce5a2 271$accept (5)
3c31a486
AD
272 on left: 0
273exp (6)
274 on left: 1 2, on right: 0 1
275
276
277state 0
278
88bce5a2 279 0 $accept: . exp $end
ce4ccb4b
AD
280 1 exp: . exp OP exp
281 2 | . NUM
643a5994 282
87675353 283 NUM shift, and go to state 1
3c31a486 284
87675353 285 exp go to state 2
3c31a486
AD
286
287
288state 1
289
ce4ccb4b 290 2 exp: NUM .
3c31a486 291
87675353 292 $default reduce using rule 2 (exp)
3c31a486
AD
293
294
295state 2
296
88bce5a2 297 0 $accept: exp . $end
ce4ccb4b 298 1 exp: exp . OP exp
3c31a486 299
88bce5a2
AD
300 $end shift, and go to state 3
301 OP shift, and go to state 4
3c31a486
AD
302
303
304state 3
305
88bce5a2 306 0 $accept: exp $end .
3c31a486 307
e8832397 308 $default accept
3c31a486
AD
309
310
311state 4
312
ce4ccb4b
AD
313 1 exp: . exp OP exp
314 1 | exp OP . exp
315 2 | . NUM
3c31a486 316
87675353 317 NUM shift, and go to state 1
3c31a486 318
87675353 319 exp go to state 5
3c31a486
AD
320
321
322state 5
323
88bce5a2
AD
324 1 exp: exp . OP exp [$end, OP]
325 1 | exp OP exp . [$end, OP]
3c31a486 326
87675353 327 $default reduce using rule 1 (exp)
7ea9a33f 328
4b3d3a8e 329 Conflict between rule 1 and token OP resolved as reduce (%left OP).
bc933ef1
AD
330]])
331
332AT_CLEANUP
333
334
bc933ef1
AD
335## -------------------------------- ##
336## Defaulted Conflicted Reduction. ##
337## -------------------------------- ##
338
339# When there are RR conflicts, some rules are disabled. Usually it is
340# simply displayed as:
341#
88bce5a2
AD
342# $end reduce using rule 3 (num)
343# $end [reduce using rule 4 (id)]
bc933ef1
AD
344#
345# But when `reduce 3' is the default action, we'd produce:
346#
88bce5a2 347# $end [reduce using rule 4 (id)]
bc933ef1
AD
348# $default reduce using rule 3 (num)
349#
350# In this precise case (a reduction is masked by the default
351# reduction), we make the `reduce 3' explicit:
352#
88bce5a2
AD
353# $end reduce using rule 3 (num)
354# $end [reduce using rule 4 (id)]
bc933ef1
AD
355# $default reduce using rule 3 (num)
356#
357# Maybe that's not the best display, but then, please propose something
358# else.
359
360AT_SETUP([Defaulted Conflicted Reduction])
361AT_KEYWORDS([report])
362
363AT_DATA([input.y],
364[[%%
365exp: num | id;
366num: '0';
367id : '0';
368%%
369]])
370
b56471a6 371AT_CHECK([bison -o input.c --report=all input.y], 0, [],
52489d44 372[[input.y: warning: 1 reduce/reduce conflict
2bf21a83 373input.y:4.6-8: warning: rule never reduced because of conflicts: id: '0'
e8832397 374]])
bc933ef1
AD
375
376# Check the contents of the report.
377AT_CHECK([cat input.output], [],
c8f002c7
AD
378[[Rules never reduced
379
380 4 id: '0'
381
382
383State 1 contains 1 reduce/reduce conflict.
bc933ef1
AD
384
385
386Grammar
387
88bce5a2 388 0 $accept: exp $end
bc933ef1
AD
389
390 1 exp: num
391 2 | id
392
393 3 num: '0'
394
395 4 id: '0'
396
397
398Terminals, with rules where they appear
399
88bce5a2 400$end (0) 0
bc933ef1
AD
401'0' (48) 3 4
402error (256)
403
404
405Nonterminals, with rules where they appear
406
88bce5a2 407$accept (4)
bc933ef1
AD
408 on left: 0
409exp (5)
410 on left: 1 2, on right: 0
411num (6)
412 on left: 3, on right: 1
413id (7)
414 on left: 4, on right: 2
415
416
417state 0
418
88bce5a2 419 0 $accept: . exp $end
ce4ccb4b
AD
420 1 exp: . num
421 2 | . id
422 3 num: . '0'
423 4 id: . '0'
bc933ef1 424
87675353 425 '0' shift, and go to state 1
bc933ef1 426
87675353
AD
427 exp go to state 2
428 num go to state 3
429 id go to state 4
bc933ef1
AD
430
431
432state 1
433
88bce5a2
AD
434 3 num: '0' . [$end]
435 4 id: '0' . [$end]
bc933ef1 436
88bce5a2
AD
437 $end reduce using rule 3 (num)
438 $end [reduce using rule 4 (id)]
87675353 439 $default reduce using rule 3 (num)
bc933ef1
AD
440
441
442state 2
443
88bce5a2 444 0 $accept: exp . $end
bc933ef1 445
88bce5a2 446 $end shift, and go to state 5
bc933ef1
AD
447
448
449state 3
450
ce4ccb4b 451 1 exp: num .
bc933ef1 452
87675353 453 $default reduce using rule 1 (exp)
bc933ef1
AD
454
455
456state 4
457
ce4ccb4b 458 2 exp: id .
bc933ef1 459
87675353 460 $default reduce using rule 2 (exp)
bc933ef1
AD
461
462
463state 5
464
88bce5a2 465 0 $accept: exp $end .
bc933ef1 466
e8832397 467 $default accept
3c31a486
AD
468]])
469
470AT_CLEANUP
471
472
473
474
475## -------------------- ##
476## %expect not enough. ##
477## -------------------- ##
478
479AT_SETUP([%expect not enough])
480
481AT_DATA([input.y],
482[[%token NUM OP
483%expect 0
484%%
485exp: exp OP exp | NUM;
486]])
487
b56471a6 488AT_CHECK([bison -o input.c input.y], 1, [],
52489d44
AD
489[input.y: warning: 1 shift/reduce conflict
490input.y: expected 0 shift/reduce conflicts
3c31a486
AD
491])
492AT_CLEANUP
493
494
495## --------------- ##
496## %expect right. ##
497## --------------- ##
498
499AT_SETUP([%expect right])
500
501AT_DATA([input.y],
502[[%token NUM OP
503%expect 1
504%%
505exp: exp OP exp | NUM;
506]])
507
b56471a6 508AT_CHECK([bison -o input.c input.y])
3c31a486
AD
509AT_CLEANUP
510
511
512## ------------------ ##
513## %expect too much. ##
514## ------------------ ##
515
516AT_SETUP([%expect too much])
517
518AT_DATA([input.y],
519[[%token NUM OP
520%expect 2
521%%
522exp: exp OP exp | NUM;
523]])
524
b56471a6 525AT_CHECK([bison -o input.c input.y], 1, [],
52489d44
AD
526[input.y: warning: 1 shift/reduce conflict
527input.y: expected 2 shift/reduce conflicts
3c31a486
AD
528])
529AT_CLEANUP
6876ecd3
PE
530
531
532## ------------------------------ ##
533## %expect with reduce conflicts ##
534## ------------------------------ ##
535
536AT_SETUP([%expect with reduce conflicts])
537
538AT_DATA([input.y],
539[[%expect 0
540%%
541program: a 'a' | a a;
542a: 'a';
543]])
544
545AT_CHECK([bison -o input.c input.y], 1, [],
546[input.y: warning: 1 reduce/reduce conflict
547input.y: expected 0 reduce/reduce conflicts
548])
549AT_CLEANUP