]> git.saurik.com Git - bison.git/blob - src/gram.c
a1a0630853becd869f8466bfefead9b215eb454b
[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 int 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 pure_parser = 0;
47
48
49 /*--------------------------------------.
50 | Return the number of symbols in RHS. |
51 `--------------------------------------*/
52
53 int
54 rule_rhs_length (rule_t *rule)
55 {
56 int res = 0;
57 item_number_t *rhsp;
58 for (rhsp = rule->rhs; *rhsp >= 0; ++rhsp)
59 ++res;
60 return res;
61 }
62
63
64 /*-------------------------------.
65 | Print this RULE's RHS on OUT. |
66 `-------------------------------*/
67
68 void
69 rule_rhs_print (rule_t *rule, FILE *out)
70 {
71 if (*rule->rhs >= 0)
72 {
73 item_number_t *r;
74 for (r = rule->rhs; *r >= 0; r++)
75 fprintf (out, " %s", symbol_tag_get (symbols[*r]));
76 fputc ('\n', out);
77 }
78 else
79 {
80 fprintf (out, " /* %s */\n", _("empty"));
81 }
82 }
83
84
85 /*-------------------------.
86 | Print this RULE on OUT. |
87 `-------------------------*/
88
89 void
90 rule_print (rule_t *rule, FILE *out)
91 {
92 fprintf (out, "%s:", symbol_tag_get (rule->lhs));
93 rule_rhs_print (rule, out);
94 }
95
96
97 /*------------------------.
98 | Dump RITEM for traces. |
99 `------------------------*/
100
101 void
102 ritem_print (FILE *out)
103 {
104 unsigned int i;
105 fputs ("RITEM\n", out);
106 for (i = 0; i < nritems; ++i)
107 if (ritem[i] >= 0)
108 fprintf (out, " %s", symbol_tag_get (symbols[ritem[i]]));
109 else
110 fprintf (out, " (rule %d)\n", -ritem[i] - 1);
111 fputs ("\n\n", out);
112 }
113
114
115 /*------------------------------------------.
116 | Return the size of the longest rule RHS. |
117 `------------------------------------------*/
118
119 size_t
120 ritem_longest_rhs (void)
121 {
122 int max = 0;
123 int i;
124
125 for (i = 1; i < nrules + 1; ++i)
126 {
127 int length = rule_rhs_length (&rules[i]);
128 if (length > max)
129 max = length;
130 }
131
132 return max;
133 }
134
135
136 /*-----------------------------------.
137 | Print the grammar's rules on OUT. |
138 `-----------------------------------*/
139
140 static inline void
141 blanks_print (unsigned n, FILE *out)
142 {
143 for (/* Nothing*/; n > 0; --n)
144 fputc (' ', out);
145 }
146
147 void
148 grammar_rules_print (FILE *out)
149 {
150 int r;
151 symbol_t *last_lhs = NULL;
152
153 /* rule # : LHS -> RHS */
154 fprintf (out, "%s\n\n", _("Grammar"));
155 for (r = 1; r < nrules + 1; r++)
156 {
157 if (last_lhs && last_lhs != rules[r].lhs)
158 fputc ('\n', out);
159
160 fprintf (out, " %3d ", r - 1);
161 if (last_lhs != rules[r].lhs)
162 {
163 last_lhs = rules[r].lhs;
164 fprintf (out, "%s:", symbol_tag_get (last_lhs));
165 }
166 else
167 {
168 blanks_print (strlen (symbol_tag_get (last_lhs)), out);
169 fputc ('|', out);
170 }
171 rule_rhs_print (&rules[r], out);
172 }
173 fputs ("\n\n", out);
174 }
175
176 /*-------------------.
177 | Dump the grammar. |
178 `-------------------*/
179
180 void
181 grammar_dump (FILE *out, const char *title)
182 {
183 int i;
184 item_number_t *r;
185
186 fprintf (out, "%s\n\n", title);
187 fprintf (out,
188 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
189 ntokens, nvars, nsyms, nrules, nritems);
190 fprintf (out, "Variables\n---------\n\n");
191 fprintf (out, "Value Sprec Sassoc Tag\n");
192 for (i = ntokens; i < nsyms; i++)
193 fprintf (out, "%5d %5d %5d %s\n",
194 i,
195 symbols[i]->prec, symbols[i]->assoc,
196 symbol_tag_get (symbols[i]));
197 fprintf (out, "\n\n");
198 fprintf (out, "Rules\n-----\n\n");
199 fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
200 for (i = 1; i < nrules + nuseless_productions + 1; i++)
201 {
202 int rhs_count = 0;
203 /* Find the last RHS index in ritems. */
204 for (r = rules[i].rhs; *r >= 0; ++r)
205 ++rhs_count;
206 fprintf (out, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
207 i - 1,
208 rules[i].prec ? rules[i].prec->prec : 0,
209 rules[i].prec ? rules[i].prec->assoc : 0,
210 rules[i].useful,
211 rules[i].rhs - ritem,
212 rules[i].rhs - ritem + rhs_count - 1,
213 rules[i].lhs->number);
214 /* Dumped the RHS. */
215 for (r = rules[i].rhs; *r >= 0; r++)
216 fprintf (out, " %3d", *r);
217 fprintf (out, " [%d]\n", -(*r) - 1);
218 }
219 fprintf (out, "\n\n");
220 fprintf (out, "Rules interpreted\n-----------------\n\n");
221 for (i = 1; i < nrules + nuseless_productions + 1; i++)
222 {
223 fprintf (out, "%-5d ", i);
224 rule_print (&rules[i], out);
225 }
226 fprintf (out, "\n\n");
227 }
228
229
230 void
231 grammar_free (void)
232 {
233 XFREE (ritem);
234 free (rules + 1);
235 XFREE (token_translations);
236 /* Free the symbol table data structure. */
237 symbols_free ();
238 }