]>
git.saurik.com Git - bison.git/blob - src/gram.c
4d88b39b2c63cea367a8b009fedd6716b4a8488d
1 /* Allocate input grammar variables for bison,
2 Copyright (C) 1984, 1986, 1989, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
29 /* Comments for these variables are in gram.h. */
31 item_number_t
*ritem
= NULL
;
32 unsigned int nritems
= 0;
37 symbol_t
**symbols
= NULL
;
42 symbol_number_t
*token_translations
= NULL
;
44 int max_user_token_number
= 256;
50 /*--------------------------------------.
51 | Return the number of symbols in RHS. |
52 `--------------------------------------*/
55 rule_rhs_length (rule_t
*rule
)
59 for (rhsp
= rule
->rhs
; *rhsp
>= 0; ++rhsp
)
65 /*-------------------------------.
66 | Print this RULE's RHS on OUT. |
67 `-------------------------------*/
70 rule_rhs_print (rule_t
*rule
, FILE *out
)
75 for (r
= rule
->rhs
; *r
>= 0; r
++)
76 fprintf (out
, " %s", symbol_tag_get (symbols
[*r
]));
81 fprintf (out
, " /* %s */\n", _("empty"));
86 /*-------------------------.
87 | Print this RULE on OUT. |
88 `-------------------------*/
91 rule_print (rule_t
*rule
, FILE *out
)
93 fprintf (out
, "%s:", symbol_tag_get (rule
->lhs
));
94 rule_rhs_print (rule
, out
);
98 /*------------------------.
99 | Dump RITEM for traces. |
100 `------------------------*/
103 ritem_print (FILE *out
)
106 fputs ("RITEM\n", out
);
107 for (i
= 0; i
< nritems
; ++i
)
109 fprintf (out
, " %s", symbol_tag_get (symbols
[ritem
[i
]]));
111 fprintf (out
, " (rule %d)\n", -ritem
[i
] - 1);
116 /*------------------------------------------.
117 | Return the size of the longest rule RHS. |
118 `------------------------------------------*/
121 ritem_longest_rhs (void)
126 for (i
= 1; i
< nrules
+ 1; ++i
)
128 int length
= rule_rhs_length (&rules
[i
]);
137 /*----------------------------------------------------------------.
138 | Print the grammar's rules numbers from BEGIN (inclusive) to END |
139 | (exclusive) on OUT under TITLE. |
140 `----------------------------------------------------------------*/
143 blanks_print (unsigned n
, FILE *out
)
145 for (/* Nothing*/; n
> 0; --n
)
150 grammar_rules_partial_print (FILE *out
, const char *title
,
154 symbol_t
*last_lhs
= NULL
;
156 /* rule # : LHS -> RHS */
157 fprintf (out
, "%s\n\n", title
);
158 for (r
= begin
; r
< end
; r
++)
160 if (last_lhs
&& last_lhs
!= rules
[r
].lhs
)
163 fprintf (out
, " %3d ", r
- 1);
164 if (last_lhs
!= rules
[r
].lhs
)
166 last_lhs
= rules
[r
].lhs
;
167 fprintf (out
, "%s:", symbol_tag_get (last_lhs
));
171 blanks_print (strlen (symbol_tag_get (last_lhs
)), out
);
174 rule_rhs_print (&rules
[r
], out
);
180 /*------------------------------------------.
181 | Print the grammar's useful rules on OUT. |
182 `------------------------------------------*/
185 grammar_rules_print (FILE *out
)
187 grammar_rules_partial_print (out
, _("Grammar"), 1, nrules
+ 1);
191 /*-------------------.
192 | Dump the grammar. |
193 `-------------------*/
196 grammar_dump (FILE *out
, const char *title
)
201 fprintf (out
, "%s\n\n", title
);
203 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
204 ntokens
, nvars
, nsyms
, nrules
, nritems
);
205 fprintf (out
, "Variables\n---------\n\n");
206 fprintf (out
, "Value Sprec Sassoc Tag\n");
207 for (i
= ntokens
; i
< nsyms
; i
++)
208 fprintf (out
, "%5d %5d %5d %s\n",
210 symbols
[i
]->prec
, symbols
[i
]->assoc
,
211 symbol_tag_get (symbols
[i
]));
212 fprintf (out
, "\n\n");
213 fprintf (out
, "Rules\n-----\n\n");
214 fprintf (out
, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
215 for (i
= 1; i
< nrules
+ nuseless_productions
+ 1; i
++)
218 /* Find the last RHS index in ritems. */
219 for (r
= rules
[i
].rhs
; *r
>= 0; ++r
)
221 fprintf (out
, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
223 rules
[i
].prec
? rules
[i
].prec
->prec
: 0,
224 rules
[i
].prec
? rules
[i
].prec
->assoc
: 0,
226 rules
[i
].rhs
- ritem
,
227 rules
[i
].rhs
- ritem
+ rhs_count
- 1,
228 rules
[i
].lhs
->number
);
229 /* Dumped the RHS. */
230 for (r
= rules
[i
].rhs
; *r
>= 0; r
++)
231 fprintf (out
, " %3d", *r
);
232 fprintf (out
, " [%d]\n", -(*r
) - 1);
234 fprintf (out
, "\n\n");
235 fprintf (out
, "Rules interpreted\n-----------------\n\n");
236 for (i
= 1; i
< nrules
+ nuseless_productions
+ 1; i
++)
238 fprintf (out
, "%-5d ", i
);
239 rule_print (&rules
[i
], out
);
241 fprintf (out
, "\n\n");
250 XFREE (token_translations
);
251 /* Free the symbol table data structure. */
253 free_merger_functions ();