]> git.saurik.com Git - bison.git/blob - src/gram.c
* src/scan-gram.l (SC_BRACED_CODE): Don't use `<.*>', it is too
[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 | Dump RITEM for traces. |
66 `------------------------*/
67
68 void
69 ritem_print (FILE *out)
70 {
71 unsigned int i;
72 fputs ("RITEM\n", out);
73 for (i = 0; i < nritems; ++i)
74 if (ritem[i] >= 0)
75 fprintf (out, " %s", quotearg_style (escape_quoting_style,
76 symbols[ritem[i]]->tag));
77 else
78 fprintf (out, " (rule %d)\n", -ritem[i] - 1);
79 fputs ("\n\n", out);
80 }
81
82
83 /*------------------------------------------.
84 | Return the size of the longest rule RHS. |
85 `------------------------------------------*/
86
87 size_t
88 ritem_longest_rhs (void)
89 {
90 int max = 0;
91 int i;
92
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 }
99
100 return max;
101 }
102
103
104 /*-------------------.
105 | Dump the grammar. |
106 `-------------------*/
107
108 void
109 grammar_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 }
160
161
162 void
163 grammar_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 }