style: factor printing of rules

* src/gram.h, src/gram.c (rule_print): New.
Use it.
This commit is contained in:
Akim Demaille
2019-02-09 07:15:56 +01:00
parent f293345aa8
commit ad7d8af6d1
4 changed files with 15 additions and 7 deletions

View File

@@ -155,6 +155,13 @@ rule_rhs_print_xml (rule const *r, FILE *out, int level)
}
}
void
rule_print (rule const *r, rule const *prev_rule, FILE *out)
{
rule_lhs_print (r, prev_rule ? prev_rule->lhs : NULL, out);
rule_rhs_print (r, out);
}
void
ritem_print (FILE *out)
{
@@ -186,7 +193,7 @@ grammar_rules_partial_print (FILE *out, const char *title,
rule_filter filter)
{
bool first = true;
sym_content *previous_lhs = NULL;
rule *previous_rule = NULL;
/* rule # : LHS -> RHS */
for (rule_number r = 0; r < nrules + nuseless_productions; r++)
@@ -195,13 +202,12 @@ grammar_rules_partial_print (FILE *out, const char *title,
continue;
if (first)
fprintf (out, "%s\n\n", title);
else if (previous_lhs && previous_lhs != rules[r].lhs)
else if (previous_rule && previous_rule->lhs != rules[r].lhs)
fputc ('\n', out);
first = false;
rule_lhs_print (&rules[r], previous_lhs, out);
rule_rhs_print (&rules[r], out);
rule_print (&rules[r], previous_rule, out);
fputc ('\n', out);
previous_lhs = rules[r].lhs;
previous_rule = &rules[r];
}
if (!first)
fputs ("\n\n", out);

View File

@@ -248,6 +248,9 @@ size_t rule_rhs_length (rule const *r);
/* Print this rule's RHS on OUT. */
void rule_rhs_print (rule const *r, FILE *out);
/* Print this rule on OUT. If a PREVIOUS_RULE was already displayed,
avoid useless repetitions of their LHS. */
void rule_print (rule const *r, rule const *prev_rule, FILE *out);

View File

@@ -241,7 +241,6 @@ build_relations (void)
rp--;
if (ISVAR (*rp))
{
/* Downcasting from item_number to symbol_number. */
edge[nedges++] = map_goto (states1[--length],
item_number_as_symbol_number (*rp));
if (nullable[*rp - ntokens])

View File

@@ -376,7 +376,7 @@ generate_states (void)
closure (s->items, s->nitems);
/* Record the reductions allowed out of this state. */
save_reductions (s);
/* Find the itemsets of the states that shifts can reach. */
/* Find the itemsets of the states that shifts/gotos can reach. */
new_itemsets (s);
/* Find or create the core structures for those states. */
append_states (s);