]> git.saurik.com Git - bison.git/blame - tests/conflicts.at
* src/reduce.c (reduce_grammar): When the language is empty,
[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
40AT_CHECK([bison input.y -o input.c])
41
42AT_CLEANUP
43
bc933ef1 44
3c31a486
AD
45## ------------------- ##
46## %nonassoc and eof. ##
47## ------------------- ##
48
49AT_SETUP([%nonassoc and eof])
50
51AT_DATA([input.y],
52[[
53%{
1207eeac
AD
54#include <config.h>
55/* We don't need a perfect malloc for these tests. */
56#undef malloc
3c31a486 57#include <stdio.h>
1207eeac
AD
58
59#if STDC_HEADERS
60# include <stdlib.h>
61#endif
62
3c31a486 63#define YYERROR_VERBOSE 1
1207eeac
AD
64static void
65yyerror (const char *msg)
66{
67 fprintf (stderr, "%s\n", msg);
68 exit (1);
69}
3c31a486
AD
70
71/* The current argument. */
72static const char *input = NULL;
73
74static int
75yylex (void)
76{
77 /* No token stands for end of file. */
78 if (input && *input)
79 return *input++;
80 else
81 return 0;
82}
83
84%}
85
86%nonassoc '<' '>'
87
88%%
89expr: expr '<' expr
90 | expr '>' expr
91 | '0'
92 ;
93%%
94int
95main (int argc, const char *argv[])
96{
97 if (argc > 1)
98 input = argv[1];
99 return yyparse ();
100}
101]])
102
103# Specify the output files to avoid problems on different file systems.
104AT_CHECK([bison input.y -o input.c])
1154cced 105AT_COMPILE([input])
3c31a486 106
1154cced 107AT_PARSER_CHECK([./input '0<0'])
3c31a486
AD
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.
1154cced 111AT_PARSER_CHECK([./input '0<0<0'], [1], [],
3c31a486
AD
112 [parse error, unexpected '<', expecting '<' or '>'
113])
114
1154cced
AD
115AT_PARSER_CHECK([./input '0>0'])
116AT_PARSER_CHECK([./input '0>0>0'], [1], [],
3c31a486
AD
117 [parse error, unexpected '>', expecting '<' or '>'
118])
119
1154cced 120AT_PARSER_CHECK([./input '0<0>0'], [1], [],
3c31a486
AD
121 [parse error, unexpected '>', expecting '<' or '>'
122])
123
124AT_CLEANUP
125
126
127
128## ------------------------- ##
129## Unresolved SR Conflicts. ##
130## ------------------------- ##
131
132AT_SETUP([Unresolved SR Conflicts])
133
6b98e4b5
AD
134AT_KEYWORDS([report])
135
3c31a486
AD
136AT_DATA([input.y],
137[[%token NUM OP
138%%
139exp: exp OP exp | NUM;
140]])
141
b408954b 142AT_CHECK([bison input.y -o input.c --report=all], 0, [],
3c31a486
AD
143[input.y contains 1 shift/reduce conflict.
144])
145
146# Check the contents of the report.
147AT_CHECK([cat input.output], [],
148[[State 5 contains 1 shift/reduce conflict.
149
150
151Grammar
152
6b98e4b5
AD
153 0 $axiom: exp $
154
155 1 exp: exp OP exp
156 2 | NUM
3c31a486
AD
157
158
159Terminals, with rules where they appear
160
161$ (0) 0
162error (256)
007a50a4
AD
163NUM (258) 2
164OP (259) 1
3c31a486
AD
165
166
167Nonterminals, with rules where they appear
168
169$axiom (5)
170 on left: 0
171exp (6)
172 on left: 1 2, on right: 0 1
173
174
175state 0
176
ce4ccb4b
AD
177 0 $axiom: . exp $
178 1 exp: . exp OP exp
179 2 | . NUM
643a5994 180
87675353 181 NUM shift, and go to state 1
3c31a486 182
87675353 183 exp go to state 2
3c31a486
AD
184
185
186state 1
187
ce4ccb4b 188 2 exp: NUM .
3c31a486 189
87675353 190 $default reduce using rule 2 (exp)
3c31a486
AD
191
192
193state 2
194
ce4ccb4b
AD
195 0 $axiom: exp . $
196 1 exp: exp . OP exp
3c31a486 197
87675353
AD
198 $ shift, and go to state 3
199 OP shift, and go to state 4
3c31a486
AD
200
201
202state 3
203
ce4ccb4b 204 0 $axiom: exp $ .
3c31a486
AD
205
206 $default accept
207
208
209state 4
210
ce4ccb4b
AD
211 1 exp: . exp OP exp
212 1 | exp OP . exp
213 2 | . NUM
3c31a486 214
87675353 215 NUM shift, and go to state 1
3c31a486 216
87675353 217 exp go to state 5
3c31a486
AD
218
219
220state 5
221
ce4ccb4b
AD
222 1 exp: exp . OP exp [$, OP]
223 1 | exp OP exp . [$, OP]
3c31a486 224
87675353 225 OP shift, and go to state 4
3c31a486 226
87675353
AD
227 OP [reduce using rule 1 (exp)]
228 $default reduce using rule 1 (exp)
3c31a486
AD
229]])
230
231AT_CLEANUP
232
233
3c31a486 234
ce4ccb4b
AD
235## ----------------------- ##
236## Resolved SR Conflicts. ##
237## ----------------------- ##
238
239AT_SETUP([Resolved SR Conflicts])
3c31a486 240
6b98e4b5
AD
241AT_KEYWORDS([report])
242
3c31a486
AD
243AT_DATA([input.y],
244[[%token NUM OP
ce4ccb4b 245%left OP
3c31a486
AD
246%%
247exp: exp OP exp | NUM;
248]])
249
ce4ccb4b 250AT_CHECK([bison input.y -o input.c --report=all])
3c31a486
AD
251
252# Check the contents of the report.
253AT_CHECK([cat input.output], [],
ce4ccb4b 254[[Grammar
3c31a486 255
6b98e4b5
AD
256 0 $axiom: exp $
257
258 1 exp: exp OP exp
259 2 | NUM
3c31a486
AD
260
261
262Terminals, with rules where they appear
263
264$ (0) 0
265error (256)
007a50a4
AD
266NUM (258) 2
267OP (259) 1
3c31a486
AD
268
269
270Nonterminals, with rules where they appear
271
272$axiom (5)
273 on left: 0
274exp (6)
275 on left: 1 2, on right: 0 1
276
277
278state 0
279
ce4ccb4b
AD
280 0 $axiom: . exp $
281 1 exp: . exp OP exp
282 2 | . NUM
643a5994 283
87675353 284 NUM shift, and go to state 1
3c31a486 285
87675353 286 exp go to state 2
3c31a486
AD
287
288
289state 1
290
ce4ccb4b 291 2 exp: NUM .
3c31a486 292
87675353 293 $default reduce using rule 2 (exp)
3c31a486
AD
294
295
296state 2
297
ce4ccb4b
AD
298 0 $axiom: exp . $
299 1 exp: exp . OP exp
3c31a486 300
87675353
AD
301 $ shift, and go to state 3
302 OP shift, and go to state 4
3c31a486
AD
303
304
305state 3
306
ce4ccb4b 307 0 $axiom: exp $ .
3c31a486
AD
308
309 $default accept
310
311
312state 4
313
ce4ccb4b
AD
314 1 exp: . exp OP exp
315 1 | exp OP . exp
316 2 | . NUM
3c31a486 317
87675353 318 NUM shift, and go to state 1
3c31a486 319
87675353 320 exp go to state 5
3c31a486
AD
321
322
323state 5
324
ce4ccb4b
AD
325 1 exp: exp . OP exp [$, OP]
326 1 | exp OP exp . [$, OP]
3c31a486 327
87675353 328 $default reduce using rule 1 (exp)
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#
342# $ reduce using rule 3 (num)
343# $ [reduce using rule 4 (id)]
344#
345# But when `reduce 3' is the default action, we'd produce:
346#
347# $ [reduce using rule 4 (id)]
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#
353# $ reduce using rule 3 (num)
354# $ [reduce using rule 4 (id)]
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
bb3f6718 371AT_CHECK([bison input.y -o input.c --report=all], 0, [],
bc933ef1
AD
372[input.y contains 1 reduce/reduce conflict.
373])
374
375# Check the contents of the report.
376AT_CHECK([cat input.output], [],
377[[State 1 contains 1 reduce/reduce conflict.
378
379
380Grammar
381
382 0 $axiom: exp $
383
384 1 exp: num
385 2 | id
386
387 3 num: '0'
388
389 4 id: '0'
390
391
392Terminals, with rules where they appear
393
394$ (0) 0
395'0' (48) 3 4
396error (256)
397
398
399Nonterminals, with rules where they appear
400
401$axiom (4)
402 on left: 0
403exp (5)
404 on left: 1 2, on right: 0
405num (6)
406 on left: 3, on right: 1
407id (7)
408 on left: 4, on right: 2
409
410
411state 0
412
ce4ccb4b
AD
413 0 $axiom: . exp $
414 1 exp: . num
415 2 | . id
416 3 num: . '0'
417 4 id: . '0'
bc933ef1 418
87675353 419 '0' shift, and go to state 1
bc933ef1 420
87675353
AD
421 exp go to state 2
422 num go to state 3
423 id go to state 4
bc933ef1
AD
424
425
426state 1
427
ce4ccb4b
AD
428 3 num: '0' . [$]
429 4 id: '0' . [$]
bc933ef1 430
87675353
AD
431 $ reduce using rule 3 (num)
432 $ [reduce using rule 4 (id)]
433 $default reduce using rule 3 (num)
bc933ef1
AD
434
435
436state 2
437
ce4ccb4b 438 0 $axiom: exp . $
bc933ef1 439
87675353 440 $ shift, and go to state 5
bc933ef1
AD
441
442
443state 3
444
ce4ccb4b 445 1 exp: num .
bc933ef1 446
87675353 447 $default reduce using rule 1 (exp)
bc933ef1
AD
448
449
450state 4
451
ce4ccb4b 452 2 exp: id .
bc933ef1 453
87675353 454 $default reduce using rule 2 (exp)
bc933ef1
AD
455
456
457state 5
458
ce4ccb4b 459 0 $axiom: exp $ .
bc933ef1
AD
460
461 $default accept
3c31a486
AD
462]])
463
464AT_CLEANUP
465
466
467
468
469## -------------------- ##
470## %expect not enough. ##
471## -------------------- ##
472
473AT_SETUP([%expect not enough])
474
475AT_DATA([input.y],
476[[%token NUM OP
477%expect 0
478%%
479exp: exp OP exp | NUM;
480]])
481
482AT_CHECK([bison input.y -o input.c], 1, [],
483[input.y contains 1 shift/reduce conflict.
484expected 0 shift/reduce conflicts
485])
486AT_CLEANUP
487
488
489## --------------- ##
490## %expect right. ##
491## --------------- ##
492
493AT_SETUP([%expect right])
494
495AT_DATA([input.y],
496[[%token NUM OP
497%expect 1
498%%
499exp: exp OP exp | NUM;
500]])
501
bc933ef1 502AT_CHECK([bison input.y -o input.c])
3c31a486
AD
503AT_CLEANUP
504
505
506## ------------------ ##
507## %expect too much. ##
508## ------------------ ##
509
510AT_SETUP([%expect too much])
511
512AT_DATA([input.y],
513[[%token NUM OP
514%expect 2
515%%
516exp: exp OP exp | NUM;
517]])
518
519AT_CHECK([bison input.y -o input.c], 1, [],
520[input.y contains 1 shift/reduce conflict.
521expected 2 shift/reduce conflicts
522])
523AT_CLEANUP