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