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