]>
Commit | Line | Data |
---|---|---|
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 | ||
19 | AT_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 | ||
31 | AT_SETUP([S/R in initial]) | |
32 | ||
33 | AT_DATA([[input.y]], | |
34 | [[%expect 1 | |
35 | %% | |
36 | exp: e 'e'; | |
37 | e: 'e' | /* Nothing. */; | |
38 | ]]) | |
39 | ||
40 | AT_CHECK([bison input.y -o input.c]) | |
41 | ||
42 | AT_CLEANUP | |
43 | ||
3c31a486 AD |
44 | ## ------------------- ## |
45 | ## %nonassoc and eof. ## | |
46 | ## ------------------- ## | |
47 | ||
48 | AT_SETUP([%nonassoc and eof]) | |
49 | ||
50 | AT_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 |
63 | static void |
64 | yyerror (const char *msg) | |
65 | { | |
66 | fprintf (stderr, "%s\n", msg); | |
67 | exit (1); | |
68 | } | |
3c31a486 AD |
69 | |
70 | /* The current argument. */ | |
71 | static const char *input = NULL; | |
72 | ||
73 | static int | |
74 | yylex (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 | %% | |
88 | expr: expr '<' expr | |
89 | | expr '>' expr | |
90 | | '0' | |
91 | ; | |
92 | %% | |
93 | int | |
94 | main (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. | |
103 | AT_CHECK([bison input.y -o input.c]) | |
104 | AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore]) | |
105 | ||
106 | AT_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. | |
110 | AT_CHECK([./input '0<0<0'], [1], [], | |
111 | [parse error, unexpected '<', expecting '<' or '>' | |
112 | ]) | |
113 | ||
114 | AT_CHECK([./input '0>0']) | |
115 | AT_CHECK([./input '0>0>0'], [1], [], | |
116 | [parse error, unexpected '>', expecting '<' or '>' | |
117 | ]) | |
118 | ||
119 | AT_CHECK([./input '0<0>0'], [1], [], | |
120 | [parse error, unexpected '>', expecting '<' or '>' | |
121 | ]) | |
122 | ||
123 | AT_CLEANUP | |
124 | ||
125 | ||
126 | ||
127 | ## ------------------------- ## | |
128 | ## Unresolved SR Conflicts. ## | |
129 | ## ------------------------- ## | |
130 | ||
131 | AT_SETUP([Unresolved SR Conflicts]) | |
132 | ||
133 | AT_DATA([input.y], | |
134 | [[%token NUM OP | |
135 | %% | |
136 | exp: exp OP exp | NUM; | |
137 | ]]) | |
138 | ||
139 | AT_CHECK([bison input.y -o input.c -v], 0, [], | |
140 | [input.y contains 1 shift/reduce conflict. | |
141 | ]) | |
142 | ||
143 | # Check the contents of the report. | |
144 | AT_CHECK([cat input.output], [], | |
145 | [[State 5 contains 1 shift/reduce conflict. | |
146 | ||
147 | ||
148 | Grammar | |
149 | ||
150 | Number, Line, Rule | |
151 | 0 3 $axiom -> exp $ | |
152 | 1 3 exp -> exp OP exp | |
153 | 2 3 exp -> NUM | |
154 | ||
155 | ||
156 | Terminals, with rules where they appear | |
157 | ||
158 | $ (0) 0 | |
159 | error (256) | |
007a50a4 AD |
160 | NUM (258) 2 |
161 | OP (259) 1 | |
3c31a486 AD |
162 | |
163 | ||
164 | Nonterminals, with rules where they appear | |
165 | ||
166 | $axiom (5) | |
167 | on left: 0 | |
168 | exp (6) | |
169 | on left: 1 2, on right: 0 1 | |
170 | ||
171 | ||
172 | state 0 | |
173 | ||
643a5994 AD |
174 | $axiom -> . exp $ (rule 0) |
175 | ||
3c31a486 AD |
176 | NUM shift, and go to state 1 |
177 | ||
178 | exp go to state 2 | |
179 | ||
180 | ||
181 | ||
182 | state 1 | |
183 | ||
184 | exp -> NUM . (rule 2) | |
185 | ||
186 | $default reduce using rule 2 (exp) | |
187 | ||
188 | ||
189 | ||
190 | state 2 | |
191 | ||
192 | $axiom -> exp . $ (rule 0) | |
193 | exp -> exp . OP exp (rule 1) | |
194 | ||
195 | $ shift, and go to state 3 | |
196 | OP shift, and go to state 4 | |
197 | ||
198 | ||
199 | ||
200 | state 3 | |
201 | ||
202 | $axiom -> exp $ . (rule 0) | |
203 | ||
204 | $default accept | |
205 | ||
206 | ||
207 | state 4 | |
208 | ||
209 | exp -> exp OP . exp (rule 1) | |
210 | ||
211 | NUM shift, and go to state 1 | |
212 | ||
213 | exp go to state 5 | |
214 | ||
215 | ||
216 | ||
217 | state 5 | |
218 | ||
219 | exp -> exp . OP exp (rule 1) | |
220 | exp -> exp OP exp . (rule 1) | |
221 | ||
222 | OP shift, and go to state 4 | |
223 | ||
224 | OP [reduce using rule 1 (exp)] | |
225 | $default reduce using rule 1 (exp) | |
226 | ||
227 | ||
228 | ||
229 | ]]) | |
230 | ||
231 | AT_CLEANUP | |
232 | ||
233 | ||
234 | ## --------------------- ## | |
235 | ## Solved SR Conflicts. ## | |
236 | ## --------------------- ## | |
237 | ||
238 | AT_SETUP([Solved SR Conflicts]) | |
239 | ||
240 | AT_DATA([input.y], | |
241 | [[%token NUM OP | |
242 | %right OP | |
243 | %% | |
244 | exp: exp OP exp | NUM; | |
245 | ]]) | |
246 | ||
247 | AT_CHECK([bison input.y -o input.c -v], 0, [], []) | |
248 | ||
249 | # Check the contents of the report. | |
250 | AT_CHECK([cat input.output], [], | |
251 | [[Conflict in state 5 between rule 2 and token OP resolved as shift. | |
252 | ||
253 | ||
254 | Grammar | |
255 | ||
256 | Number, Line, Rule | |
257 | 0 4 $axiom -> exp $ | |
258 | 1 4 exp -> exp OP exp | |
259 | 2 4 exp -> NUM | |
260 | ||
261 | ||
262 | Terminals, with rules where they appear | |
263 | ||
264 | $ (0) 0 | |
265 | error (256) | |
007a50a4 AD |
266 | NUM (258) 2 |
267 | OP (259) 1 | |
3c31a486 AD |
268 | |
269 | ||
270 | Nonterminals, with rules where they appear | |
271 | ||
272 | $axiom (5) | |
273 | on left: 0 | |
274 | exp (6) | |
275 | on left: 1 2, on right: 0 1 | |
276 | ||
277 | ||
278 | state 0 | |
279 | ||
643a5994 AD |
280 | $axiom -> . exp $ (rule 0) |
281 | ||
3c31a486 AD |
282 | NUM shift, and go to state 1 |
283 | ||
284 | exp go to state 2 | |
285 | ||
286 | ||
287 | ||
288 | state 1 | |
289 | ||
290 | exp -> NUM . (rule 2) | |
291 | ||
292 | $default reduce using rule 2 (exp) | |
293 | ||
294 | ||
295 | ||
296 | state 2 | |
297 | ||
298 | $axiom -> exp . $ (rule 0) | |
299 | exp -> exp . OP exp (rule 1) | |
300 | ||
301 | $ shift, and go to state 3 | |
302 | OP shift, and go to state 4 | |
303 | ||
304 | ||
305 | ||
306 | state 3 | |
307 | ||
308 | $axiom -> exp $ . (rule 0) | |
309 | ||
310 | $default accept | |
311 | ||
312 | ||
313 | state 4 | |
314 | ||
315 | exp -> exp OP . exp (rule 1) | |
316 | ||
317 | NUM shift, and go to state 1 | |
318 | ||
319 | exp go to state 5 | |
320 | ||
321 | ||
322 | ||
323 | state 5 | |
324 | ||
325 | exp -> exp . OP exp (rule 1) | |
326 | exp -> exp OP exp . (rule 1) | |
327 | ||
328 | OP shift, and go to state 4 | |
329 | ||
330 | $default reduce using rule 1 (exp) | |
331 | ||
332 | ||
333 | ||
334 | ]]) | |
335 | ||
336 | AT_CLEANUP | |
337 | ||
338 | ||
339 | ||
340 | ||
341 | ## -------------------- ## | |
342 | ## %expect not enough. ## | |
343 | ## -------------------- ## | |
344 | ||
345 | AT_SETUP([%expect not enough]) | |
346 | ||
347 | AT_DATA([input.y], | |
348 | [[%token NUM OP | |
349 | %expect 0 | |
350 | %% | |
351 | exp: exp OP exp | NUM; | |
352 | ]]) | |
353 | ||
354 | AT_CHECK([bison input.y -o input.c], 1, [], | |
355 | [input.y contains 1 shift/reduce conflict. | |
356 | expected 0 shift/reduce conflicts | |
357 | ]) | |
358 | AT_CLEANUP | |
359 | ||
360 | ||
361 | ## --------------- ## | |
362 | ## %expect right. ## | |
363 | ## --------------- ## | |
364 | ||
365 | AT_SETUP([%expect right]) | |
366 | ||
367 | AT_DATA([input.y], | |
368 | [[%token NUM OP | |
369 | %expect 1 | |
370 | %% | |
371 | exp: exp OP exp | NUM; | |
372 | ]]) | |
373 | ||
374 | AT_CHECK([bison input.y -o input.c], 0) | |
375 | AT_CLEANUP | |
376 | ||
377 | ||
378 | ## ------------------ ## | |
379 | ## %expect too much. ## | |
380 | ## ------------------ ## | |
381 | ||
382 | AT_SETUP([%expect too much]) | |
383 | ||
384 | AT_DATA([input.y], | |
385 | [[%token NUM OP | |
386 | %expect 2 | |
387 | %% | |
388 | exp: exp OP exp | NUM; | |
389 | ]]) | |
390 | ||
391 | AT_CHECK([bison input.y -o input.c], 1, [], | |
392 | [input.y contains 1 shift/reduce conflict. | |
393 | expected 2 shift/reduce conflicts | |
394 | ]) | |
395 | AT_CLEANUP |