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