]> git.saurik.com Git - bison.git/blame - src/gram.c
Do not include <assert.h>.
[bison.git] / src / gram.c
CommitLineData
f7d4d87a 1/* Allocate input grammar variables for bison,
8b3df748 2 Copyright (C) 1984, 1986, 1989, 2001, 2002 Free Software Foundation, Inc.
f7d4d87a 3
076ab033 4 This file is part of Bison, the GNU Compiler Compiler.
f7d4d87a 5
076ab033
AD
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
f7d4d87a 10
076ab033
AD
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
f7d4d87a 15
076ab033
AD
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
f7d4d87a
DM
20
21
4a120d45 22#include "system.h"
8b3df748 23#include "quotearg.h"
0e78e603 24#include "symtab.h"
78ab8f67
AD
25#include "gram.h"
26#include "reduce.h"
3067fbef 27#include "reader.h"
4a120d45 28
6b98e4b5 29/* Comments for these variables are in gram.h. */
f7d4d87a 30
62a3e4f0 31item_number_t *ritem = NULL;
0c2d3f4c 32unsigned int nritems = 0;
75142d45 33
1a2b5d37 34rule_t *rules = NULL;
9222837b 35rule_number_t nrules = 0;
0e78e603 36
db8837cb 37symbol_t **symbols = NULL;
5123689b
AD
38int nsyms = 0;
39int ntokens = 1;
40int nvars = 0;
41
a49aecd5 42symbol_number_t *token_translations = NULL;
f7d4d87a 43
280a38c3 44int max_user_token_number = 256;
f7d4d87a 45
676385e2 46int glr_parser = 0;
280a38c3 47int pure_parser = 0;
f7d4d87a 48
c2713865 49
c8f002c7
AD
50/*--------------------------------------------------------------.
51| Return true IFF the rule has a `number' smaller than NRULES. |
52`--------------------------------------------------------------*/
53
54bool
55rule_useful_p (rule_t *r)
56{
57 return r->number < nrules;
58}
59
60
61/*-------------------------------------------------------------.
62| Return true IFF the rule has a `number' higher than NRULES. |
63`-------------------------------------------------------------*/
64
65bool
66rule_useless_p (rule_t *r)
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
77bool
78rule_never_reduced_p (rule_t *r)
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
90void
91rule_lhs_print (rule_t *rule, symbol_t *previous_lhs, FILE *out)
92{
4b3d3a8e 93 fprintf (out, " %3d ", rule->number);
ce4ccb4b
AD
94 if (previous_lhs != rule->lhs)
95 {
97650f4e 96 fprintf (out, "%s:", rule->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
112int
113rule_rhs_length (rule_t *rule)
114{
115 int res = 0;
62a3e4f0 116 item_number_t *rhsp;
c3b407f4
AD
117 for (rhsp = rule->rhs; *rhsp >= 0; ++rhsp)
118 ++res;
119 return res;
120}
121
122
6b98e4b5
AD
123/*-------------------------------.
124| Print this RULE's RHS on OUT. |
125`-------------------------------*/
126
127void
128rule_rhs_print (rule_t *rule, FILE *out)
129{
130 if (*rule->rhs >= 0)
131 {
132 item_number_t *r;
133 for (r = rule->rhs; *r >= 0; r++)
97650f4e 134 fprintf (out, " %s", symbols[*r]->tag);
6b98e4b5
AD
135 fputc ('\n', out);
136 }
137 else
138 {
139 fprintf (out, " /* %s */\n", _("empty"));
140 }
141}
142
143
144/*-------------------------.
145| Print this RULE on OUT. |
146`-------------------------*/
147
148void
149rule_print (rule_t *rule, FILE *out)
150{
97650f4e 151 fprintf (out, "%s:", rule->lhs->tag);
6b98e4b5
AD
152 rule_rhs_print (rule, out);
153}
154
155
c2713865
AD
156/*------------------------.
157| Dump RITEM for traces. |
158`------------------------*/
159
cbbe7505 160void
3067fbef 161ritem_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
178size_t
179ritem_longest_rhs (void)
180{
c3b407f4 181 int max = 0;
9222837b 182 rule_number_t 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 199void
9757c359 200grammar_rules_partial_print (FILE *out, const char *title,
c8f002c7 201 rule_filter_t filter)
6b98e4b5
AD
202{
203 int r;
637c4b28 204 bool first = true;
ce4ccb4b 205 symbol_t *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
230void
231grammar_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
241void
242grammar_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
3f823769 250 fprintf (out, "Variables\n---------\n\n");
9222837b
AD
251 {
252 symbol_number_t i;
3f823769 253 fprintf (out, "Value Sprec Sassoc Tag\n");
9222837b
AD
254
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
AD
260 fprintf (out, "\n\n");
261 }
262
3f823769 263 fprintf (out, "Rules\n-----\n\n");
9222837b
AD
264 {
265 rule_number_t i;
3f823769 266 fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
4b3d3a8e 267 for (i = 0; i < nrules + nuseless_productions; i++)
9222837b
AD
268 {
269 rule_t *rule = &rules[i];
270 item_number_t *r = NULL;
e3fbd37f
PE
271 unsigned int rhs_itemno = rule->rhs - ritem;
272 unsigned int rhs_count = 0;
9222837b
AD
273 /* Find the last RHS index in ritems. */
274 for (r = rule->rhs; *r >= 0; ++r)
275 ++rhs_count;
e3fbd37f 276 fprintf (out, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->",
4b3d3a8e 277 i,
9222837b
AD
278 rule->prec ? rule->prec->prec : 0,
279 rule->prec ? rule->prec->assoc : 0,
280 rule->useful,
e3fbd37f
PE
281 rhs_itemno,
282 rhs_itemno + rhs_count - 1,
9222837b
AD
283 rule->lhs->number);
284 /* Dumped the RHS. */
285 for (r = rule->rhs; *r >= 0; r++)
286 fprintf (out, " %3d", *r);
4b3d3a8e 287 fprintf (out, " [%d]\n", item_number_as_rule_number (*r));
9222837b
AD
288 }
289 }
78ab8f67 290 fprintf (out, "\n\n");
9222837b 291
3f823769 292 fprintf (out, "Rules interpreted\n-----------------\n\n");
9222837b
AD
293 {
294 rule_number_t r;
4b3d3a8e 295 for (r = 0; r < nrules + nuseless_productions; r++)
9222837b
AD
296 {
297 fprintf (out, "%-5d ", r);
298 rule_print (&rules[r], out);
299 }
300 }
78ab8f67
AD
301 fprintf (out, "\n\n");
302}
5372019f
AD
303
304
c8f002c7
AD
305/*------------------------------------------------------------------.
306| Report on STDERR the rules that are not flagged USEFUL, using the |
307| MESSAGE (which can be `useless rule' when invoked after grammar |
308| reduction, or `never reduced' after conflicts were taken into |
309| account). |
310`------------------------------------------------------------------*/
311
312void
313grammar_rules_never_reduced_report (const char *message)
314{
315 rule_number_t r;
316 for (r = 0; r < nrules ; ++r)
317 if (!rules[r].useful)
318 {
319 LOCATION_PRINT (stderr, rules[r].location);
320 fprintf (stderr, ": %s: %s: ",
321 _("warning"), message);
322 rule_print (&rules[r], stderr);
323 }
324}
325
5372019f
AD
326void
327grammar_free (void)
328{
329 XFREE (ritem);
4b3d3a8e 330 free (rules);
5372019f
AD
331 XFREE (token_translations);
332 /* Free the symbol table data structure. */
333 symbols_free ();
676385e2 334 free_merger_functions ();
5372019f 335}