1 |
2002-04-07 Akim Demaille <akim@epita.fr> |
2002-04-07 Akim Demaille <akim@epita.fr> |
2 |
|
|
3 |
|
* src/gram.h, src/gram.c (rules_swap): New. |
4 |
|
(ritem_longest_rhs): Use it. |
5 |
|
* src/gram.h (rule_t): `number' is a new member. |
6 |
|
* src/reader.c (packgram): Set it. |
7 |
|
* src/reduce.c (reduce_grammar_tables): Move the useless rules at |
8 |
|
the end of `rules', and count them out of `nrules'. |
9 |
|
(reduce_output, dump_grammar): Adjust. |
10 |
|
* src/print.c (print_grammar): It is no longer needed to check for |
11 |
|
the usefulness of a rule, as useless rules are beyond `nrules + 1'. |
12 |
|
* tests/reduce.at (Reduced Automaton): New test. |
13 |
|
|
14 |
|
diff -ur -x*.po -x Makefile -x *.pot -x configure* -x TODO bison-1.49a/src/gram.c bison/src/gram.c |
15 |
|
--- bison-1.49a/src/gram.c Sun Apr 7 17:36:56 2002 |
16 |
|
+++ bison/src/gram.c Sun Apr 7 18:36:42 2002 |
17 |
|
2input grammar variables for bison, |
18 |
|
- Copyright 1984, 1986, 1989, 2001 Free Software Foundation, Inc. |
19 |
|
+ Copyright 1984, 1986, 1989, 2001, 2002 Free Software Foundation, Inc. |
20 |
|
|
21 |
|
This file is part of Bison, the GNU Compiler Compiler. |
22 |
|
|
23 |
|
1_number; |
24 |
|
|
25 |
|
|
26 |
|
+/*--------------------------------------. |
27 |
|
+| Return the number of symbols in RHS. | |
28 |
|
+`--------------------------------------*/ |
29 |
|
+ |
30 |
|
+int |
31 |
|
+rule_rhs_length (rule_t *rule) |
32 |
|
+{ |
33 |
|
+ int res = 0; |
34 |
|
+ short *rhsp; |
35 |
|
+ for (rhsp = rule->rhs; *rhsp >= 0; ++rhsp) |
36 |
|
+ ++res; |
37 |
|
+ return res; |
38 |
|
+} |
39 |
|
+ |
40 |
|
+ |
41 |
|
/*------------------------. |
42 |
|
| Dump RITEM for traces. | |
43 |
|
`------------------------*/ |
44 |
|
6ngest_rhs (void) |
45 |
|
{ |
46 |
|
- int length; |
47 |
|
- int max; |
48 |
|
+ int max = 0; |
49 |
|
int i; |
50 |
|
|
51 |
|
- length = 0; |
52 |
|
- max = 0; |
53 |
|
- for (i = 0; i < nritems; ++i) |
54 |
|
- if (ritem[i] >= 0) |
55 |
|
- { |
56 |
|
- length++; |
57 |
|
- } |
58 |
|
- else |
59 |
|
- { |
60 |
|
- if (length > max) |
61 |
|
- max = length; |
62 |
|
- length = 0; |
63 |
|
- } |
64 |
|
+ for (i = 1; i < nrules + 1; ++i) |
65 |
|
+ { |
66 |
|
+ int length = rule_rhs_length (&rules[i]); |
67 |
|
+ if (length > max) |
68 |
|
+ max = length; |
69 |
|
+ } |
70 |
|
|
71 |
|
return max; |
72 |
|
} |
73 |
|
diff -ur -x*.po -x Makefile -x *.pot -x configure* -x TODO bison-1.49a/src/gram.h bison/src/gram.h |
74 |
|
--- bison-1.49a/src/gram.h Sun Apr 7 17:55:00 2002 |
75 |
|
+++ bison/src/gram.h Sun Apr 7 18:36:42 2002 |
76 |
|
0ule_s |
77 |
|
{ |
78 |
|
+ /* The number of the rule in the source. It is usually the index in |
79 |
|
+ RULES too, except if there are useless rules. */ |
80 |
|
+ short number; |
81 |
|
+ |
82 |
|
short lhs; |
83 |
|
short *rhs; |
84 |
|
short prec; |
85 |
|
0r_token_number; |
86 |
|
|
87 |
|
+/* Report the length of the RHS. */ |
88 |
|
+int rule_rhs_length PARAMS ((rule_t *rule)); |
89 |
|
|
90 |
|
/* Dump RITEM for traces. */ |
91 |
|
void ritem_print PARAMS ((FILE *out)); |
92 |
|
diff -ur -x*.po -x Makefile -x *.pot -x configure* -x TODO bison-1.49a/src/print.c bison/src/print.c |
93 |
|
--- bison-1.49a/src/print.c Sun Apr 7 17:55:00 2002 |
94 |
|
+++ bison/src/print.c Sun Apr 7 18:19:39 2002 |
95 |
|
17%s\n\n", _("Grammar")); |
96 |
|
fprintf (out, " %s\n", _("Number, Line, Rule")); |
97 |
|
for (i = 1; i < nrules + 1; i++) |
98 |
|
- /* Don't print rules disabled in reduce_grammar_tables. */ |
99 |
|
- if (rules[i].useful) |
100 |
|
- { |
101 |
|
- fprintf (out, _(" %3d %3d %s ->"), |
102 |
|
- i - 1, rules[i].line, escape (symbols[rules[i].lhs]->tag)); |
103 |
|
- rule = rules[i].rhs; |
104 |
|
- if (*rule >= 0) |
105 |
|
- while (*rule >= 0) |
106 |
|
- fprintf (out, " %s", escape (symbols[*rule++]->tag)); |
107 |
|
- else |
108 |
|
- fprintf (out, " /* %s */", _("empty")); |
109 |
|
- fputc ('\n', out); |
110 |
|
- } |
111 |
|
+ { |
112 |
|
+ fprintf (out, _(" %3d %3d %s ->"), |
113 |
|
+ i - 1, rules[i].line, escape (symbols[rules[i].lhs]->tag)); |
114 |
|
+ rule = rules[i].rhs; |
115 |
|
+ if (*rule >= 0) |
116 |
|
+ while (*rule >= 0) |
117 |
|
+ fprintf (out, " %s", escape (symbols[*rule++]->tag)); |
118 |
|
+ else |
119 |
|
+ fprintf (out, " /* %s */", _("empty")); |
120 |
|
+ fputc ('\n', out); |
121 |
|
+ } |
122 |
|
fputs ("\n\n", out); |
123 |
|
|
124 |
|
|
125 |
|
diff -ur -x*.po -x Makefile -x *.pot -x configure* -x TODO bison-1.49a/src/reader.c bison/src/reader.c |
126 |
|
--- bison-1.49a/src/reader.c Sun Apr 7 17:56:13 2002 |
127 |
|
+++ bison/src/reader.c Sun Apr 7 18:19:39 2002 |
128 |
|
@@ -1687,6 +1687,7 @@ |
129 |
|
while (p) |
130 |
|
{ |
131 |
|
bucket *ruleprec = p->ruleprec; |
132 |
|
+ rules[ruleno].number = ruleno; |
133 |
|
rules[ruleno].lhs = p->sym->number; |
134 |
|
rules[ruleno].rhs = ritem + itemno; |
135 |
|
rules[ruleno].line = p->line; |
136 |
|
diff -ur -x*.po -x Makefile -x *.pot -x configure* -x TODO bison-1.49a/src/reduce.c bison/src/reduce.c |
137 |
|
--- bison-1.49a/src/reduce.c Sun Apr 7 17:55:00 2002 |
138 |
|
+++ bison/src/reduce.c Sun Apr 7 18:36:42 2002 |
139 |
|
2V1, rules[i].precsym); |
140 |
|
} |
141 |
|
|
142 |
|
+ |
143 |
|
+/*-------------------------------------------------------------------. |
144 |
|
+| Put the useless productions at the end of RULES, and adjust NRULES | |
145 |
|
+| accordingly. | |
146 |
|
+`-------------------------------------------------------------------*/ |
147 |
|
+ |
148 |
|
static void |
149 |
|
reduce_grammar_tables (void) |
150 |
|
{ |
151 |
|
- /* This is turned off because we would need to change the numbers in |
152 |
|
- the case statements in the actions file. |
153 |
|
- |
154 |
|
- We don't disable it via CPP so that it is still checked with the |
155 |
|
- rest of the code, to avoid its becoming completely obsolete. |
156 |
|
- |
157 |
|
- FIXME: I think the comment above demonstrates this code must be |
158 |
|
- turned off for *semantic* parser, not in the general case. Try |
159 |
|
- to understand this better --akim. */ |
160 |
|
- |
161 |
|
- if (0) |
162 |
|
- /* remove useless productions */ |
163 |
|
- if (nuseless_productions > 0) |
164 |
|
- { |
165 |
|
- short np, pn, ni, pi; |
166 |
|
- |
167 |
|
- np = 0; |
168 |
|
- ni = 0; |
169 |
|
- for (pn = 1; pn < nrules + 1; pn++) |
170 |
|
- if (bitset_test (P, pn)) |
171 |
|
- { |
172 |
|
- np++; |
173 |
|
- if (pn != np) |
174 |
|
- { |
175 |
|
- rules[np].lhs = rules[pn].lhs; |
176 |
|
- rules[np].line = rules[pn].line; |
177 |
|
- rules[np].prec = rules[pn].prec; |
178 |
|
- rules[np].assoc = rules[pn].assoc; |
179 |
|
- rules[np].rhs = rules[pn].rhs; |
180 |
|
- if (rules[np].rhs - ritem != ni) |
181 |
|
- { |
182 |
|
- pi = rules[np].rhs - ritem; |
183 |
|
- rules[np].rhs = ritem + ni; |
184 |
|
- while (ritem[pi] >= 0) |
185 |
|
- ritem[ni++] = ritem[pi++]; |
186 |
|
- ritem[ni++] = -np; |
187 |
|
- } |
188 |
|
- } |
189 |
|
- else |
190 |
|
- { |
191 |
|
- while (ritem[ni++] >= 0) |
192 |
|
- /* Nothing. */; |
193 |
|
- } |
194 |
|
- } |
195 |
|
- |
196 |
|
- ritem[ni] = 0; |
197 |
|
- nrules -= nuseless_productions; |
198 |
|
- nitems = ni; |
199 |
|
- nritems = ni; |
200 |
|
+ /* Flag useless productions. */ |
201 |
|
+ { |
202 |
|
+ int pn; |
203 |
|
+ for (pn = 1; pn < nrules + 1; pn++) |
204 |
|
+ rules[pn].useful = bitset_test (P, pn); |
205 |
|
+ } |
206 |
|
|
207 |
|
- /* Is it worth it to reduce the amount of memory for the |
208 |
|
- grammar? Probably not. */ |
209 |
|
- } |
210 |
|
+ /* Map the nonterminals to their new index: useful first, useless |
211 |
|
+ afterwards. Kept for later report. */ |
212 |
|
+ { |
213 |
|
+ int useful = 1; |
214 |
|
+ int useless = nrules + 1 - nuseless_productions; |
215 |
|
+ rule_t *rules_sorted = XMALLOC (rule_t, nrules + 1) - 1; |
216 |
|
+ int i; |
217 |
|
+ for (i = 1; i < nrules + 1; ++i) |
218 |
|
+ rules_sorted[rules[i].useful ? useful++ : useless++] = rules[i]; |
219 |
|
+ free (rules + 1); |
220 |
|
+ rules = rules_sorted; |
221 |
|
|
222 |
|
- /* Disable useless productions. */ |
223 |
|
- if (nuseless_productions > 0) |
224 |
|
+ /* Also reorder ritems. */ |
225 |
|
{ |
226 |
|
- int pn; |
227 |
|
- for (pn = 1; pn < nrules + 1; pn++) |
228 |
|
- rules[pn].useful = bitset_test (P, pn); |
229 |
|
+ short *ritems_sorted = XCALLOC (short, nitems + 1); |
230 |
|
+ short *ritemsp = ritems_sorted; |
231 |
|
+ for (i = 1; i < nrules + 1; ++i) |
232 |
|
+ { |
233 |
|
+ short *rhsp = rules[i].rhs; |
234 |
|
+ rules[i].rhs = ritemsp; |
235 |
|
+ for (/* Nothing. */; *rhsp >= 0; ++rhsp) |
236 |
|
+ *ritemsp++ = *rhsp; |
237 |
|
+ *ritemsp++ = -i; |
238 |
|
+ } |
239 |
|
+ *ritemsp++ = 0; |
240 |
|
+ free (ritem); |
241 |
|
+ ritem = ritems_sorted; |
242 |
|
} |
243 |
|
+ nrules -= nuseless_productions; |
244 |
|
+ } |
245 |
|
+ |
246 |
|
+ /* Adjust NRITEMS and NITEMS. */ |
247 |
|
+ { |
248 |
|
+ int r; |
249 |
|
+ int length; |
250 |
|
+ for (r = nrules + 1; r < nrules + 1 + nuseless_productions; ++r) |
251 |
|
+ { |
252 |
|
+ length = rule_rhs_length (&rules[r]); |
253 |
|
+ nritems -= length + 1; |
254 |
|
+ nitems -= length + 1; |
255 |
|
+ } |
256 |
|
+ } |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
|
@@ -378,16 +372,15 @@ |
261 |
|
{ |
262 |
|
int i; |
263 |
|
fprintf (out, "%s\n\n", _("Useless rules:")); |
264 |
|
- for (i = 1; i < nrules + 1; i++) |
265 |
|
- if (!rules[i].useful) |
266 |
|
- { |
267 |
|
- rule r; |
268 |
|
- fprintf (out, "#%-4d ", i - 1); |
269 |
|
- fprintf (out, "%s:", symbols[rules[i].lhs]->tag); |
270 |
|
- for (r = rules[i].rhs; *r >= 0; r++) |
271 |
|
- fprintf (out, " %s", symbols[*r]->tag); |
272 |
|
- fputs (";\n", out); |
273 |
|
- } |
274 |
|
+ for (i = nrules + 1; i < nuseless_productions + nrules + 1; i++) |
275 |
|
+ { |
276 |
|
+ rule r; |
277 |
|
+ fprintf (out, "#%-4d ", rules[i].number - 1); |
278 |
|
+ fprintf (out, "%s:", symbols[rules[i].lhs]->tag); |
279 |
|
+ for (r = rules[i].rhs; *r >= 0; r++) |
280 |
|
+ fprintf (out, " %s", symbols[*r]->tag); |
281 |
|
+ fputs (";\n", out); |
282 |
|
+ } |
283 |
|
fputs ("\n\n", out); |
284 |
|
} |
285 |
|
} |
286 |
|
@@ -411,7 +404,7 @@ |
287 |
|
fprintf (out, "\n\n"); |
288 |
|
fprintf (out, "Rules\n-----\n\n"); |
289 |
|
fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n"); |
290 |
|
- for (i = 1; i < nrules + 1; i++) |
291 |
|
+ for (i = 1; i < nrules + nuseless_productions + 1; i++) |
292 |
|
{ |
293 |
|
int rhs_count = 0; |
294 |
|
/* Find the last RHS index in ritems. */ |
295 |
|
@@ -429,7 +422,7 @@ |
296 |
|
} |
297 |
|
fprintf (out, "\n\n"); |
298 |
|
fprintf (out, "Rules interpreted\n-----------------\n\n"); |
299 |
|
- for (i = 1; i < nrules + 1; i++) |
300 |
|
+ for (i = 1; i < nrules + nuseless_productions + 1; i++) |
301 |
|
{ |
302 |
|
fprintf (out, "%-5d %s :", i, symbols[rules[i].lhs]->tag); |
303 |
|
for (r = rules[i].rhs; *r >= 0; r++) |
304 |
|
2art symbol %s does not derive any sentence"), |
305 |
|
symbols[start_symbol]->tag); |
306 |
|
|
307 |
|
- reduce_grammar_tables (); |
308 |
|
+ if (nuseless_productions > 0) |
309 |
|
+ reduce_grammar_tables (); |
310 |
|
if (nuseless_nonterminals > 0) |
311 |
|
nonterminals_reduce (); |
312 |
|
|
313 |
|
diff -ur -x*.po -x Makefile -x *.pot -x configure* -x TODO bison-1.49a/tests/reduce.at bison/tests/reduce.at |
314 |
|
--- bison-1.49a/tests/reduce.at Sun Apr 7 17:36:56 2002 |
315 |
|
+++ bison/tests/reduce.at Sun Apr 7 18:19:39 2002 |
316 |
|
0------- ## |
317 |
|
+## Reduced Automaton. ## |
318 |
|
+## ------------------- ## |
319 |
|
+ |
320 |
|
+# Check that the automaton is that as the for the grammar reduced by |
321 |
|
+# hand. |
322 |
|
+ |
323 |
|
+AT_SETUP([Reduced Automaton]) |
324 |
|
+ |
325 |
|
+# The non reduced grammar. |
326 |
|
+# ------------------------ |
327 |
|
+AT_DATA([[not-reduced.y]], |
328 |
|
+[[/* A useless token. */ |
329 |
|
+%token useless_token |
330 |
|
+/* A useful one. */ |
331 |
|
+%token useful |
332 |
|
+%verbose |
333 |
|
+%output="not-reduced.c" |
334 |
|
+ |
335 |
|
+%% |
336 |
|
+ |
337 |
|
+exp: useful { /* A useful action. */ } |
338 |
|
+ | non_productive { /* A non productive action. */ } |
339 |
|
+ ; |
340 |
|
+ |
341 |
|
+not_reachable: useful { /* A not reachable action. */ } |
342 |
|
+ ; |
343 |
|
+ |
344 |
|
+non_productive: non_productive useless_token |
345 |
|
+ { /* Another non productive action. */ } |
346 |
|
+ ; |
347 |
|
+]]) |
348 |
|
+ |
349 |
|
+AT_CHECK([[bison not-reduced.y]], 0, [], |
350 |
|
+[[not-reduced.y contains 2 useless nonterminals and 3 useless rules |
351 |
|
+]]) |
352 |
|
+ |
353 |
|
+AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' not-reduced.output]], 0, |
354 |
|
+[[Useless nonterminals: |
355 |
|
+ not_reachable |
356 |
|
+ non_productive |
357 |
|
+Terminals which are not used: |
358 |
|
+ useless_token |
359 |
|
+Useless rules: |
360 |
|
+#2 exp: non_productive; |
361 |
|
+#3 not_reachable: useful; |
362 |
|
+#4 non_productive: non_productive useless_token; |
363 |
|
+]]) |
364 |
|
+ |
365 |
|
+# The reduced grammar. |
366 |
|
+# -------------------- |
367 |
|
+AT_DATA([[reduced.y]], |
368 |
|
+[[/* A useless token. */ |
369 |
|
+%token useless_token |
370 |
|
+/* A useful one. */ |
371 |
|
+%token useful |
372 |
|
+%verbose |
373 |
|
+%output="reduced.c" |
374 |
|
+ |
375 |
|
+%% |
376 |
|
+ |
377 |
|
+exp: useful { /* A useful action. */ } |
378 |
|
+// | non_productive { /* A non productive action. */ } */ |
379 |
|
+ ; |
380 |
|
+ |
381 |
|
+//not_reachable: useful { /* A not reachable action. */ } |
382 |
|
+// ; |
383 |
|
+ |
384 |
|
+//non_productive: non_productive useless_token |
385 |
|
+// { /* Another non productive action. */ } |
386 |
|
+// ; |
387 |
|
+]]) |
388 |
|
+ |
389 |
|
+AT_CHECK([[bison reduced.y]]) |
390 |
|
+ |
391 |
|
+# Comparing the parsers. |
392 |
|
+cp reduced.c expout |
393 |
|
+AT_CHECK([sed 's/not-reduced/reduced/g' not-reduced.c], 0, [expout]) |
394 |
|
+ |
395 |
|
+AT_CLEANUP |
396 |
|
+ |
397 |
|
+ |
398 |
|
+ |
399 |
|
+## ------------------- ## |
400 |
|
## Underivable Rules. ## |
401 |
|
## ------------------- ## |
402 |
|
|
403 |
|
2002-04-07 Akim Demaille <akim@epita.fr> |
404 |
|
|
405 |
* src/output.c (output_rule_data): Fix various range errors: |
* src/output.c (output_rule_data): Fix various range errors: |
406 |
`rules' starts at 1, not 0. |
`rules' starts at 1, not 0. |
407 |
|
|