]> git.saurik.com Git - bison.git/blame - src/gram.c
Renaming file to glr.c.
[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;
5123689b 35int 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
280a38c3 46int pure_parser = 0;
f7d4d87a 47
c2713865 48
c3b407f4
AD
49/*--------------------------------------.
50| Return the number of symbols in RHS. |
51`--------------------------------------*/
52
53int
54rule_rhs_length (rule_t *rule)
55{
56 int res = 0;
62a3e4f0 57 item_number_t *rhsp;
c3b407f4
AD
58 for (rhsp = rule->rhs; *rhsp >= 0; ++rhsp)
59 ++res;
60 return res;
61}
62
63
6b98e4b5
AD
64/*-------------------------------.
65| Print this RULE's RHS on OUT. |
66`-------------------------------*/
67
68void
69rule_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
89void
90rule_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
c2713865
AD
97/*------------------------.
98| Dump RITEM for traces. |
99`------------------------*/
100
cbbe7505 101void
3067fbef 102ritem_print (FILE *out)
f7d4d87a 103{
0c2d3f4c 104 unsigned int i;
3067fbef 105 fputs ("RITEM\n", out);
75142d45
AD
106 for (i = 0; i < nritems; ++i)
107 if (ritem[i] >= 0)
6b98e4b5 108 fprintf (out, " %s", symbol_tag_get (symbols[ritem[i]]));
3067fbef 109 else
29d29c8f 110 fprintf (out, " (rule %d)\n", -ritem[i] - 1);
3067fbef 111 fputs ("\n\n", out);
f7d4d87a 112}
c2713865
AD
113
114
115/*------------------------------------------.
116| Return the size of the longest rule RHS. |
117`------------------------------------------*/
118
119size_t
120ritem_longest_rhs (void)
121{
c3b407f4 122 int max = 0;
9e7f6bbd 123 int i;
c2713865 124
c3b407f4
AD
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 }
c2713865
AD
131
132 return max;
133}
78ab8f67
AD
134
135
9757c359
AD
136/*----------------------------------------------------------------.
137| Print the grammar's rules numbers from BEGIN (inclusive) to END |
138| (exclusive) on OUT under TITLE. |
139`----------------------------------------------------------------*/
6b98e4b5
AD
140
141static inline void
142blanks_print (unsigned n, FILE *out)
143{
144 for (/* Nothing*/; n > 0; --n)
145 fputc (' ', out);
146}
147
148void
9757c359
AD
149grammar_rules_partial_print (FILE *out, const char *title,
150 int begin, int end)
6b98e4b5
AD
151{
152 int r;
153 symbol_t *last_lhs = NULL;
154
155 /* rule # : LHS -> RHS */
9757c359
AD
156 fprintf (out, "%s\n\n", title);
157 for (r = begin; r < end; r++)
6b98e4b5
AD
158 {
159 if (last_lhs && last_lhs != rules[r].lhs)
160 fputc ('\n', out);
161
162 fprintf (out, " %3d ", r - 1);
163 if (last_lhs != rules[r].lhs)
164 {
165 last_lhs = rules[r].lhs;
166 fprintf (out, "%s:", symbol_tag_get (last_lhs));
167 }
168 else
169 {
170 blanks_print (strlen (symbol_tag_get (last_lhs)), out);
171 fputc ('|', out);
172 }
173 rule_rhs_print (&rules[r], out);
174 }
175 fputs ("\n\n", out);
176}
177
9757c359
AD
178
179/*------------------------------------------.
180| Print the grammar's useful rules on OUT. |
181`------------------------------------------*/
182
183void
184grammar_rules_print (FILE *out)
185{
186 grammar_rules_partial_print (out, _("Grammar"), 1, nrules + 1);
187}
188
189
78ab8f67
AD
190/*-------------------.
191| Dump the grammar. |
192`-------------------*/
193
194void
195grammar_dump (FILE *out, const char *title)
196{
197 int i;
198 item_number_t *r;
199
200 fprintf (out, "%s\n\n", title);
201 fprintf (out,
202 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
203 ntokens, nvars, nsyms, nrules, nritems);
204 fprintf (out, "Variables\n---------\n\n");
205 fprintf (out, "Value Sprec Sassoc Tag\n");
206 for (i = ntokens; i < nsyms; i++)
207 fprintf (out, "%5d %5d %5d %s\n",
208 i,
209 symbols[i]->prec, symbols[i]->assoc,
6b98e4b5 210 symbol_tag_get (symbols[i]));
78ab8f67
AD
211 fprintf (out, "\n\n");
212 fprintf (out, "Rules\n-----\n\n");
213 fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
214 for (i = 1; i < nrules + nuseless_productions + 1; i++)
215 {
216 int rhs_count = 0;
217 /* Find the last RHS index in ritems. */
218 for (r = rules[i].rhs; *r >= 0; ++r)
219 ++rhs_count;
220 fprintf (out, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
221 i - 1,
222 rules[i].prec ? rules[i].prec->prec : 0,
223 rules[i].prec ? rules[i].prec->assoc : 0,
224 rules[i].useful,
225 rules[i].rhs - ritem,
226 rules[i].rhs - ritem + rhs_count - 1,
227 rules[i].lhs->number);
228 /* Dumped the RHS. */
229 for (r = rules[i].rhs; *r >= 0; r++)
230 fprintf (out, " %3d", *r);
231 fprintf (out, " [%d]\n", -(*r) - 1);
232 }
233 fprintf (out, "\n\n");
234 fprintf (out, "Rules interpreted\n-----------------\n\n");
235 for (i = 1; i < nrules + nuseless_productions + 1; i++)
236 {
6b98e4b5
AD
237 fprintf (out, "%-5d ", i);
238 rule_print (&rules[i], out);
78ab8f67
AD
239 }
240 fprintf (out, "\n\n");
241}
5372019f
AD
242
243
244void
245grammar_free (void)
246{
247 XFREE (ritem);
248 free (rules + 1);
249 XFREE (token_translations);
250 /* Free the symbol table data structure. */
251 symbols_free ();
252}