]>
Commit | Line | Data |
---|---|---|
cb4956ee AD |
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 | ||
7d13ff5f | 47 | AT_CHECK([[bison input.y]]) |
cb4956ee AD |
48 | |
49 | AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0, | |
7d13ff5f | 50 | [[Terminals which are not used: |
cb4956ee AD |
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 | ||
760b53a8 AD |
91 | AT_CHECK([[bison input.y]], 0, [], |
92 | [[input.y contains 9 useless nonterminals | |
93 | ]]) | |
cb4956ee AD |
94 | |
95 | AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0, | |
760b53a8 | 96 | [[Useless nonterminals: |
cb4956ee AD |
97 | useless1 |
98 | useless2 | |
99 | useless3 | |
100 | useless4 | |
101 | useless5 | |
102 | useless6 | |
103 | useless7 | |
104 | useless8 | |
105 | useless9 | |
106 | ]]) | |
107 | ||
108 | AT_CLEANUP | |
68f1e3ed AD |
109 | |
110 | ||
111 | ||
112 | ## --------------- ## | |
113 | ## Useless Rules. ## | |
114 | ## --------------- ## | |
115 | ||
116 | AT_SETUP([Useless Rules]) | |
117 | ||
118 | AT_DATA([[input.y]], | |
119 | [[%verbose | |
120 | %output="input.c" | |
121 | %token useful | |
122 | %% | |
123 | exp: useful; | |
124 | useless1: '1'; | |
125 | useless2: '2'; | |
126 | useless3: '3'; | |
127 | useless4: '4'; | |
128 | useless5: '5'; | |
129 | useless6: '6'; | |
130 | useless7: '7'; | |
131 | useless8: '8'; | |
132 | useless9: '9'; | |
133 | ]]) | |
134 | ||
135 | AT_CHECK([[bison input.y]], 0, [], | |
136 | [[input.y contains 9 useless nonterminals and 9 useless rules | |
137 | ]]) | |
138 | ||
139 | AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0, | |
140 | [[Useless nonterminals: | |
141 | useless1 | |
142 | useless2 | |
143 | useless3 | |
144 | useless4 | |
145 | useless5 | |
146 | useless6 | |
147 | useless7 | |
148 | useless8 | |
149 | useless9 | |
150 | Terminals which are not used: | |
151 | '1' | |
152 | '2' | |
153 | '3' | |
154 | '4' | |
155 | '5' | |
156 | '6' | |
157 | '7' | |
158 | '8' | |
159 | '9' | |
160 | Useless rules: | |
161 | #2 useless1: '1'; | |
162 | #3 useless2: '2'; | |
163 | #4 useless3: '3'; | |
164 | #5 useless4: '4'; | |
165 | #6 useless5: '5'; | |
166 | #7 useless6: '6'; | |
167 | #8 useless7: '7'; | |
168 | #9 useless8: '8'; | |
169 | #10 useless9: '9'; | |
170 | ]]) | |
171 | ||
172 | AT_CLEANUP | |
173 | ||
174 | ||
175 | ||
c3b407f4 AD |
176 | ## ------------------- ## |
177 | ## Reduced Automaton. ## | |
178 | ## ------------------- ## | |
179 | ||
180 | # Check that the automaton is that as the for the grammar reduced by | |
181 | # hand. | |
182 | ||
183 | AT_SETUP([Reduced Automaton]) | |
184 | ||
185 | # The non reduced grammar. | |
186 | # ------------------------ | |
187 | AT_DATA([[not-reduced.y]], | |
188 | [[/* A useless token. */ | |
189 | %token useless_token | |
190 | /* A useful one. */ | |
191 | %token useful | |
192 | %verbose | |
193 | %output="not-reduced.c" | |
194 | ||
195 | %% | |
196 | ||
197 | exp: useful { /* A useful action. */ } | |
198 | | non_productive { /* A non productive action. */ } | |
199 | ; | |
200 | ||
201 | not_reachable: useful { /* A not reachable action. */ } | |
202 | ; | |
203 | ||
204 | non_productive: non_productive useless_token | |
205 | { /* Another non productive action. */ } | |
206 | ; | |
e9955c83 | 207 | %% |
c3b407f4 AD |
208 | ]]) |
209 | ||
210 | AT_CHECK([[bison not-reduced.y]], 0, [], | |
211 | [[not-reduced.y contains 2 useless nonterminals and 3 useless rules | |
212 | ]]) | |
213 | ||
214 | AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' not-reduced.output]], 0, | |
215 | [[Useless nonterminals: | |
216 | not_reachable | |
217 | non_productive | |
218 | Terminals which are not used: | |
219 | useless_token | |
220 | Useless rules: | |
221 | #2 exp: non_productive; | |
222 | #3 not_reachable: useful; | |
223 | #4 non_productive: non_productive useless_token; | |
224 | ]]) | |
225 | ||
226 | # The reduced grammar. | |
227 | # -------------------- | |
228 | AT_DATA([[reduced.y]], | |
229 | [[/* A useless token. */ | |
230 | %token useless_token | |
231 | /* A useful one. */ | |
232 | %token useful | |
233 | %verbose | |
234 | %output="reduced.c" | |
235 | ||
236 | %% | |
237 | ||
238 | exp: useful { /* A useful action. */ } | |
239 | // | non_productive { /* A non productive action. */ } */ | |
240 | ; | |
241 | ||
242 | //not_reachable: useful { /* A not reachable action. */ } | |
243 | // ; | |
244 | ||
245 | //non_productive: non_productive useless_token | |
246 | // { /* Another non productive action. */ } | |
247 | // ; | |
e9955c83 | 248 | %% |
c3b407f4 AD |
249 | ]]) |
250 | ||
251 | AT_CHECK([[bison reduced.y]]) | |
252 | ||
253 | # Comparing the parsers. | |
254 | cp reduced.c expout | |
255 | AT_CHECK([sed 's/not-reduced/reduced/g' not-reduced.c], 0, [expout]) | |
256 | ||
257 | AT_CLEANUP | |
258 | ||
259 | ||
260 | ||
68f1e3ed AD |
261 | ## ------------------- ## |
262 | ## Underivable Rules. ## | |
263 | ## ------------------- ## | |
264 | ||
265 | AT_SETUP([Underivable Rules]) | |
266 | ||
267 | AT_DATA([[input.y]], | |
268 | [[%verbose | |
269 | %output="input.c" | |
270 | %token useful | |
271 | %% | |
272 | exp: useful | underivable; | |
273 | underivable: indirection; | |
274 | indirection: underivable; | |
275 | ]]) | |
276 | ||
277 | AT_CHECK([[bison input.y]], 0, [], | |
278 | [[input.y contains 2 useless nonterminals and 3 useless rules | |
279 | ]]) | |
280 | ||
281 | AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0, | |
282 | [[Useless nonterminals: | |
283 | underivable | |
284 | indirection | |
285 | Useless rules: | |
286 | #2 exp: underivable; | |
287 | #3 underivable: indirection; | |
288 | #4 indirection: underivable; | |
289 | ]]) | |
290 | ||
291 | AT_CLEANUP |