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