]> git.saurik.com Git - bison.git/blob - src/gram.c
e6fb7089b08aa0819e6446dd019836f5728ec1c7
[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 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 token_number_t *token_translations = NULL;
43
44 token_number_t start_symbol = 0;
45
46 int max_user_token_number = 256;
47
48 int semantic_parser = 0;
49
50 int pure_parser = 0;
51
52
53 /*--------------------------------------.
54 | Return the number of symbols in RHS. |
55 `--------------------------------------*/
56
57 int
58 rule_rhs_length (rule_t *rule)
59 {
60 int res = 0;
61 item_number_t *rhsp;
62 for (rhsp = rule->rhs; *rhsp >= 0; ++rhsp)
63 ++res;
64 return res;
65 }
66
67
68 /*------------------------.
69 | Dump RITEM for traces. |
70 `------------------------*/
71
72 void
73 ritem_print (FILE *out)
74 {
75 int i;
76 fputs ("RITEM\n", out);
77 for (i = 0; i < nritems; ++i)
78 if (ritem[i] >= 0)
79 fprintf (out, " %s", quotearg_style (escape_quoting_style,
80 symbols[ritem[i]]->tag));
81 else
82 fprintf (out, " (rule %d)\n", -ritem[i] - 1);
83 fputs ("\n\n", out);
84 }
85
86
87 /*------------------------------------------.
88 | Return the size of the longest rule RHS. |
89 `------------------------------------------*/
90
91 size_t
92 ritem_longest_rhs (void)
93 {
94 int max = 0;
95 int i;
96
97 for (i = 1; i < nrules + 1; ++i)
98 {
99 int length = rule_rhs_length (&rules[i]);
100 if (length > max)
101 max = length;
102 }
103
104 return max;
105 }
106
107
108 /*-------------------.
109 | Dump the grammar. |
110 `-------------------*/
111
112 void
113 grammar_dump (FILE *out, const char *title)
114 {
115 int i;
116 item_number_t *r;
117
118 fprintf (out, "%s\n\n", title);
119 fprintf (out,
120 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
121 ntokens, nvars, nsyms, nrules, nritems);
122 fprintf (out, "Variables\n---------\n\n");
123 fprintf (out, "Value Sprec Sassoc Tag\n");
124 for (i = ntokens; i < nsyms; i++)
125 fprintf (out, "%5d %5d %5d %s\n",
126 i,
127 symbols[i]->prec, symbols[i]->assoc,
128 quotearg_style (escape_quoting_style, symbols[i]->tag));
129 fprintf (out, "\n\n");
130 fprintf (out, "Rules\n-----\n\n");
131 fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
132 for (i = 1; i < nrules + nuseless_productions + 1; i++)
133 {
134 int rhs_count = 0;
135 /* Find the last RHS index in ritems. */
136 for (r = rules[i].rhs; *r >= 0; ++r)
137 ++rhs_count;
138 fprintf (out, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
139 i - 1,
140 rules[i].prec ? rules[i].prec->prec : 0,
141 rules[i].prec ? rules[i].prec->assoc : 0,
142 rules[i].useful,
143 rules[i].rhs - ritem,
144 rules[i].rhs - ritem + rhs_count - 1,
145 rules[i].lhs->number);
146 /* Dumped the RHS. */
147 for (r = rules[i].rhs; *r >= 0; r++)
148 fprintf (out, " %3d", *r);
149 fprintf (out, " [%d]\n", -(*r) - 1);
150 }
151 fprintf (out, "\n\n");
152 fprintf (out, "Rules interpreted\n-----------------\n\n");
153 for (i = 1; i < nrules + nuseless_productions + 1; i++)
154 {
155 fprintf (out, "%-5d %s :",
156 i, quotearg_style (escape_quoting_style, rules[i].lhs->tag));
157 for (r = rules[i].rhs; *r >= 0; r++)
158 fprintf (out, " %s",
159 quotearg_style (escape_quoting_style, symbols[*r]->tag));
160 fputc ('\n', out);
161 }
162 fprintf (out, "\n\n");
163 }