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