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