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