2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1995-09-04 12:33:28 adam
8 * Various cleanup. YAZ util used instead.
10 * Revision 1.3 1995/01/25 11:30:51 adam
11 * Simple error reporting when parsing regular expressions.
12 * Memory usage reduced.
14 * Revision 1.2 1995/01/24 16:00:23 adam
15 * Added -ansi to CFLAGS.
16 * Some changes to the dfa module.
18 * Revision 1.1 1994/09/26 10:16:58 adam
19 * First version of dfa module in alex. This version uses yacc to parse
20 * regular expressions. This should be hand-made instead.
34 #define TRAN_CHUNK 100
36 int init_DFA_states (struct DFA_states **dfasp, SetType st, int hash)
38 struct DFA_states *dfas;
42 dfas = (struct DFA_states *) imalloc (sizeof(struct DFA_states));
44 dfas->hasharray = (struct DFA_state **)
45 imalloc (sizeof(struct DFA_state*) * hash);
46 assert (dfas->hasharray);
48 dfas->freelist = dfas->unmarked = dfas->marked = NULL;
49 dfas->statemem = NULL;
54 dfas->transmem = tm = (struct DFA_trans *)
55 imalloc (sizeof(struct DFA_trans));
58 tm->size = TRAN_CHUNK;
60 tm->tran_block = (struct DFA_tran *)
61 imalloc (sizeof(struct DFA_tran) * tm->size);
62 assert (tm->tran_block);
64 dfas->sortarray = NULL;
65 for (i=0; i<dfas->hash; i++)
66 dfas->hasharray[i] = NULL;
70 int rm_DFA_states (struct DFA_states **dfasp)
72 struct DFA_states *dfas = *dfasp;
74 struct DFA_trans *tm, *tm1;
78 ifree (dfas->hasharray);
79 ifree (dfas->sortarray);
81 for (tm=dfas->transmem; tm; tm=tm1)
83 ifree (tm->tran_block);
87 for (sm=dfas->statemem; sm; sm=sm1)
89 ifree (sm->state_block);
98 int add_DFA_state (struct DFA_states *dfas, Set *s, struct DFA_state **sp)
101 struct DFA_state *si, **sip;
106 assert (dfas->hasharray);
107 sip = dfas->hasharray + (hash_Set (dfas->st, *s) % dfas->hash);
108 for (si = *sip; si; si=si->link)
109 if (eq_Set (dfas->st, si->set, *s))
112 *s = rm_Set (dfas->st, *s);
117 sb = (DFA_stateb *) imalloc (sizeof(*sb));
118 sb->next = dfas->statemem;
120 sb->state_block = si = dfas->freelist =
121 (struct DFA_state *) imalloc (sizeof(struct DFA_state)*DFA_CHUNK);
122 for (i = 0; i<DFA_CHUNK-1; i++, si++)
128 dfas->freelist = si->next;
130 si->next = dfas->unmarked;
136 si->no = (dfas->no)++;
144 void add_DFA_tran (struct DFA_states *dfas, struct DFA_state *s,
145 int ch0, int ch1, int to)
147 struct DFA_trans *tm;
151 if (tm->ptr == tm->size)
153 tm = (struct DFA_trans *) imalloc (sizeof(struct DFA_trans));
155 tm->next = dfas->transmem;
157 tm->size = s->tran_no >= TRAN_CHUNK ? s->tran_no+8 : TRAN_CHUNK;
158 tm->tran_block = (struct DFA_tran *)
159 imalloc (sizeof(struct DFA_tran) * tm->size);
160 assert (tm->tran_block);
162 memcpy (tm->tran_block, s->trans,
163 s->tran_no*sizeof (struct DFA_tran));
164 tm->ptr = s->tran_no;
165 s->trans = tm->tran_block;
168 t = tm->tran_block + tm->ptr++;
174 struct DFA_state *get_DFA_state (struct DFA_states *dfas)
176 struct DFA_state *si;
178 if (!(si = dfas->unmarked))
180 dfas->unmarked = si->next;
181 si->next = dfas->marked;
183 si->trans = dfas->transmem->tran_block + dfas->transmem->ptr;
188 void sort_DFA_states (struct DFA_states *dfas)
192 dfas->sortarray = (struct DFA_state **)
193 imalloc (sizeof(struct DFA_state *)*dfas->no);
194 for (s = dfas->marked; s; s=s->next)
195 dfas->sortarray[s->no] = s;
196 ifree (dfas->hasharray);
197 dfas->hasharray = NULL;