]> git.saurik.com Git - bison.git/blame - tests/conflicts.at
Display items as we display rules.
[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])
105AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
106
107AT_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.
111AT_CHECK([./input '0<0<0'], [1], [],
112 [parse error, unexpected '<', expecting '<' or '>'
113])
114
115AT_CHECK([./input '0>0'])
116AT_CHECK([./input '0>0>0'], [1], [],
117 [parse error, unexpected '>', expecting '<' or '>'
118])
119
120AT_CHECK([./input '0<0>0'], [1], [],
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
3c31a486
AD
181 NUM shift, and go to state 1
182
183 exp go to state 2
184
185
186
187state 1
188
ce4ccb4b 189 2 exp: NUM .
3c31a486
AD
190
191 $default reduce using rule 2 (exp)
192
193
194
195state 2
196
ce4ccb4b
AD
197 0 $axiom: exp . $
198 1 exp: exp . OP exp
3c31a486
AD
199
200 $ shift, and go to state 3
201 OP shift, and go to state 4
202
203
204
205state 3
206
ce4ccb4b 207 0 $axiom: exp $ .
3c31a486
AD
208
209 $default accept
210
211
212state 4
213
ce4ccb4b
AD
214 1 exp: . exp OP exp
215 1 | exp OP . exp
216 2 | . NUM
3c31a486
AD
217
218 NUM shift, and go to state 1
219
220 exp go to state 5
221
222
223
224state 5
225
ce4ccb4b
AD
226 1 exp: exp . OP exp [$, OP]
227 1 | exp OP exp . [$, OP]
3c31a486
AD
228
229 OP shift, and go to state 4
230
231 OP [reduce using rule 1 (exp)]
232 $default reduce using rule 1 (exp)
233
234
235
236]])
237
238AT_CLEANUP
239
240
3c31a486 241
ce4ccb4b
AD
242## ----------------------- ##
243## Resolved SR Conflicts. ##
244## ----------------------- ##
245
246AT_SETUP([Resolved SR Conflicts])
3c31a486 247
6b98e4b5
AD
248AT_KEYWORDS([report])
249
3c31a486
AD
250AT_DATA([input.y],
251[[%token NUM OP
ce4ccb4b 252%left OP
3c31a486
AD
253%%
254exp: exp OP exp | NUM;
255]])
256
ce4ccb4b 257AT_CHECK([bison input.y -o input.c --report=all])
3c31a486
AD
258
259# Check the contents of the report.
260AT_CHECK([cat input.output], [],
ce4ccb4b 261[[Grammar
3c31a486 262
6b98e4b5
AD
263 0 $axiom: exp $
264
265 1 exp: exp OP exp
266 2 | NUM
3c31a486
AD
267
268
269Terminals, with rules where they appear
270
271$ (0) 0
272error (256)
007a50a4
AD
273NUM (258) 2
274OP (259) 1
3c31a486
AD
275
276
277Nonterminals, with rules where they appear
278
279$axiom (5)
280 on left: 0
281exp (6)
282 on left: 1 2, on right: 0 1
283
284
285state 0
286
ce4ccb4b
AD
287 0 $axiom: . exp $
288 1 exp: . exp OP exp
289 2 | . NUM
643a5994 290
3c31a486
AD
291 NUM shift, and go to state 1
292
293 exp go to state 2
294
295
296
297state 1
298
ce4ccb4b 299 2 exp: NUM .
3c31a486
AD
300
301 $default reduce using rule 2 (exp)
302
303
304
305state 2
306
ce4ccb4b
AD
307 0 $axiom: exp . $
308 1 exp: exp . OP exp
3c31a486
AD
309
310 $ shift, and go to state 3
311 OP shift, and go to state 4
312
313
314
315state 3
316
ce4ccb4b 317 0 $axiom: exp $ .
3c31a486
AD
318
319 $default accept
320
321
322state 4
323
ce4ccb4b
AD
324 1 exp: . exp OP exp
325 1 | exp OP . exp
326 2 | . NUM
3c31a486
AD
327
328 NUM shift, and go to state 1
329
330 exp go to state 5
331
332
333
334state 5
335
ce4ccb4b
AD
336 1 exp: exp . OP exp [$, OP]
337 1 | exp OP exp . [$, OP]
3c31a486 338
3c31a486 339
bc933ef1
AD
340 $default reduce using rule 1 (exp)
341
ce4ccb4b 342 Conflict between rule 2 and token OP resolved as shift (%left OP).
bc933ef1
AD
343
344
345]])
346
347AT_CLEANUP
348
349
bc933ef1
AD
350## -------------------------------- ##
351## Defaulted Conflicted Reduction. ##
352## -------------------------------- ##
353
354# When there are RR conflicts, some rules are disabled. Usually it is
355# simply displayed as:
356#
357# $ reduce using rule 3 (num)
358# $ [reduce using rule 4 (id)]
359#
360# But when `reduce 3' is the default action, we'd produce:
361#
362# $ [reduce using rule 4 (id)]
363# $default reduce using rule 3 (num)
364#
365# In this precise case (a reduction is masked by the default
366# reduction), we make the `reduce 3' explicit:
367#
368# $ reduce using rule 3 (num)
369# $ [reduce using rule 4 (id)]
370# $default reduce using rule 3 (num)
371#
372# Maybe that's not the best display, but then, please propose something
373# else.
374
375AT_SETUP([Defaulted Conflicted Reduction])
376AT_KEYWORDS([report])
377
378AT_DATA([input.y],
379[[%%
380exp: num | id;
381num: '0';
382id : '0';
383%%
384]])
385
386AT_CHECK([bison input.y -o input.c --report=all], 1, [],
387[input.y contains 1 reduce/reduce conflict.
388])
389
390# Check the contents of the report.
391AT_CHECK([cat input.output], [],
392[[State 1 contains 1 reduce/reduce conflict.
393
394
395Grammar
396
397 0 $axiom: exp $
398
399 1 exp: num
400 2 | id
401
402 3 num: '0'
403
404 4 id: '0'
405
406
407Terminals, with rules where they appear
408
409$ (0) 0
410'0' (48) 3 4
411error (256)
412
413
414Nonterminals, with rules where they appear
415
416$axiom (4)
417 on left: 0
418exp (5)
419 on left: 1 2, on right: 0
420num (6)
421 on left: 3, on right: 1
422id (7)
423 on left: 4, on right: 2
424
425
426state 0
427
ce4ccb4b
AD
428 0 $axiom: . exp $
429 1 exp: . num
430 2 | . id
431 3 num: . '0'
432 4 id: . '0'
bc933ef1
AD
433
434 '0' shift, and go to state 1
435
436 exp go to state 2
437 num go to state 3
438 id go to state 4
439
440
441
442state 1
443
ce4ccb4b
AD
444 3 num: '0' . [$]
445 4 id: '0' . [$]
bc933ef1
AD
446
447 $ reduce using rule 3 (num)
448 $ [reduce using rule 4 (id)]
449 $default reduce using rule 3 (num)
450
451
452
453state 2
454
ce4ccb4b 455 0 $axiom: exp . $
bc933ef1
AD
456
457 $ shift, and go to state 5
458
459
460
461state 3
462
ce4ccb4b 463 1 exp: num .
bc933ef1 464
3c31a486
AD
465 $default reduce using rule 1 (exp)
466
bc933ef1
AD
467
468
469state 4
470
ce4ccb4b 471 2 exp: id .
bc933ef1
AD
472
473 $default reduce using rule 2 (exp)
474
475
476
477state 5
478
ce4ccb4b 479 0 $axiom: exp $ .
bc933ef1
AD
480
481 $default accept
3c31a486
AD
482
483
484]])
485
486AT_CLEANUP
487
488
489
490
491## -------------------- ##
492## %expect not enough. ##
493## -------------------- ##
494
495AT_SETUP([%expect not enough])
496
497AT_DATA([input.y],
498[[%token NUM OP
499%expect 0
500%%
501exp: exp OP exp | NUM;
502]])
503
504AT_CHECK([bison input.y -o input.c], 1, [],
505[input.y contains 1 shift/reduce conflict.
506expected 0 shift/reduce conflicts
507])
508AT_CLEANUP
509
510
511## --------------- ##
512## %expect right. ##
513## --------------- ##
514
515AT_SETUP([%expect right])
516
517AT_DATA([input.y],
518[[%token NUM OP
519%expect 1
520%%
521exp: exp OP exp | NUM;
522]])
523
bc933ef1 524AT_CHECK([bison input.y -o input.c])
3c31a486
AD
525AT_CLEANUP
526
527
528## ------------------ ##
529## %expect too much. ##
530## ------------------ ##
531
532AT_SETUP([%expect too much])
533
534AT_DATA([input.y],
535[[%token NUM OP
536%expect 2
537%%
538exp: exp OP exp | NUM;
539]])
540
541AT_CHECK([bison input.y -o input.c], 1, [],
542[input.y contains 1 shift/reduce conflict.
543expected 2 shift/reduce conflicts
544])
545AT_CLEANUP