]> git.saurik.com Git - bison.git/blame_incremental - tests/conflicts.at
Regen.
[bison.git] / tests / conflicts.at
... / ...
CommitLineData
1# Exercising Bison on conflicts. -*- Autotest -*-
2
3# Copyright (C) 2002, 2003 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., 59 Temple Place - Suite 330, Boston, MA
18# 02111-1307, USA.
19
20AT_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
32AT_SETUP([S/R in initial])
33
34AT_DATA([[input.y]],
35[[%expect 1
36%%
37exp: e 'e';
38e: 'e' | /* Nothing. */;
39]])
40
41AT_CHECK([bison -o input.c input.y], 0, [],
42[[input.y:4.9: warning: rule never reduced because of conflicts: e: /* empty */
43]])
44
45AT_CLEANUP
46
47
48## ------------------- ##
49## %nonassoc and eof. ##
50## ------------------- ##
51
52AT_SETUP([%nonassoc and eof])
53
54AT_DATA_GRAMMAR([input.y],
55[[
56%{
57#include <stdio.h>
58
59#if STDC_HEADERS
60# include <stdlib.h>
61#endif
62
63#define YYERROR_VERBOSE 1
64static void
65yyerror (const char *msg)
66{
67 fprintf (stderr, "%s\n", msg);
68 exit (1);
69}
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 -o input.c input.y])
105AT_COMPILE([input])
106
107AT_PARSER_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_PARSER_CHECK([./input '0<0<0'], [1], [],
112 [syntax error, unexpected '<', expecting '<' or '>'
113])
114
115AT_PARSER_CHECK([./input '0>0'])
116AT_PARSER_CHECK([./input '0>0>0'], [1], [],
117 [syntax error, unexpected '>', expecting '<' or '>'
118])
119
120AT_PARSER_CHECK([./input '0<0>0'], [1], [],
121 [syntax error, unexpected '>', expecting '<' or '>'
122])
123
124AT_CLEANUP
125
126
127
128## ------------------------- ##
129## Unresolved SR Conflicts. ##
130## ------------------------- ##
131
132AT_SETUP([Unresolved SR Conflicts])
133
134AT_KEYWORDS([report])
135
136AT_DATA([input.y],
137[[%token NUM OP
138%%
139exp: exp OP exp | NUM;
140]])
141
142AT_CHECK([bison -o input.c --report=all input.y], 0, [],
143[input.y: conflicts: 1 shift/reduce
144])
145
146# Check the contents of the report.
147AT_CHECK([cat input.output], [],
148[[State 5 conflicts: 1 shift/reduce
149
150
151Grammar
152
153 0 $accept: exp $end
154
155 1 exp: exp OP exp
156 2 | NUM
157
158
159Terminals, with rules where they appear
160
161$end (0) 0
162error (256)
163NUM (258) 2
164OP (259) 1
165
166
167Nonterminals, with rules where they appear
168
169$accept (5)
170 on left: 0
171exp (6)
172 on left: 1 2, on right: 0 1
173
174
175state 0
176
177 0 $accept: . exp $end
178 1 exp: . exp OP exp
179 2 | . NUM
180
181 NUM shift, and go to state 1
182
183 exp go to state 2
184
185
186state 1
187
188 2 exp: NUM .
189
190 $default reduce using rule 2 (exp)
191
192
193state 2
194
195 0 $accept: exp . $end
196 1 exp: exp . OP exp
197
198 $end shift, and go to state 3
199 OP shift, and go to state 4
200
201
202state 3
203
204 0 $accept: exp $end .
205
206 $default accept
207
208
209state 4
210
211 1 exp: . exp OP exp
212 1 | exp OP . exp
213 2 | . NUM
214
215 NUM shift, and go to state 1
216
217 exp go to state 5
218
219
220state 5
221
222 1 exp: exp . OP exp [$end, OP]
223 1 | exp OP exp . [$end, OP]
224
225 OP shift, and go to state 4
226
227 OP [reduce using rule 1 (exp)]
228 $default reduce using rule 1 (exp)
229]])
230
231AT_CLEANUP
232
233
234
235## ----------------------- ##
236## Resolved SR Conflicts. ##
237## ----------------------- ##
238
239AT_SETUP([Resolved SR Conflicts])
240
241AT_KEYWORDS([report])
242
243AT_DATA([input.y],
244[[%token NUM OP
245%left OP
246%%
247exp: exp OP exp | NUM;
248]])
249
250AT_CHECK([bison -o input.c --report=all input.y])
251
252# Check the contents of the report.
253AT_CHECK([cat input.output], [],
254[[Grammar
255
256 0 $accept: exp $end
257
258 1 exp: exp OP exp
259 2 | NUM
260
261
262Terminals, with rules where they appear
263
264$end (0) 0
265error (256)
266NUM (258) 2
267OP (259) 1
268
269
270Nonterminals, with rules where they appear
271
272$accept (5)
273 on left: 0
274exp (6)
275 on left: 1 2, on right: 0 1
276
277
278state 0
279
280 0 $accept: . exp $end
281 1 exp: . exp OP exp
282 2 | . NUM
283
284 NUM shift, and go to state 1
285
286 exp go to state 2
287
288
289state 1
290
291 2 exp: NUM .
292
293 $default reduce using rule 2 (exp)
294
295
296state 2
297
298 0 $accept: exp . $end
299 1 exp: exp . OP exp
300
301 $end shift, and go to state 3
302 OP shift, and go to state 4
303
304
305state 3
306
307 0 $accept: exp $end .
308
309 $default accept
310
311
312state 4
313
314 1 exp: . exp OP exp
315 1 | exp OP . exp
316 2 | . NUM
317
318 NUM shift, and go to state 1
319
320 exp go to state 5
321
322
323state 5
324
325 1 exp: exp . OP exp [$end, OP]
326 1 | exp OP exp . [$end, OP]
327
328 $default reduce using rule 1 (exp)
329
330 Conflict between rule 1 and token OP resolved as reduce (%left OP).
331]])
332
333AT_CLEANUP
334
335
336## -------------------------------- ##
337## Defaulted Conflicted Reduction. ##
338## -------------------------------- ##
339
340# When there are RR conflicts, some rules are disabled. Usually it is
341# simply displayed as:
342#
343# $end reduce using rule 3 (num)
344# $end [reduce using rule 4 (id)]
345#
346# But when `reduce 3' is the default action, we'd produce:
347#
348# $end [reduce using rule 4 (id)]
349# $default reduce using rule 3 (num)
350#
351# In this precise case (a reduction is masked by the default
352# reduction), we make the `reduce 3' explicit:
353#
354# $end reduce using rule 3 (num)
355# $end [reduce using rule 4 (id)]
356# $default reduce using rule 3 (num)
357#
358# Maybe that's not the best display, but then, please propose something
359# else.
360
361AT_SETUP([Defaulted Conflicted Reduction])
362AT_KEYWORDS([report])
363
364AT_DATA([input.y],
365[[%%
366exp: num | id;
367num: '0';
368id : '0';
369%%
370]])
371
372AT_CHECK([bison -o input.c --report=all input.y], 0, [],
373[[input.y: conflicts: 1 reduce/reduce
374input.y:4.6-8: warning: rule never reduced because of conflicts: id: '0'
375]])
376
377# Check the contents of the report.
378AT_CHECK([cat input.output], [],
379[[Rules never reduced
380
381 4 id: '0'
382
383
384State 1 conflicts: 1 reduce/reduce
385
386
387Grammar
388
389 0 $accept: exp $end
390
391 1 exp: num
392 2 | id
393
394 3 num: '0'
395
396 4 id: '0'
397
398
399Terminals, with rules where they appear
400
401$end (0) 0
402'0' (48) 3 4
403error (256)
404
405
406Nonterminals, with rules where they appear
407
408$accept (4)
409 on left: 0
410exp (5)
411 on left: 1 2, on right: 0
412num (6)
413 on left: 3, on right: 1
414id (7)
415 on left: 4, on right: 2
416
417
418state 0
419
420 0 $accept: . exp $end
421 1 exp: . num
422 2 | . id
423 3 num: . '0'
424 4 id: . '0'
425
426 '0' shift, and go to state 1
427
428 exp go to state 2
429 num go to state 3
430 id go to state 4
431
432
433state 1
434
435 3 num: '0' . [$end]
436 4 id: '0' . [$end]
437
438 $end reduce using rule 3 (num)
439 $end [reduce using rule 4 (id)]
440 $default reduce using rule 3 (num)
441
442
443state 2
444
445 0 $accept: exp . $end
446
447 $end shift, and go to state 5
448
449
450state 3
451
452 1 exp: num .
453
454 $default reduce using rule 1 (exp)
455
456
457state 4
458
459 2 exp: id .
460
461 $default reduce using rule 2 (exp)
462
463
464state 5
465
466 0 $accept: exp $end .
467
468 $default accept
469]])
470
471AT_CLEANUP
472
473
474
475
476## -------------------- ##
477## %expect not enough. ##
478## -------------------- ##
479
480AT_SETUP([%expect not enough])
481
482AT_DATA([input.y],
483[[%token NUM OP
484%expect 0
485%%
486exp: exp OP exp | NUM;
487]])
488
489AT_CHECK([bison -o input.c input.y], 0, [],
490[input.y: conflicts: 1 shift/reduce
491input.y: warning: expected 0 shift/reduce conflicts
492])
493AT_CLEANUP
494
495
496## --------------- ##
497## %expect right. ##
498## --------------- ##
499
500AT_SETUP([%expect right])
501
502AT_DATA([input.y],
503[[%token NUM OP
504%expect 1
505%%
506exp: exp OP exp | NUM;
507]])
508
509AT_CHECK([bison -o input.c input.y])
510AT_CLEANUP
511
512
513## ------------------ ##
514## %expect too much. ##
515## ------------------ ##
516
517AT_SETUP([%expect too much])
518
519AT_DATA([input.y],
520[[%token NUM OP
521%expect 2
522%%
523exp: exp OP exp | NUM;
524]])
525
526AT_CHECK([bison -o input.c input.y], 0, [],
527[input.y: conflicts: 1 shift/reduce
528input.y: warning: expected 2 shift/reduce conflicts
529])
530AT_CLEANUP
531
532
533## ------------------------------ ##
534## %expect with reduce conflicts ##
535## ------------------------------ ##
536
537AT_SETUP([%expect with reduce conflicts])
538
539AT_DATA([input.y],
540[[%expect 0
541%%
542program: a 'a' | a a;
543a: 'a';
544]])
545
546AT_CHECK([bison -o input.c input.y], 0, [],
547[input.y: conflicts: 1 reduce/reduce
548input.y: warning: expected 0 reduce/reduce conflicts
549])
550AT_CLEANUP