lalr: clarify the API

* src/state.h, src/state.c (state_reduction_find): Clarify.
Die on errors.
* src/lalr.c (goto_list_new): New.
Use it.
This commit is contained in:
Akim Demaille
2019-02-27 18:33:47 +01:00
parent c837141832
commit e062b9f70d
3 changed files with 14 additions and 6 deletions

View File

@@ -54,8 +54,16 @@ typedef struct goto_list
goto_number value;
} goto_list;
static goto_list *
goto_list_new (goto_number value, struct goto_list *next)
{
goto_list *res = xmalloc (sizeof *res);
res->next = next;
res->value = value;
return res;
}
/* LA is an NLA by NTOKENS matrix of bits. LA[l, i] is 1 if the rule
/* LA is an nLA by NTOKENS matrix of bits. LA[l, i] is 1 if the rule
LArule[l] is applicable in the appropriate state when the next
token is symbol i. If LA[l, i] and LA[l, j] are both 1 for i != j,
it is a conflict. */
@@ -232,10 +240,8 @@ static void
add_lookback_edge (state *s, rule const *r, goto_number gotono)
{
int ri = state_reduction_find (s, r);
goto_list *sp = xmalloc (sizeof *sp);
sp->next = lookback[(s->reductions->lookahead_tokens - LA) + ri];
sp->value = gotono;
lookback[(s->reductions->lookahead_tokens - LA) + ri] = sp;
int idx = (s->reductions->lookahead_tokens - LA) + ri;
lookback[idx] = goto_list_new (gotono, lookback[idx]);
}

View File

@@ -218,7 +218,7 @@ state_reduction_find (state *s, rule const *r)
for (int i = 0; i < reds->num; ++i)
if (reds->rules[i] == r)
return i;
return -1;
abort ();
}

View File

@@ -239,6 +239,8 @@ 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);
/* The index of the reduction of state S that corresponds to rule R.
Aborts if there is no reduction of R in S. */
int state_reduction_find (state *s, rule const *r);
/* Set the errs of STATE. */