]> git.saurik.com Git - bison.git/blob - src/gram.c
Instead of attaching lookaheads and duplicating the rules being
[bison.git] / src / gram.c
1 /* Allocate input grammar variables for bison,
2 Copyright (C) 1984, 1986, 1989, 2001, 2002 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
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.
10
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.
15
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. */
20
21
22 #include "system.h"
23 #include "quotearg.h"
24 #include "symtab.h"
25 #include "gram.h"
26 #include "reduce.h"
27 #include "reader.h"
28
29 /* Comments for these variables are in gram.h. */
30
31 item_number_t *ritem = NULL;
32 unsigned int nritems = 0;
33
34 rule_t *rules = NULL;
35 rule_number_t nrules = 0;
36
37 symbol_t **symbols = NULL;
38 int nsyms = 0;
39 int ntokens = 1;
40 int nvars = 0;
41
42 symbol_number_t *token_translations = NULL;
43
44 int max_user_token_number = 256;
45
46 int glr_parser = 0;
47 int pure_parser = 0;
48
49
50 /*----------------------------------------------------------------.
51 | Print this RULE's number and lhs on OUT. If a PREVIOUS_LHS was |
52 | already displayed (by a previous call for another rule), avoid |
53 | useless repetitions. |
54 `----------------------------------------------------------------*/
55
56 void
57 rule_lhs_print (rule_t *rule, symbol_t *previous_lhs, FILE *out)
58 {
59 fprintf (out, " %3d ", rule->number);
60 if (previous_lhs != rule->lhs)
61 {
62 fprintf (out, "%s:", rule->lhs->tag);
63 }
64 else
65 {
66 int n;
67 for (n = strlen (previous_lhs->tag); n > 0; --n)
68 fputc (' ', out);
69 fputc ('|', out);
70 }
71 }
72
73
74 /*--------------------------------------.
75 | Return the number of symbols in RHS. |
76 `--------------------------------------*/
77
78 int
79 rule_rhs_length (rule_t *rule)
80 {
81 int res = 0;
82 item_number_t *rhsp;
83 for (rhsp = rule->rhs; *rhsp >= 0; ++rhsp)
84 ++res;
85 return res;
86 }
87
88
89 /*-------------------------------.
90 | Print this RULE's RHS on OUT. |
91 `-------------------------------*/
92
93 void
94 rule_rhs_print (rule_t *rule, FILE *out)
95 {
96 if (*rule->rhs >= 0)
97 {
98 item_number_t *r;
99 for (r = rule->rhs; *r >= 0; r++)
100 fprintf (out, " %s", symbols[*r]->tag);
101 fputc ('\n', out);
102 }
103 else
104 {
105 fprintf (out, " /* %s */\n", _("empty"));
106 }
107 }
108
109
110 /*-------------------------.
111 | Print this RULE on OUT. |
112 `-------------------------*/
113
114 void
115 rule_print (rule_t *rule, FILE *out)
116 {
117 fprintf (out, "%s:", rule->lhs->tag);
118 rule_rhs_print (rule, out);
119 }
120
121
122 /*------------------------.
123 | Dump RITEM for traces. |
124 `------------------------*/
125
126 void
127 ritem_print (FILE *out)
128 {
129 unsigned int i;
130 fputs ("RITEM\n", out);
131 for (i = 0; i < nritems; ++i)
132 if (ritem[i] >= 0)
133 fprintf (out, " %s", symbols[ritem[i]]->tag);
134 else
135 fprintf (out, " (rule %d)\n", item_number_as_rule_number (ritem[i]));
136 fputs ("\n\n", out);
137 }
138
139
140 /*------------------------------------------.
141 | Return the size of the longest rule RHS. |
142 `------------------------------------------*/
143
144 size_t
145 ritem_longest_rhs (void)
146 {
147 int max = 0;
148 rule_number_t r;
149
150 for (r = 0; r < nrules; ++r)
151 {
152 int length = rule_rhs_length (&rules[r]);
153 if (length > max)
154 max = length;
155 }
156
157 return max;
158 }
159
160
161 /*----------------------------------------------------------------.
162 | Print the grammar's rules numbers from BEGIN (inclusive) to END |
163 | (exclusive) on OUT under TITLE. |
164 `----------------------------------------------------------------*/
165
166 void
167 grammar_rules_partial_print (FILE *out, const char *title,
168 rule_number_t begin, rule_number_t end)
169 {
170 int r;
171 symbol_t *previous_lhs = NULL;
172
173 /* rule # : LHS -> RHS */
174 fprintf (out, "%s\n\n", title);
175 for (r = begin; r < end; r++)
176 {
177 if (previous_lhs && previous_lhs != rules[r].lhs)
178 fputc ('\n', out);
179 rule_lhs_print (&rules[r], previous_lhs, out);
180 rule_rhs_print (&rules[r], out);
181 previous_lhs = rules[r].lhs;
182 }
183 fputs ("\n\n", out);
184 }
185
186
187 /*------------------------------------------.
188 | Print the grammar's useful rules on OUT. |
189 `------------------------------------------*/
190
191 void
192 grammar_rules_print (FILE *out)
193 {
194 grammar_rules_partial_print (out, _("Grammar"), 0, nrules);
195 }
196
197
198 /*-------------------.
199 | Dump the grammar. |
200 `-------------------*/
201
202 void
203 grammar_dump (FILE *out, const char *title)
204 {
205 fprintf (out, "%s\n\n", title);
206 fprintf (out,
207 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
208 ntokens, nvars, nsyms, nrules, nritems);
209
210
211 fprintf (out, "Variables\n---------\n\n");
212 {
213 symbol_number_t i;
214 fprintf (out, "Value Sprec Sassoc Tag\n");
215
216 for (i = ntokens; i < nsyms; i++)
217 fprintf (out, "%5d %5d %5d %s\n",
218 i,
219 symbols[i]->prec, symbols[i]->assoc,
220 symbols[i]->tag);
221 fprintf (out, "\n\n");
222 }
223
224 fprintf (out, "Rules\n-----\n\n");
225 {
226 rule_number_t i;
227 fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
228 for (i = 0; i < nrules + nuseless_productions; i++)
229 {
230 rule_t *rule = &rules[i];
231 item_number_t *r = NULL;
232 int rhs_count = 0;
233 /* Find the last RHS index in ritems. */
234 for (r = rule->rhs; *r >= 0; ++r)
235 ++rhs_count;
236 fprintf (out, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
237 i,
238 rule->prec ? rule->prec->prec : 0,
239 rule->prec ? rule->prec->assoc : 0,
240 rule->useful,
241 rule->rhs - ritem,
242 rule->rhs - ritem + rhs_count - 1,
243 rule->lhs->number);
244 /* Dumped the RHS. */
245 for (r = rule->rhs; *r >= 0; r++)
246 fprintf (out, " %3d", *r);
247 fprintf (out, " [%d]\n", item_number_as_rule_number (*r));
248 }
249 }
250 fprintf (out, "\n\n");
251
252 fprintf (out, "Rules interpreted\n-----------------\n\n");
253 {
254 rule_number_t r;
255 for (r = 0; r < nrules + nuseless_productions; r++)
256 {
257 fprintf (out, "%-5d ", r);
258 rule_print (&rules[r], out);
259 }
260 }
261 fprintf (out, "\n\n");
262 }
263
264
265 void
266 grammar_free (void)
267 {
268 XFREE (ritem);
269 free (rules);
270 XFREE (token_translations);
271 /* Free the symbol table data structure. */
272 symbols_free ();
273 free_merger_functions ();
274 }