]>
Commit | Line | Data |
---|---|---|
6f84e9ab PE |
1 | /* Allocate input grammar variables for Bison. |
2 | ||
a737b216 PE |
3 | Copyright (C) 1984, 1986, 1989, 2001, 2002, 2003 Free Software |
4 | Foundation, Inc. | |
f7d4d87a | 5 | |
076ab033 | 6 | This file is part of Bison, the GNU Compiler Compiler. |
f7d4d87a | 7 | |
076ab033 AD |
8 | Bison is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
f7d4d87a | 12 | |
076ab033 AD |
13 | Bison is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
f7d4d87a | 17 | |
076ab033 AD |
18 | You should have received a copy of the GNU General Public License |
19 | along with Bison; see the file COPYING. If not, write to | |
0fb669f9 PE |
20 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. */ | |
f7d4d87a DM |
22 | |
23 | ||
4a120d45 | 24 | #include "system.h" |
81ebdef9 PE |
25 | |
26 | #include <quotearg.h> | |
27 | ||
78ab8f67 | 28 | #include "gram.h" |
3067fbef | 29 | #include "reader.h" |
81ebdef9 PE |
30 | #include "reduce.h" |
31 | #include "symtab.h" | |
4a120d45 | 32 | |
6b98e4b5 | 33 | /* Comments for these variables are in gram.h. */ |
f7d4d87a | 34 | |
81ebdef9 | 35 | item_number *ritem = NULL; |
0c2d3f4c | 36 | unsigned int nritems = 0; |
75142d45 | 37 | |
81ebdef9 PE |
38 | rule *rules = NULL; |
39 | rule_number nrules = 0; | |
0e78e603 | 40 | |
81ebdef9 | 41 | symbol **symbols = NULL; |
5123689b AD |
42 | int nsyms = 0; |
43 | int ntokens = 1; | |
44 | int nvars = 0; | |
45 | ||
81ebdef9 | 46 | symbol_number *token_translations = NULL; |
f7d4d87a | 47 | |
280a38c3 | 48 | int max_user_token_number = 256; |
f7d4d87a | 49 | |
c8f002c7 AD |
50 | /*--------------------------------------------------------------. |
51 | | Return true IFF the rule has a `number' smaller than NRULES. | | |
52 | `--------------------------------------------------------------*/ | |
53 | ||
54 | bool | |
81ebdef9 | 55 | rule_useful_p (rule *r) |
c8f002c7 AD |
56 | { |
57 | return r->number < nrules; | |
58 | } | |
59 | ||
60 | ||
61 | /*-------------------------------------------------------------. | |
62 | | Return true IFF the rule has a `number' higher than NRULES. | | |
63 | `-------------------------------------------------------------*/ | |
64 | ||
65 | bool | |
81ebdef9 | 66 | rule_useless_p (rule *r) |
c8f002c7 AD |
67 | { |
68 | return r->number >= nrules; | |
69 | } | |
70 | ||
71 | ||
72 | /*--------------------------------------------------------------------. | |
73 | | Return true IFF the rule is not flagged as useful *and* is useful. | | |
74 | | In other words, it was discarded because of conflicts. | | |
75 | `--------------------------------------------------------------------*/ | |
76 | ||
77 | bool | |
81ebdef9 | 78 | rule_never_reduced_p (rule *r) |
c8f002c7 AD |
79 | { |
80 | return !r->useful && r->number < nrules; | |
81 | } | |
82 | ||
83 | ||
ce4ccb4b AD |
84 | /*----------------------------------------------------------------. |
85 | | Print this RULE's number and lhs on OUT. If a PREVIOUS_LHS was | | |
86 | | already displayed (by a previous call for another rule), avoid | | |
87 | | useless repetitions. | | |
88 | `----------------------------------------------------------------*/ | |
89 | ||
90 | void | |
81ebdef9 | 91 | rule_lhs_print (rule *r, symbol *previous_lhs, FILE *out) |
ce4ccb4b | 92 | { |
81ebdef9 PE |
93 | fprintf (out, " %3d ", r->number); |
94 | if (previous_lhs != r->lhs) | |
ce4ccb4b | 95 | { |
81ebdef9 | 96 | fprintf (out, "%s:", r->lhs->tag); |
ce4ccb4b AD |
97 | } |
98 | else | |
99 | { | |
100 | int n; | |
97650f4e | 101 | for (n = strlen (previous_lhs->tag); n > 0; --n) |
ce4ccb4b AD |
102 | fputc (' ', out); |
103 | fputc ('|', out); | |
104 | } | |
105 | } | |
106 | ||
107 | ||
c3b407f4 AD |
108 | /*--------------------------------------. |
109 | | Return the number of symbols in RHS. | | |
110 | `--------------------------------------*/ | |
111 | ||
112 | int | |
81ebdef9 | 113 | rule_rhs_length (rule *r) |
c3b407f4 AD |
114 | { |
115 | int res = 0; | |
81ebdef9 PE |
116 | item_number *rhsp; |
117 | for (rhsp = r->rhs; *rhsp >= 0; ++rhsp) | |
c3b407f4 AD |
118 | ++res; |
119 | return res; | |
120 | } | |
121 | ||
122 | ||
6b98e4b5 | 123 | /*-------------------------------. |
81ebdef9 | 124 | | Print this rule's RHS on OUT. | |
6b98e4b5 AD |
125 | `-------------------------------*/ |
126 | ||
127 | void | |
81ebdef9 | 128 | rule_rhs_print (rule *r, FILE *out) |
6b98e4b5 | 129 | { |
81ebdef9 | 130 | if (*r->rhs >= 0) |
6b98e4b5 | 131 | { |
81ebdef9 PE |
132 | item_number *rp; |
133 | for (rp = r->rhs; *rp >= 0; rp++) | |
134 | fprintf (out, " %s", symbols[*rp]->tag); | |
6b98e4b5 AD |
135 | fputc ('\n', out); |
136 | } | |
137 | else | |
138 | { | |
139 | fprintf (out, " /* %s */\n", _("empty")); | |
140 | } | |
141 | } | |
142 | ||
143 | ||
144 | /*-------------------------. | |
81ebdef9 | 145 | | Print this rule on OUT. | |
6b98e4b5 AD |
146 | `-------------------------*/ |
147 | ||
148 | void | |
81ebdef9 | 149 | rule_print (rule *r, FILE *out) |
6b98e4b5 | 150 | { |
81ebdef9 PE |
151 | fprintf (out, "%s:", r->lhs->tag); |
152 | rule_rhs_print (r, out); | |
6b98e4b5 AD |
153 | } |
154 | ||
155 | ||
c2713865 AD |
156 | /*------------------------. |
157 | | Dump RITEM for traces. | | |
158 | `------------------------*/ | |
159 | ||
cbbe7505 | 160 | void |
3067fbef | 161 | ritem_print (FILE *out) |
f7d4d87a | 162 | { |
0c2d3f4c | 163 | unsigned int i; |
3067fbef | 164 | fputs ("RITEM\n", out); |
75142d45 AD |
165 | for (i = 0; i < nritems; ++i) |
166 | if (ritem[i] >= 0) | |
97650f4e | 167 | fprintf (out, " %s", symbols[ritem[i]]->tag); |
3067fbef | 168 | else |
4b3d3a8e | 169 | fprintf (out, " (rule %d)\n", item_number_as_rule_number (ritem[i])); |
3067fbef | 170 | fputs ("\n\n", out); |
f7d4d87a | 171 | } |
c2713865 AD |
172 | |
173 | ||
174 | /*------------------------------------------. | |
175 | | Return the size of the longest rule RHS. | | |
176 | `------------------------------------------*/ | |
177 | ||
178 | size_t | |
179 | ritem_longest_rhs (void) | |
180 | { | |
c3b407f4 | 181 | int max = 0; |
81ebdef9 | 182 | rule_number r; |
c2713865 | 183 | |
4b3d3a8e | 184 | for (r = 0; r < nrules; ++r) |
c3b407f4 | 185 | { |
9222837b | 186 | int length = rule_rhs_length (&rules[r]); |
c3b407f4 AD |
187 | if (length > max) |
188 | max = length; | |
189 | } | |
c2713865 AD |
190 | |
191 | return max; | |
192 | } | |
78ab8f67 AD |
193 | |
194 | ||
c8f002c7 AD |
195 | /*-----------------------------------------------------------------. |
196 | | Print the grammar's rules that match FILTER on OUT under TITLE. | | |
197 | `-----------------------------------------------------------------*/ | |
6b98e4b5 | 198 | |
6b98e4b5 | 199 | void |
9757c359 | 200 | grammar_rules_partial_print (FILE *out, const char *title, |
81ebdef9 | 201 | rule_filter filter) |
6b98e4b5 | 202 | { |
a737b216 | 203 | rule_number r; |
637c4b28 | 204 | bool first = true; |
81ebdef9 | 205 | symbol *previous_lhs = NULL; |
6b98e4b5 AD |
206 | |
207 | /* rule # : LHS -> RHS */ | |
c8f002c7 | 208 | for (r = 0; r < nrules + nuseless_productions; r++) |
6b98e4b5 | 209 | { |
c8f002c7 AD |
210 | if (filter && !filter (&rules[r])) |
211 | continue; | |
212 | if (first) | |
213 | fprintf (out, "%s\n\n", title); | |
214 | else if (previous_lhs && previous_lhs != rules[r].lhs) | |
6b98e4b5 | 215 | fputc ('\n', out); |
637c4b28 | 216 | first = false; |
ce4ccb4b | 217 | rule_lhs_print (&rules[r], previous_lhs, out); |
6b98e4b5 | 218 | rule_rhs_print (&rules[r], out); |
ce4ccb4b | 219 | previous_lhs = rules[r].lhs; |
6b98e4b5 | 220 | } |
c8f002c7 AD |
221 | if (!first) |
222 | fputs ("\n\n", out); | |
6b98e4b5 AD |
223 | } |
224 | ||
9757c359 AD |
225 | |
226 | /*------------------------------------------. | |
227 | | Print the grammar's useful rules on OUT. | | |
228 | `------------------------------------------*/ | |
229 | ||
230 | void | |
231 | grammar_rules_print (FILE *out) | |
232 | { | |
c8f002c7 | 233 | grammar_rules_partial_print (out, _("Grammar"), rule_useful_p); |
9757c359 AD |
234 | } |
235 | ||
236 | ||
78ab8f67 AD |
237 | /*-------------------. |
238 | | Dump the grammar. | | |
239 | `-------------------*/ | |
240 | ||
241 | void | |
242 | grammar_dump (FILE *out, const char *title) | |
243 | { | |
78ab8f67 AD |
244 | fprintf (out, "%s\n\n", title); |
245 | fprintf (out, | |
246 | "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n", | |
247 | ntokens, nvars, nsyms, nrules, nritems); | |
9222837b AD |
248 | |
249 | ||
fb9c0b33 PE |
250 | fputs (("Variables\n---------\n\n" |
251 | "Value Sprec Sassoc Tag\n"), | |
252 | out); | |
9222837b | 253 | { |
81ebdef9 | 254 | symbol_number i; |
9222837b AD |
255 | for (i = ntokens; i < nsyms; i++) |
256 | fprintf (out, "%5d %5d %5d %s\n", | |
257 | i, | |
258 | symbols[i]->prec, symbols[i]->assoc, | |
97650f4e | 259 | symbols[i]->tag); |
9222837b | 260 | } |
fb9c0b33 | 261 | fputs ("\n\n", out); |
9222837b | 262 | |
fb9c0b33 PE |
263 | fputs (("Rules\n-----\n\n" |
264 | "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n"), | |
265 | out); | |
9222837b | 266 | { |
81ebdef9 | 267 | rule_number i; |
4b3d3a8e | 268 | for (i = 0; i < nrules + nuseless_productions; i++) |
9222837b | 269 | { |
81ebdef9 | 270 | rule *rule_i = &rules[i]; |
a737b216 | 271 | item_number *rp = NULL; |
81ebdef9 | 272 | unsigned int rhs_itemno = rule_i->rhs - ritem; |
04098407 | 273 | unsigned int rhs_count = 0; |
9222837b | 274 | /* Find the last RHS index in ritems. */ |
a737b216 | 275 | for (rp = rule_i->rhs; *rp >= 0; ++rp) |
9222837b | 276 | ++rhs_count; |
e3fbd37f | 277 | fprintf (out, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->", |
4b3d3a8e | 278 | i, |
81ebdef9 PE |
279 | rule_i->prec ? rule_i->prec->prec : 0, |
280 | rule_i->prec ? rule_i->prec->assoc : 0, | |
281 | rule_i->useful, | |
e3fbd37f PE |
282 | rhs_itemno, |
283 | rhs_itemno + rhs_count - 1, | |
81ebdef9 | 284 | rule_i->lhs->number); |
9222837b | 285 | /* Dumped the RHS. */ |
a737b216 PE |
286 | for (rp = rule_i->rhs; *rp >= 0; rp++) |
287 | fprintf (out, " %3d", *rp); | |
288 | fprintf (out, " [%d]\n", item_number_as_rule_number (*rp)); | |
9222837b AD |
289 | } |
290 | } | |
fb9c0b33 | 291 | fputs ("\n\n", out); |
9222837b | 292 | |
fb9c0b33 | 293 | fputs ("Rules interpreted\n-----------------\n\n", out); |
9222837b | 294 | { |
81ebdef9 | 295 | rule_number r; |
4b3d3a8e | 296 | for (r = 0; r < nrules + nuseless_productions; r++) |
9222837b AD |
297 | { |
298 | fprintf (out, "%-5d ", r); | |
299 | rule_print (&rules[r], out); | |
300 | } | |
301 | } | |
fb9c0b33 | 302 | fputs ("\n\n", out); |
78ab8f67 | 303 | } |
5372019f AD |
304 | |
305 | ||
c8f002c7 AD |
306 | /*------------------------------------------------------------------. |
307 | | Report on STDERR the rules that are not flagged USEFUL, using the | | |
308 | | MESSAGE (which can be `useless rule' when invoked after grammar | | |
309 | | reduction, or `never reduced' after conflicts were taken into | | |
310 | | account). | | |
311 | `------------------------------------------------------------------*/ | |
312 | ||
313 | void | |
314 | grammar_rules_never_reduced_report (const char *message) | |
315 | { | |
81ebdef9 | 316 | rule_number r; |
c8f002c7 AD |
317 | for (r = 0; r < nrules ; ++r) |
318 | if (!rules[r].useful) | |
319 | { | |
5fcdb07b | 320 | location_print (stderr, rules[r].location); |
c8f002c7 AD |
321 | fprintf (stderr, ": %s: %s: ", |
322 | _("warning"), message); | |
323 | rule_print (&rules[r], stderr); | |
324 | } | |
325 | } | |
326 | ||
5372019f AD |
327 | void |
328 | grammar_free (void) | |
329 | { | |
afbb696d | 330 | free (ritem); |
4b3d3a8e | 331 | free (rules); |
afbb696d | 332 | free (token_translations); |
5372019f AD |
333 | /* Free the symbol table data structure. */ |
334 | symbols_free (); | |
676385e2 | 335 | free_merger_functions (); |
5372019f | 336 | } |