[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
lalr: clarify the API
From: |
Akim Demaille |
Subject: |
lalr: clarify the API |
Date: |
Fri, 1 Mar 2019 18:37:36 +0100 |
commit e062b9f70daa59f193d3cf7e59b77e8ae8bea656
Author: Akim Demaille <address@hidden>
Date: Wed Feb 27 18:33:47 2019 +0100
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.
diff --git a/src/lalr.c b/src/lalr.c
index 6a3ee044..035bf5c0 100644
--- a/src/lalr.c
+++ b/src/lalr.c
@@ -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]);
}
diff --git a/src/state.c b/src/state.c
index 781e49c3..b8b8e2ff 100644
--- a/src/state.c
+++ b/src/state.c
@@ -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 ();
}
diff --git a/src/state.h b/src/state.h
index 16066049..498323fe 100644
--- a/src/state.h
+++ b/src/state.h
@@ -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. */
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- lalr: clarify the API,
Akim Demaille <=