gram: factor the printing of items and the computation of their rule

There are several places where we need to recover the rule from an
item, let's factor that into item_rule.  We also want to print items
in a nice way: we do it when generating the *output file, but it is
also useful in debug messages.

* src/gram.h, src/gram.c (item_rule, item_print): New.
* src/print.c (print_core): Use them.
* src/state.h, src/state.c: Propagate constness.
This commit is contained in:
Akim Demaille
2019-01-28 19:05:38 +01:00
parent c4f143eb96
commit e1783bc686
6 changed files with 53 additions and 31 deletions

View File

@@ -192,12 +192,12 @@ closure (item_number const *core, size_t n)
/* core is sorted on item index in ritem, which is sorted on rule number.
Compute itemset with the same sort. */
nitemset = 0;
size_t c = 0;
/* A bit index over RULESET. */
rule_number ruleno;
bitset_iterator iter;
nitemset = 0;
size_t c = 0;
BITSET_FOR_EACH (iter, ruleset, ruleno, 0)
{
item_number itemno = rules[ruleno].rhs - ritem;

View File

@@ -48,6 +48,35 @@ int max_user_token_number = 256;
int required_version = 0;
rule const *
item_rule (item_number const *item)
{
item_number const *sp = item;
while (*sp >= 0)
++sp;
rule_number r = item_number_as_rule_number (*sp);
return &rules[r];
}
void
item_print (item_number *item, rule const *previous_rule, FILE *out)
{
rule const *r = item_rule (item);
rule_lhs_print (r, previous_rule ? previous_rule->lhs : NULL, out);
for (item_number *sp = r->rhs; sp < item; sp++)
fprintf (out, " %s", symbols[*sp]->tag);
fputs (" .", out);
if (0 <= *r->rhs)
for (item_number *sp = item; 0 <= *sp; ++sp)
fprintf (out, " %s", symbols[*sp]->tag);
else
fprintf (out, " %%empty");
}
bool
rule_useful_in_grammar_p (rule const *r)
{

View File

@@ -166,6 +166,7 @@ item_number_is_rule_number (item_number i)
return i < 0;
}
/*--------.
| Rules. |
`--------*/
@@ -207,6 +208,15 @@ typedef struct
extern rule *rules;
/* Get the rule associated to this item. ITEM points inside RITEM. */
rule const *item_rule (item_number const *item);
/* Pretty-print this ITEM (as in the report). ITEM points inside
RITEM. PREVIOUS_RULE is used to see if the lhs is common, in which
case LHS is factored. Passing NULL is fine. */
void item_print (item_number *item, rule const *previous_rule,
FILE *out);
/* A function that selects a rule. */
typedef bool (*rule_filter) (rule const *);

View File

@@ -63,8 +63,6 @@ print_core (FILE *out, state *s)
{
item_number *sitems = s->items;
size_t snritems = s->nitems;
sym_content *previous_lhs = NULL;
/* Output all the items of a state, not only its kernel. */
if (report_flag & report_itemsets)
{
@@ -78,33 +76,18 @@ print_core (FILE *out, state *s)
fputc ('\n', out);
rule const *previous_rule = NULL;
for (size_t i = 0; i < snritems; i++)
{
item_number *sp1 = ritem + sitems[i];
item_number *sp = sp1;
while (*sp >= 0)
sp++;
rule_number r = item_number_as_rule_number (*sp);
rule_lhs_print (&rules[r], previous_lhs, out);
previous_lhs = rules[r].lhs;
for (sp = rules[r].rhs; sp < sp1; sp++)
fprintf (out, " %s", symbols[*sp]->tag);
fputs (" .", out);
if (0 <= *rules[r].rhs)
for (/* Nothing */; 0 <= *sp; ++sp)
fprintf (out, " %s", symbols[*sp]->tag);
else
fprintf (out, " %%empty");
rule const *r = item_rule (sp1);
item_print (sp1, previous_rule, out);
previous_rule = r;
/* Display the lookahead tokens? */
if (report_flag & report_lookahead_tokens
&& item_number_is_rule_number (*sp1))
state_rule_lookahead_tokens_print (s, &rules[r], out);
state_rule_lookahead_tokens_print (s, r, out);
fputc ('\n', out);
}
}

View File

@@ -218,7 +218,7 @@ state_reductions_set (state *s, int num, rule **reds)
int
state_reduction_find (state *s, rule *r)
state_reduction_find (state *s, rule const *r)
{
reductions *reds = s->reductions;
for (int i = 0; i < reds->num; ++i)
@@ -247,7 +247,7 @@ state_errs_set (state *s, int num, symbol **tokens)
`--------------------------------------------------*/
void
state_rule_lookahead_tokens_print (state *s, rule *r, FILE *out)
state_rule_lookahead_tokens_print (state *s, rule const *r, FILE *out)
{
/* Find the reduction we are handling. */
reductions *reds = s->reductions;
@@ -270,7 +270,7 @@ state_rule_lookahead_tokens_print (state *s, rule *r, FILE *out)
}
void
state_rule_lookahead_tokens_print_xml (state *s, rule *r,
state_rule_lookahead_tokens_print_xml (state *s, rule const *r,
FILE *out, int level)
{
/* Find the reduction we are handling. */

View File

@@ -239,15 +239,15 @@ void state_transitions_set (state *s, int num, state **dst);
/* Set the reductions of STATE. */
void state_reductions_set (state *s, int num, rule **reds);
int state_reduction_find (state *s, rule *r);
int state_reduction_find (state *s, rule const *r);
/* Set the errs of STATE. */
void state_errs_set (state *s, int num, symbol **errors);
/* Print on OUT all the lookahead tokens such that this STATE wants to
reduce R. */
void state_rule_lookahead_tokens_print (state *s, rule *r, FILE *out);
void state_rule_lookahead_tokens_print_xml (state *s, rule *r,
void state_rule_lookahead_tokens_print (state *s, rule const *r, FILE *out);
void state_rule_lookahead_tokens_print_xml (state *s, rule const *r,
FILE *out, int level);
/* Create/destroy the states hash table. */