]> git.saurik.com Git - bison.git/blame - src/gram.c
Move symbols handling code out of the reader.
[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
f7d4d87a
DM
29/* comments for these variables are in gram.h */
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
c2713865
AD
64/*------------------------.
65| Dump RITEM for traces. |
66`------------------------*/
67
cbbe7505 68void
3067fbef 69ritem_print (FILE *out)
f7d4d87a 70{
0c2d3f4c 71 unsigned int i;
3067fbef 72 fputs ("RITEM\n", out);
75142d45
AD
73 for (i = 0; i < nritems; ++i)
74 if (ritem[i] >= 0)
8b3df748
AD
75 fprintf (out, " %s", quotearg_style (escape_quoting_style,
76 symbols[ritem[i]]->tag));
3067fbef 77 else
29d29c8f 78 fprintf (out, " (rule %d)\n", -ritem[i] - 1);
3067fbef 79 fputs ("\n\n", out);
f7d4d87a 80}
c2713865
AD
81
82
83/*------------------------------------------.
84| Return the size of the longest rule RHS. |
85`------------------------------------------*/
86
87size_t
88ritem_longest_rhs (void)
89{
c3b407f4 90 int max = 0;
9e7f6bbd 91 int i;
c2713865 92
c3b407f4
AD
93 for (i = 1; i < nrules + 1; ++i)
94 {
95 int length = rule_rhs_length (&rules[i]);
96 if (length > max)
97 max = length;
98 }
c2713865
AD
99
100 return max;
101}
78ab8f67
AD
102
103
104/*-------------------.
105| Dump the grammar. |
106`-------------------*/
107
108void
109grammar_dump (FILE *out, const char *title)
110{
111 int i;
112 item_number_t *r;
113
114 fprintf (out, "%s\n\n", title);
115 fprintf (out,
116 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
117 ntokens, nvars, nsyms, nrules, nritems);
118 fprintf (out, "Variables\n---------\n\n");
119 fprintf (out, "Value Sprec Sassoc Tag\n");
120 for (i = ntokens; i < nsyms; i++)
121 fprintf (out, "%5d %5d %5d %s\n",
122 i,
123 symbols[i]->prec, symbols[i]->assoc,
124 quotearg_style (escape_quoting_style, symbols[i]->tag));
125 fprintf (out, "\n\n");
126 fprintf (out, "Rules\n-----\n\n");
127 fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
128 for (i = 1; i < nrules + nuseless_productions + 1; i++)
129 {
130 int rhs_count = 0;
131 /* Find the last RHS index in ritems. */
132 for (r = rules[i].rhs; *r >= 0; ++r)
133 ++rhs_count;
134 fprintf (out, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
135 i - 1,
136 rules[i].prec ? rules[i].prec->prec : 0,
137 rules[i].prec ? rules[i].prec->assoc : 0,
138 rules[i].useful,
139 rules[i].rhs - ritem,
140 rules[i].rhs - ritem + rhs_count - 1,
141 rules[i].lhs->number);
142 /* Dumped the RHS. */
143 for (r = rules[i].rhs; *r >= 0; r++)
144 fprintf (out, " %3d", *r);
145 fprintf (out, " [%d]\n", -(*r) - 1);
146 }
147 fprintf (out, "\n\n");
148 fprintf (out, "Rules interpreted\n-----------------\n\n");
149 for (i = 1; i < nrules + nuseless_productions + 1; i++)
150 {
151 fprintf (out, "%-5d %s :",
152 i, quotearg_style (escape_quoting_style, rules[i].lhs->tag));
153 for (r = rules[i].rhs; *r >= 0; r++)
154 fprintf (out, " %s",
155 quotearg_style (escape_quoting_style, symbols[*r]->tag));
156 fputc ('\n', out);
157 }
158 fprintf (out, "\n\n");
159}
5372019f
AD
160
161
162void
163grammar_free (void)
164{
165 XFREE (ritem);
166 free (rules + 1);
167 XFREE (token_translations);
168 /* Free the symbol table data structure. */
169 symbols_free ();
170}