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