]> git.saurik.com Git - bison.git/blame - tests/conflicts.at
Let symbols have a location.
[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
133AT_DATA([input.y],
134[[%token NUM OP
135%%
136exp: exp OP exp | NUM;
137]])
138
b408954b 139AT_CHECK([bison input.y -o input.c --report=all], 0, [],
3c31a486
AD
140[input.y contains 1 shift/reduce conflict.
141])
142
143# Check the contents of the report.
144AT_CHECK([cat input.output], [],
145[[State 5 contains 1 shift/reduce conflict.
146
147
148Grammar
149
150 Number, Line, Rule
151 0 3 $axiom -> exp $
152 1 3 exp -> exp OP exp
153 2 3 exp -> NUM
154
155
156Terminals, with rules where they appear
157
158$ (0) 0
159error (256)
007a50a4
AD
160NUM (258) 2
161OP (259) 1
3c31a486
AD
162
163
164Nonterminals, with rules where they appear
165
166$axiom (5)
167 on left: 0
168exp (6)
169 on left: 1 2, on right: 0 1
170
171
172state 0
173
643a5994 174 $axiom -> . exp $ (rule 0)
b408954b
AD
175 exp -> . exp OP exp (rule 1)
176 exp -> . NUM (rule 2)
643a5994 177
3c31a486
AD
178 NUM shift, and go to state 1
179
180 exp go to state 2
181
182
183
184state 1
185
186 exp -> NUM . (rule 2)
187
188 $default reduce using rule 2 (exp)
189
190
191
192state 2
193
194 $axiom -> exp . $ (rule 0)
195 exp -> exp . OP exp (rule 1)
196
197 $ shift, and go to state 3
198 OP shift, and go to state 4
199
200
201
202state 3
203
204 $axiom -> exp $ . (rule 0)
205
206 $default accept
207
208
209state 4
210
b408954b 211 exp -> . exp OP exp (rule 1)
3c31a486 212 exp -> exp OP . exp (rule 1)
b408954b 213 exp -> . NUM (rule 2)
3c31a486
AD
214
215 NUM shift, and go to state 1
216
217 exp go to state 5
218
219
220
221state 5
222
b408954b
AD
223 exp -> exp . OP exp [$, OP] (rule 1)
224 exp -> exp OP exp . [$, OP] (rule 1)
3c31a486
AD
225
226 OP shift, and go to state 4
227
228 OP [reduce using rule 1 (exp)]
229 $default reduce using rule 1 (exp)
230
231
232
233]])
234
235AT_CLEANUP
236
237
238## --------------------- ##
239## Solved SR Conflicts. ##
240## --------------------- ##
241
242AT_SETUP([Solved SR Conflicts])
243
244AT_DATA([input.y],
245[[%token NUM OP
246%right OP
247%%
248exp: exp OP exp | NUM;
249]])
250
b408954b 251AT_CHECK([bison input.y -o input.c --report=all], 0, [], [])
3c31a486
AD
252
253# Check the contents of the report.
254AT_CHECK([cat input.output], [],
b408954b 255[[Grammar
3c31a486
AD
256
257 Number, Line, Rule
258 0 4 $axiom -> exp $
259 1 4 exp -> exp OP exp
260 2 4 exp -> NUM
261
262
263Terminals, with rules where they appear
264
265$ (0) 0
266error (256)
007a50a4
AD
267NUM (258) 2
268OP (259) 1
3c31a486
AD
269
270
271Nonterminals, with rules where they appear
272
273$axiom (5)
274 on left: 0
275exp (6)
276 on left: 1 2, on right: 0 1
277
278
279state 0
280
643a5994 281 $axiom -> . exp $ (rule 0)
b408954b
AD
282 exp -> . exp OP exp (rule 1)
283 exp -> . NUM (rule 2)
643a5994 284
3c31a486
AD
285 NUM shift, and go to state 1
286
287 exp go to state 2
288
289
290
291state 1
292
293 exp -> NUM . (rule 2)
294
295 $default reduce using rule 2 (exp)
296
297
298
299state 2
300
301 $axiom -> exp . $ (rule 0)
302 exp -> exp . OP exp (rule 1)
303
304 $ shift, and go to state 3
305 OP shift, and go to state 4
306
307
308
309state 3
310
311 $axiom -> exp $ . (rule 0)
312
313 $default accept
314
315
316state 4
317
b408954b 318 exp -> . exp OP exp (rule 1)
3c31a486 319 exp -> exp OP . exp (rule 1)
b408954b 320 exp -> . NUM (rule 2)
3c31a486
AD
321
322 NUM shift, and go to state 1
323
324 exp go to state 5
325
326
327
328state 5
329
b408954b
AD
330 exp -> exp . OP exp [$] (rule 1)
331 exp -> exp OP exp . [$] (rule 1)
3c31a486
AD
332
333 OP shift, and go to state 4
334
335 $default reduce using rule 1 (exp)
336
b408954b 337 Conflict between rule 2 and token OP resolved as reduce (%right OP).
3c31a486
AD
338
339
340]])
341
342AT_CLEANUP
343
344
345
346
347## -------------------- ##
348## %expect not enough. ##
349## -------------------- ##
350
351AT_SETUP([%expect not enough])
352
353AT_DATA([input.y],
354[[%token NUM OP
355%expect 0
356%%
357exp: exp OP exp | NUM;
358]])
359
360AT_CHECK([bison input.y -o input.c], 1, [],
361[input.y contains 1 shift/reduce conflict.
362expected 0 shift/reduce conflicts
363])
364AT_CLEANUP
365
366
367## --------------- ##
368## %expect right. ##
369## --------------- ##
370
371AT_SETUP([%expect right])
372
373AT_DATA([input.y],
374[[%token NUM OP
375%expect 1
376%%
377exp: exp OP exp | NUM;
378]])
379
380AT_CHECK([bison input.y -o input.c], 0)
381AT_CLEANUP
382
383
384## ------------------ ##
385## %expect too much. ##
386## ------------------ ##
387
388AT_SETUP([%expect too much])
389
390AT_DATA([input.y],
391[[%token NUM OP
392%expect 2
393%%
394exp: exp OP exp | NUM;
395]])
396
397AT_CHECK([bison input.y -o input.c], 1, [],
398[input.y contains 1 shift/reduce conflict.
399expected 2 shift/reduce conflicts
400])
401AT_CLEANUP