]>
git.saurik.com Git - bison.git/blob - src/gram.c
1 /* Allocate input grammar variables for Bison.
3 Copyright (C) 1984, 1986, 1989, 2001-2003, 2005-2013 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "print-xml.h"
32 /* Comments for these variables are in gram.h. */
34 item_number
*ritem
= NULL
;
35 unsigned int nritems
= 0;
38 rule_number nrules
= 0;
40 symbol
**symbols
= NULL
;
45 symbol_number
*token_translations
= NULL
;
47 int max_user_token_number
= 256;
50 rule_useful_in_grammar_p (rule
const *r
)
52 return r
->number
< nrules
;
56 rule_useless_in_grammar_p (rule
const *r
)
58 return !rule_useful_in_grammar_p (r
);
62 rule_useless_in_parser_p (rule
const *r
)
64 return !r
->useful
&& rule_useful_in_grammar_p (r
);
68 rule_lhs_print (rule
const *r
, symbol
const *previous_lhs
, FILE *out
)
70 fprintf (out
, " %3d ", r
->number
);
71 if (previous_lhs
!= r
->lhs
)
72 fprintf (out
, "%s:", r
->lhs
->tag
);
74 fprintf (out
, "%*s|", (int) strlen (previous_lhs
->tag
), "");
78 rule_lhs_print_xml (rule
const *r
, FILE *out
, int level
)
80 xml_printf (out
, level
, "<lhs>%s</lhs>", r
->lhs
->tag
);
84 rule_rhs_length (rule
const *r
)
88 for (rhsp
= r
->rhs
; *rhsp
>= 0; ++rhsp
)
94 rule_rhs_print (rule
const *r
, FILE *out
)
99 for (rp
= r
->rhs
; *rp
>= 0; rp
++)
100 fprintf (out
, " %s", symbols
[*rp
]->tag
);
104 fprintf (out
, " /* %s */", _("empty"));
109 rule_rhs_print_xml (rule
const *r
, FILE *out
, int level
)
114 xml_puts (out
, level
, "<rhs>");
115 for (rp
= r
->rhs
; *rp
>= 0; rp
++)
116 xml_printf (out
, level
+ 1, "<symbol>%s</symbol>",
117 xml_escape (symbols
[*rp
]->tag
));
118 xml_puts (out
, level
, "</rhs>");
122 xml_puts (out
, level
, "<rhs>");
123 xml_puts (out
, level
+ 1, "<empty/>");
124 xml_puts (out
, level
, "</rhs>");
129 ritem_print (FILE *out
)
132 fputs ("RITEM\n", out
);
133 for (i
= 0; i
< nritems
; ++i
)
135 fprintf (out
, " %s", symbols
[ritem
[i
]]->tag
);
137 fprintf (out
, " (rule %d)\n", item_number_as_rule_number (ritem
[i
]));
142 ritem_longest_rhs (void)
147 for (r
= 0; r
< nrules
; ++r
)
149 int length
= rule_rhs_length (&rules
[r
]);
158 grammar_rules_partial_print (FILE *out
, const char *title
,
163 symbol
*previous_lhs
= NULL
;
165 /* rule # : LHS -> RHS */
166 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
168 if (filter
&& !filter (&rules
[r
]))
171 fprintf (out
, "%s\n\n", title
);
172 else if (previous_lhs
&& previous_lhs
!= rules
[r
].lhs
)
175 rule_lhs_print (&rules
[r
], previous_lhs
, out
);
176 rule_rhs_print (&rules
[r
], out
);
178 previous_lhs
= rules
[r
].lhs
;
185 grammar_rules_print (FILE *out
)
187 grammar_rules_partial_print (out
, _("Grammar"), rule_useful_in_grammar_p
);
191 grammar_rules_print_xml (FILE *out
, int level
)
196 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
199 xml_puts (out
, level
+ 1, "<rules>");
202 char const *usefulness
;
203 if (rule_useless_in_grammar_p (&rules
[r
]))
204 usefulness
= "useless-in-grammar";
205 else if (rule_useless_in_parser_p (&rules
[r
]))
206 usefulness
= "useless-in-parser";
208 usefulness
= "useful";
209 xml_indent (out
, level
+ 2);
210 fprintf (out
, "<rule number=\"%d\" usefulness=\"%s\"",
211 rules
[r
].number
, usefulness
);
212 if (rules
[r
].precsym
)
213 fprintf (out
, " percent_prec=\"%s\"",
214 xml_escape (rules
[r
].precsym
->tag
));
217 rule_lhs_print_xml (&rules
[r
], out
, level
+ 3);
218 rule_rhs_print_xml (&rules
[r
], out
, level
+ 3);
219 xml_puts (out
, level
+ 2, "</rule>");
222 xml_puts (out
, level
+ 1, "</rules>");
224 xml_puts (out
, level
+ 1, "<rules/>");
228 grammar_dump (FILE *out
, const char *title
)
230 fprintf (out
, "%s\n\n", title
);
232 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
233 ntokens
, nvars
, nsyms
, nrules
, nritems
);
236 fprintf (out
, "Variables\n---------\n\n");
239 fprintf (out
, "Value Sprec Sassoc Tag\n");
241 for (i
= ntokens
; i
< nsyms
; i
++)
242 fprintf (out
, "%5d %5d %5d %s\n",
244 symbols
[i
]->prec
, symbols
[i
]->assoc
,
246 fprintf (out
, "\n\n");
249 fprintf (out
, "Rules\n-----\n\n");
253 "Num (Prec, Assoc, Useful, Ritem Range) Lhs"
254 " -> Rhs (Ritem range) [Num]\n");
255 for (i
= 0; i
< nrules
+ nuseless_productions
; i
++)
257 rule
const *rule_i
= &rules
[i
];
258 item_number
*rp
= NULL
;
259 unsigned int rhs_itemno
= rule_i
->rhs
- ritem
;
260 unsigned int rhs_count
= 0;
261 /* Find the last RHS index in ritems. */
262 for (rp
= rule_i
->rhs
; *rp
>= 0; ++rp
)
264 fprintf (out
, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->",
266 rule_i
->prec
? rule_i
->prec
->prec
: 0,
267 rule_i
->prec
? rule_i
->prec
->assoc
: 0,
270 rhs_itemno
+ rhs_count
- 1,
271 rule_i
->lhs
->number
);
272 /* Dumped the RHS. */
273 for (rp
= rule_i
->rhs
; *rp
>= 0; rp
++)
274 fprintf (out
, " %3d", *rp
);
275 fprintf (out
, " [%d]\n", item_number_as_rule_number (*rp
));
278 fprintf (out
, "\n\n");
280 fprintf (out
, "Rules interpreted\n-----------------\n\n");
283 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
285 fprintf (out
, "%-5d %s:", r
, rules
[r
].lhs
->tag
);
286 rule_rhs_print (&rules
[r
], out
);
290 fprintf (out
, "\n\n");
294 grammar_rules_useless_report (const char *message
)
297 for (r
= 0; r
< nrules
; ++r
)
298 if (!rules
[r
].useful
)
299 complain (&rules
[r
].location
, Wother
, "%s", message
);
308 free (token_translations
);
309 /* Free the symbol table data structure. */
311 free_merger_functions ();