]> git.saurik.com Git - bison.git/blob - tests/reduce.at
7dc939ed399e4428911efeb30449c28cd37287fa
[bison.git] / tests / reduce.at
1 # Exercising Bison Grammar Reduction. -*- Autotest -*-
2 # Copyright 2001 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([[Grammar Reduction.]])
20
21
22 ## ------------------- ##
23 ## Useless Terminals. ##
24 ## ------------------- ##
25
26 AT_SETUP([Useless Terminals])
27
28 AT_DATA([[input.y]],
29 [[%verbose
30 %output="input.c"
31
32 %token useless1
33 %token useless2
34 %token useless3
35 %token useless4
36 %token useless5
37 %token useless6
38 %token useless7
39 %token useless8
40 %token useless9
41
42 %token useful
43 %%
44 exp: useful;
45 ]])
46
47 AT_CHECK([[bison input.y]])
48
49 AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0,
50 [[Terminals which are not used:
51 useless1
52 useless2
53 useless3
54 useless4
55 useless5
56 useless6
57 useless7
58 useless8
59 useless9
60 ]])
61
62 AT_CLEANUP
63
64
65
66 ## ---------------------- ##
67 ## Useless Nonterminals. ##
68 ## ---------------------- ##
69
70 AT_SETUP([Useless Nonterminals])
71
72 AT_DATA([[input.y]],
73 [[%verbose
74 %output="input.c"
75
76 %nterm useless1
77 %nterm useless2
78 %nterm useless3
79 %nterm useless4
80 %nterm useless5
81 %nterm useless6
82 %nterm useless7
83 %nterm useless8
84 %nterm useless9
85
86 %token useful
87 %%
88 exp: useful;
89 ]])
90
91 AT_CHECK([[bison input.y]], 0, [],
92 [[input.y: warning: 9 useless nonterminals
93 input.y:4.8-15: warning: useless nonterminal: useless1
94 input.y:5.8-15: warning: useless nonterminal: useless2
95 input.y:6.8-15: warning: useless nonterminal: useless3
96 input.y:7.8-15: warning: useless nonterminal: useless4
97 input.y:8.8-15: warning: useless nonterminal: useless5
98 input.y:9.8-15: warning: useless nonterminal: useless6
99 input.y:10.8-15: warning: useless nonterminal: useless7
100 input.y:11.8-15: warning: useless nonterminal: useless8
101 input.y:12.8-15: warning: useless nonterminal: useless9
102 ]])
103
104 AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0,
105 [[Useless nonterminals:
106 useless1
107 useless2
108 useless3
109 useless4
110 useless5
111 useless6
112 useless7
113 useless8
114 useless9
115 ]])
116
117 AT_CLEANUP
118
119
120
121 ## --------------- ##
122 ## Useless Rules. ##
123 ## --------------- ##
124
125 AT_SETUP([Useless Rules])
126
127 AT_KEYWORDS([report])
128
129 AT_DATA([[input.y]],
130 [[%verbose
131 %output="input.c"
132 %token useful
133 %%
134 exp: useful;
135 useless1: '1';
136 useless2: '2';
137 useless3: '3';
138 useless4: '4';
139 useless5: '5';
140 useless6: '6';
141 useless7: '7';
142 useless8: '8';
143 useless9: '9';
144 ]])
145
146 AT_CHECK([[bison input.y]], 0, [],
147 [[input.y: warning: 9 useless nonterminals and 9 useless rules
148 input.y:6.1-8: warning: useless nonterminal: useless1
149 input.y:7.1-8: warning: useless nonterminal: useless2
150 input.y:8.1-8: warning: useless nonterminal: useless3
151 input.y:9.1-8: warning: useless nonterminal: useless4
152 input.y:10.1-8: warning: useless nonterminal: useless5
153 input.y:11.1-8: warning: useless nonterminal: useless6
154 input.y:12.1-8: warning: useless nonterminal: useless7
155 input.y:13.1-8: warning: useless nonterminal: useless8
156 input.y:14.1-8: warning: useless nonterminal: useless9
157 input.y:6.9-13: warning: useless rule: useless1: '1'
158 input.y:7.9-13: warning: useless rule: useless2: '2'
159 input.y:8.9-13: warning: useless rule: useless3: '3'
160 input.y:9.9-13: warning: useless rule: useless4: '4'
161 input.y:10.9-13: warning: useless rule: useless5: '5'
162 input.y:11.9-13: warning: useless rule: useless6: '6'
163 input.y:12.9-13: warning: useless rule: useless7: '7'
164 input.y:13.9-13: warning: useless rule: useless8: '8'
165 input.y:14.9-13: warning: useless rule: useless9: '9'
166 ]])
167
168 AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0,
169 [[Useless nonterminals:
170 useless1
171 useless2
172 useless3
173 useless4
174 useless5
175 useless6
176 useless7
177 useless8
178 useless9
179 Terminals which are not used:
180 '1'
181 '2'
182 '3'
183 '4'
184 '5'
185 '6'
186 '7'
187 '8'
188 '9'
189 Useless rules
190 2 useless1: '1'
191 3 useless2: '2'
192 4 useless3: '3'
193 5 useless4: '4'
194 6 useless5: '5'
195 7 useless6: '6'
196 8 useless7: '7'
197 9 useless8: '8'
198 10 useless9: '9'
199 ]])
200
201 AT_CLEANUP
202
203
204
205 ## ------------------- ##
206 ## Reduced Automaton. ##
207 ## ------------------- ##
208
209 # Check that the automaton is that as the for the grammar reduced by
210 # hand.
211
212 AT_SETUP([Reduced Automaton])
213
214 AT_KEYWORDS([report])
215
216 # The non reduced grammar.
217 # ------------------------
218 AT_DATA([[not-reduced.y]],
219 [[/* A useless token. */
220 %token useless_token
221 /* A useful one. */
222 %token useful
223 %verbose
224 %output="not-reduced.c"
225
226 %%
227
228 exp: useful { /* A useful action. */ }
229 | non_productive { /* A non productive action. */ }
230 ;
231
232 not_reachable: useful { /* A not reachable action. */ }
233 ;
234
235 non_productive: non_productive useless_token
236 { /* Another non productive action. */ }
237 ;
238 %%
239 ]])
240
241 AT_CHECK([[bison not-reduced.y]], 0, [],
242 [[not-reduced.y: warning: 2 useless nonterminals and 3 useless rules
243 not-reduced.y:14.1-13: warning: useless nonterminal: not_reachable
244 not-reduced.y:11.6-19: warning: useless nonterminal: non_productive
245 not-reduced.y:11.4-57: warning: useless rule: exp: non_productive
246 not-reduced.y:14.14-56: warning: useless rule: not_reachable: useful
247 not-reduced.y:17.15-18.63: warning: useless rule: non_productive: non_productive useless_token
248 ]])
249
250 AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' not-reduced.output]], 0,
251 [[Useless nonterminals:
252 not_reachable
253 non_productive
254 Terminals which are not used:
255 useless_token
256 Useless rules
257 2 exp: non_productive
258 3 not_reachable: useful
259 4 non_productive: non_productive useless_token
260 ]])
261
262 # The reduced grammar.
263 # --------------------
264 AT_DATA([[reduced.y]],
265 [[/* A useless token. */
266 %token useless_token
267 /* A useful one. */
268 %token useful
269 %verbose
270 %output="reduced.c"
271
272 %%
273
274 exp: useful { /* A useful action. */ }
275 // | non_productive { /* A non productive action. */ } */
276 ;
277
278 //not_reachable: useful { /* A not reachable action. */ }
279 // ;
280
281 //non_productive: non_productive useless_token
282 // { /* Another non productive action. */ }
283 // ;
284 %%
285 ]])
286
287 AT_CHECK([[bison reduced.y]])
288
289 # Comparing the parsers.
290 cp reduced.c expout
291 AT_CHECK([sed 's/not-reduced/reduced/g' not-reduced.c], 0, [expout])
292
293 AT_CLEANUP
294
295
296
297 ## ------------------- ##
298 ## Underivable Rules. ##
299 ## ------------------- ##
300
301 AT_SETUP([Underivable Rules])
302
303 AT_KEYWORDS([report])
304
305 AT_DATA([[input.y]],
306 [[%verbose
307 %output="input.c"
308 %token useful
309 %%
310 exp: useful | underivable;
311 underivable: indirection;
312 indirection: underivable;
313 ]])
314
315 AT_CHECK([[bison input.y]], 0, [],
316 [[input.y: warning: 2 useless nonterminals and 3 useless rules
317 input.y:5.15-25: warning: useless nonterminal: underivable
318 input.y:6.14-24: warning: useless nonterminal: indirection
319 input.y:5.13-25: warning: useless rule: exp: underivable
320 input.y:6.12-24: warning: useless rule: underivable: indirection
321 input.y:7.12-24: warning: useless rule: indirection: underivable
322 ]])
323
324 AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0,
325 [[Useless nonterminals:
326 underivable
327 indirection
328 Useless rules
329 2 exp: underivable
330 3 underivable: indirection
331 4 indirection: underivable
332 ]])
333
334 AT_CLEANUP