]> git.saurik.com Git - bison.git/blame - tests/conflicts.at
Regen.
[bison.git] / tests / conflicts.at
CommitLineData
3c31a486 1# Exercising Bison on conflicts. -*- Autotest -*-
643a5994 2# Copyright (C) 2002 Free Software Foundation, Inc.
3c31a486
AD
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
643a5994
AD
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])
41
42AT_CLEANUP
43
3c31a486
AD
44## ------------------- ##
45## %nonassoc and eof. ##
46## ------------------- ##
47
48AT_SETUP([%nonassoc and eof])
49
50AT_DATA([input.y],
51[[
52%{
1207eeac
AD
53#include <config.h>
54/* We don't need a perfect malloc for these tests. */
55#undef malloc
3c31a486 56#include <stdio.h>
1207eeac
AD
57
58#if STDC_HEADERS
59# include <stdlib.h>
60#endif
61
3c31a486 62#define YYERROR_VERBOSE 1
1207eeac
AD
63static void
64yyerror (const char *msg)
65{
66 fprintf (stderr, "%s\n", msg);
67 exit (1);
68}
3c31a486
AD
69
70/* The current argument. */
71static const char *input = NULL;
72
73static int
74yylex (void)
75{
76 /* No token stands for end of file. */
77 if (input && *input)
78 return *input++;
79 else
80 return 0;
81}
82
83%}
84
85%nonassoc '<' '>'
86
87%%
88expr: expr '<' expr
89 | expr '>' expr
90 | '0'
91 ;
92%%
93int
94main (int argc, const char *argv[])
95{
96 if (argc > 1)
97 input = argv[1];
98 return yyparse ();
99}
100]])
101
102# Specify the output files to avoid problems on different file systems.
103AT_CHECK([bison input.y -o input.c])
104AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
105
106AT_CHECK([./input '0<0'])
107# FIXME: This is an actual bug, but a new one, in the sense that
108# no one has ever spotted it! The messages are *wrong*: there should
109# be nothing there, it should be expected eof.
110AT_CHECK([./input '0<0<0'], [1], [],
111 [parse error, unexpected '<', expecting '<' or '>'
112])
113
114AT_CHECK([./input '0>0'])
115AT_CHECK([./input '0>0>0'], [1], [],
116 [parse error, unexpected '>', expecting '<' or '>'
117])
118
119AT_CHECK([./input '0<0>0'], [1], [],
120 [parse error, unexpected '>', expecting '<' or '>'
121])
122
123AT_CLEANUP
124
125
126
127## ------------------------- ##
128## Unresolved SR Conflicts. ##
129## ------------------------- ##
130
131AT_SETUP([Unresolved SR Conflicts])
132
6b98e4b5
AD
133AT_KEYWORDS([report])
134
3c31a486
AD
135AT_DATA([input.y],
136[[%token NUM OP
137%%
138exp: exp OP exp | NUM;
139]])
140
b408954b 141AT_CHECK([bison input.y -o input.c --report=all], 0, [],
3c31a486
AD
142[input.y contains 1 shift/reduce conflict.
143])
144
145# Check the contents of the report.
146AT_CHECK([cat input.output], [],
147[[State 5 contains 1 shift/reduce conflict.
148
149
150Grammar
151
6b98e4b5
AD
152 0 $axiom: exp $
153
154 1 exp: exp OP exp
155 2 | NUM
3c31a486
AD
156
157
158Terminals, with rules where they appear
159
160$ (0) 0
161error (256)
007a50a4
AD
162NUM (258) 2
163OP (259) 1
3c31a486
AD
164
165
166Nonterminals, with rules where they appear
167
168$axiom (5)
169 on left: 0
170exp (6)
171 on left: 1 2, on right: 0 1
172
173
174state 0
175
643a5994 176 $axiom -> . exp $ (rule 0)
b408954b
AD
177 exp -> . exp OP exp (rule 1)
178 exp -> . NUM (rule 2)
643a5994 179
3c31a486
AD
180 NUM shift, and go to state 1
181
182 exp go to state 2
183
184
185
186state 1
187
188 exp -> NUM . (rule 2)
189
190 $default reduce using rule 2 (exp)
191
192
193
194state 2
195
196 $axiom -> exp . $ (rule 0)
197 exp -> exp . OP exp (rule 1)
198
199 $ shift, and go to state 3
200 OP shift, and go to state 4
201
202
203
204state 3
205
206 $axiom -> exp $ . (rule 0)
207
208 $default accept
209
210
211state 4
212
b408954b 213 exp -> . exp OP exp (rule 1)
3c31a486 214 exp -> exp OP . exp (rule 1)
b408954b 215 exp -> . NUM (rule 2)
3c31a486
AD
216
217 NUM shift, and go to state 1
218
219 exp go to state 5
220
221
222
223state 5
224
b408954b
AD
225 exp -> exp . OP exp [$, OP] (rule 1)
226 exp -> exp OP exp . [$, OP] (rule 1)
3c31a486
AD
227
228 OP shift, and go to state 4
229
230 OP [reduce using rule 1 (exp)]
231 $default reduce using rule 1 (exp)
232
233
234
235]])
236
237AT_CLEANUP
238
239
240## --------------------- ##
241## Solved SR Conflicts. ##
242## --------------------- ##
243
244AT_SETUP([Solved SR Conflicts])
245
6b98e4b5
AD
246AT_KEYWORDS([report])
247
3c31a486
AD
248AT_DATA([input.y],
249[[%token NUM OP
250%right OP
251%%
252exp: exp OP exp | NUM;
253]])
254
b408954b 255AT_CHECK([bison input.y -o input.c --report=all], 0, [], [])
3c31a486
AD
256
257# Check the contents of the report.
258AT_CHECK([cat input.output], [],
b408954b 259[[Grammar
3c31a486 260
6b98e4b5
AD
261 0 $axiom: exp $
262
263 1 exp: exp OP exp
264 2 | NUM
3c31a486
AD
265
266
267Terminals, with rules where they appear
268
269$ (0) 0
270error (256)
007a50a4
AD
271NUM (258) 2
272OP (259) 1
3c31a486
AD
273
274
275Nonterminals, with rules where they appear
276
277$axiom (5)
278 on left: 0
279exp (6)
280 on left: 1 2, on right: 0 1
281
282
283state 0
284
643a5994 285 $axiom -> . exp $ (rule 0)
b408954b
AD
286 exp -> . exp OP exp (rule 1)
287 exp -> . NUM (rule 2)
643a5994 288
3c31a486
AD
289 NUM shift, and go to state 1
290
291 exp go to state 2
292
293
294
295state 1
296
297 exp -> NUM . (rule 2)
298
299 $default reduce using rule 2 (exp)
300
301
302
303state 2
304
305 $axiom -> exp . $ (rule 0)
306 exp -> exp . OP exp (rule 1)
307
308 $ shift, and go to state 3
309 OP shift, and go to state 4
310
311
312
313state 3
314
315 $axiom -> exp $ . (rule 0)
316
317 $default accept
318
319
320state 4
321
b408954b 322 exp -> . exp OP exp (rule 1)
3c31a486 323 exp -> exp OP . exp (rule 1)
b408954b 324 exp -> . NUM (rule 2)
3c31a486
AD
325
326 NUM shift, and go to state 1
327
328 exp go to state 5
329
330
331
332state 5
333
b408954b
AD
334 exp -> exp . OP exp [$] (rule 1)
335 exp -> exp OP exp . [$] (rule 1)
3c31a486
AD
336
337 OP shift, and go to state 4
338
339 $default reduce using rule 1 (exp)
340
b408954b 341 Conflict between rule 2 and token OP resolved as reduce (%right OP).
3c31a486
AD
342
343
344]])
345
346AT_CLEANUP
347
348
349
350
351## -------------------- ##
352## %expect not enough. ##
353## -------------------- ##
354
355AT_SETUP([%expect not enough])
356
357AT_DATA([input.y],
358[[%token NUM OP
359%expect 0
360%%
361exp: exp OP exp | NUM;
362]])
363
364AT_CHECK([bison input.y -o input.c], 1, [],
365[input.y contains 1 shift/reduce conflict.
366expected 0 shift/reduce conflicts
367])
368AT_CLEANUP
369
370
371## --------------- ##
372## %expect right. ##
373## --------------- ##
374
375AT_SETUP([%expect right])
376
377AT_DATA([input.y],
378[[%token NUM OP
379%expect 1
380%%
381exp: exp OP exp | NUM;
382]])
383
384AT_CHECK([bison input.y -o input.c], 0)
385AT_CLEANUP
386
387
388## ------------------ ##
389## %expect too much. ##
390## ------------------ ##
391
392AT_SETUP([%expect too much])
393
394AT_DATA([input.y],
395[[%token NUM OP
396%expect 2
397%%
398exp: exp OP exp | NUM;
399]])
400
401AT_CHECK([bison input.y -o input.c], 1, [],
402[input.y contains 1 shift/reduce conflict.
403expected 2 shift/reduce conflicts
404])
405AT_CLEANUP