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