]>
git.saurik.com Git - bison.git/blob - src/gram.c
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;
35 rule_number_t nrules
= 0;
37 symbol_t
**symbols
= NULL
;
42 symbol_number_t
*token_translations
= NULL
;
44 int max_user_token_number
= 256;
50 /*----------------------------------------------------------------.
51 | Print this RULE's number and lhs on OUT. If a PREVIOUS_LHS was |
52 | already displayed (by a previous call for another rule), avoid |
53 | useless repetitions. |
54 `----------------------------------------------------------------*/
57 rule_lhs_print (rule_t
*rule
, symbol_t
*previous_lhs
, FILE *out
)
59 fprintf (out
, " %3d ", rule
->number
);
60 if (previous_lhs
!= rule
->lhs
)
62 fprintf (out
, "%s:", rule
->lhs
->tag
);
67 for (n
= strlen (previous_lhs
->tag
); n
> 0; --n
)
74 /*--------------------------------------.
75 | Return the number of symbols in RHS. |
76 `--------------------------------------*/
79 rule_rhs_length (rule_t
*rule
)
83 for (rhsp
= rule
->rhs
; *rhsp
>= 0; ++rhsp
)
89 /*-------------------------------.
90 | Print this RULE's RHS on OUT. |
91 `-------------------------------*/
94 rule_rhs_print (rule_t
*rule
, FILE *out
)
99 for (r
= rule
->rhs
; *r
>= 0; r
++)
100 fprintf (out
, " %s", symbols
[*r
]->tag
);
105 fprintf (out
, " /* %s */\n", _("empty"));
110 /*-------------------------.
111 | Print this RULE on OUT. |
112 `-------------------------*/
115 rule_print (rule_t
*rule
, FILE *out
)
117 fprintf (out
, "%s:", rule
->lhs
->tag
);
118 rule_rhs_print (rule
, out
);
122 /*------------------------.
123 | Dump RITEM for traces. |
124 `------------------------*/
127 ritem_print (FILE *out
)
130 fputs ("RITEM\n", out
);
131 for (i
= 0; i
< nritems
; ++i
)
133 fprintf (out
, " %s", symbols
[ritem
[i
]]->tag
);
135 fprintf (out
, " (rule %d)\n", item_number_as_rule_number (ritem
[i
]));
140 /*------------------------------------------.
141 | Return the size of the longest rule RHS. |
142 `------------------------------------------*/
145 ritem_longest_rhs (void)
150 for (r
= 0; r
< nrules
; ++r
)
152 int length
= rule_rhs_length (&rules
[r
]);
161 /*----------------------------------------------------------------.
162 | Print the grammar's rules numbers from BEGIN (inclusive) to END |
163 | (exclusive) on OUT under TITLE. |
164 `----------------------------------------------------------------*/
167 grammar_rules_partial_print (FILE *out
, const char *title
,
168 rule_number_t begin
, rule_number_t end
)
171 symbol_t
*previous_lhs
= NULL
;
173 /* rule # : LHS -> RHS */
174 fprintf (out
, "%s\n\n", title
);
175 for (r
= begin
; r
< end
; r
++)
177 if (previous_lhs
&& previous_lhs
!= rules
[r
].lhs
)
179 rule_lhs_print (&rules
[r
], previous_lhs
, out
);
180 rule_rhs_print (&rules
[r
], out
);
181 previous_lhs
= rules
[r
].lhs
;
187 /*------------------------------------------.
188 | Print the grammar's useful rules on OUT. |
189 `------------------------------------------*/
192 grammar_rules_print (FILE *out
)
194 grammar_rules_partial_print (out
, _("Grammar"), 0, nrules
);
198 /*-------------------.
199 | Dump the grammar. |
200 `-------------------*/
203 grammar_dump (FILE *out
, const char *title
)
205 fprintf (out
, "%s\n\n", title
);
207 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
208 ntokens
, nvars
, nsyms
, nrules
, nritems
);
211 fprintf (out
, "Variables\n---------\n\n");
214 fprintf (out
, "Value Sprec Sassoc Tag\n");
216 for (i
= ntokens
; i
< nsyms
; i
++)
217 fprintf (out
, "%5d %5d %5d %s\n",
219 symbols
[i
]->prec
, symbols
[i
]->assoc
,
221 fprintf (out
, "\n\n");
224 fprintf (out
, "Rules\n-----\n\n");
227 fprintf (out
, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
228 for (i
= 0; i
< nrules
+ nuseless_productions
; i
++)
230 rule_t
*rule
= &rules
[i
];
231 item_number_t
*r
= NULL
;
233 /* Find the last RHS index in ritems. */
234 for (r
= rule
->rhs
; *r
>= 0; ++r
)
236 fprintf (out
, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
238 rule
->prec
? rule
->prec
->prec
: 0,
239 rule
->prec
? rule
->prec
->assoc
: 0,
242 rule
->rhs
- ritem
+ rhs_count
- 1,
244 /* Dumped the RHS. */
245 for (r
= rule
->rhs
; *r
>= 0; r
++)
246 fprintf (out
, " %3d", *r
);
247 fprintf (out
, " [%d]\n", item_number_as_rule_number (*r
));
250 fprintf (out
, "\n\n");
252 fprintf (out
, "Rules interpreted\n-----------------\n\n");
255 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
257 fprintf (out
, "%-5d ", r
);
258 rule_print (&rules
[r
], out
);
261 fprintf (out
, "\n\n");
270 XFREE (token_translations
);
271 /* Free the symbol table data structure. */
273 free_merger_functions ();