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