1 /* Copyright (C) 2006, Index Data ApS
2 * See the file LICENSE for details.
4 * $Id: nfa.c,v 1.5 2006-05-04 18:58:54 adam Exp $
9 * \brief NFA for character set normalizing
11 * This is a simple NFA-based system for character set normalizing
12 * in yaz and zebra. Unlike standard NFA's, this operates on ranges of
13 * characters at a time, so as to keep the size small.
15 * All characters are internally handled as unsigned 32-bit ints, so
16 * this works well with unicode. Translating to and from utf-8 is trivial, if
19 * The NFA stores a translation-thing in the terminal nodes. It does not
20 * concern itself with what that thing is, for us it is juts a void*
22 * The module consists of two parts: Building the NFA, and translating
34 typedef struct yaz_nfa_backref_info yaz_backref_info;
36 struct yaz_nfa_backref_info {
43 int nstates; /* how many states do we have */
44 int nbackrefs; /* how many backrefs do we have */
45 yaz_nfa_state *laststate; /* points to the last in the circular list */
46 yaz_nfa_state *firststate; /* the initial state */
47 yaz_backref_info *curr_backrefs; /* the backrefs we are matching */
48 yaz_backref_info *best_backrefs; /* backrefs of the best match so far*/
49 int lastmatch; /* the result of last match */
52 struct yaz_nfa_state {
53 int num; /* state number. used for resoving ambiguities, and for dumping */
54 void *result; /* signals a terminal state, and gives the result */
55 yaz_nfa_transition *lasttrans; /* circular list of transitions */
56 yaz_nfa_state *next; /* Circular linked list */
57 int backref_start; /* which backreference starts here. 0 if none */
58 int backref_end; /* which backreference ends here. 0 if none */
61 struct yaz_nfa_transition {
62 yaz_nfa_state *to_state; /* where to */
63 yaz_nfa_char range_start;
64 yaz_nfa_char range_end;
65 yaz_nfa_transition *next; /* a linked list of them */
68 /* a range from 1 down to 0 is impossible, and is used to denote an */
69 /* empty (epsilon) transition */
73 /* a limit for the number of empty transitions we can have in a row before */
74 /* we declare this a loop */
75 #define LOOP_LIMIT 100
81 * Initializing and destroying whole nfa's
84 yaz_nfa *yaz_nfa_init() {
85 NMEM my_nmem = nmem_create();
86 yaz_nfa *n = nmem_malloc(my_nmem, sizeof(yaz_nfa));
94 n->lastmatch = YAZ_NFA_NOMATCH;
98 void yaz_nfa_destroy(yaz_nfa *n) {
99 nmem_destroy(n->nmem);
104 * Low-level interface to building the NFA
107 yaz_nfa_state *yaz_nfa_add_state(yaz_nfa *n) {
108 yaz_nfa_state *s = nmem_malloc(n->nmem, sizeof(yaz_nfa_state));
109 s->num = (n->nstates)++;
112 s->backref_start = 0;
115 s->next = n->laststate->next;
116 n->laststate->next = s;
118 } else { /* first state */
126 int yaz_nfa_set_result(yaz_nfa *n, yaz_nfa_state *s, void *result) {
127 if ((s->result)&&result)
133 void *yaz_nfa_get_result(yaz_nfa *n, yaz_nfa_state *s) {
139 int yaz_nfa_set_backref_point(yaz_nfa *n, yaz_nfa_state *s,
143 if (s->backref_start)
145 s->backref_start = backref_number;
146 if (n->nbackrefs<=backref_number) {
147 n->nbackrefs = backref_number+1;
148 n->curr_backrefs = 0;
149 n->best_backrefs = 0;
150 /* clear them just in case we have already matched on */
151 /* with this nfa, and created a too small backref table */
152 /* we will reallocate when matching. */
157 if (n->nbackrefs<backref_number)
158 return 2; /* can't end a backref that has not been started */
159 s->backref_end = backref_number;
164 int yaz_nfa_get_backref_point(yaz_nfa *n, yaz_nfa_state *s,
169 return s->backref_start;
171 return s->backref_end;
174 void yaz_nfa_add_transition(yaz_nfa *n,
175 yaz_nfa_state *from_state,
176 yaz_nfa_state *to_state,
177 yaz_nfa_char range_start,
178 yaz_nfa_char range_end) {
179 yaz_nfa_transition *t = nmem_malloc(n->nmem, sizeof(yaz_nfa_transition));
180 t->range_start = range_start;
181 t->range_end = range_end;
182 t->to_state = to_state;
183 if (from_state->lasttrans) {
184 t->next= from_state->lasttrans->next;
185 from_state->lasttrans->next = t;
186 from_state->lasttrans = t;
187 } else { /* first trans */
188 from_state->lasttrans = t;
193 void yaz_nfa_add_empty_transition( yaz_nfa *n,
194 yaz_nfa_state *from_state,
195 yaz_nfa_state *to_state) {
196 yaz_nfa_add_transition(n, from_state, to_state,
197 EMPTY_START, EMPTY_END);
201 * Medium-level interface
204 /* Finds a transition from s where the range is exactly c..c */
205 /* There should only be one such transition */
206 static yaz_nfa_state *find_single_trans(
208 yaz_nfa_char range_start,
209 yaz_nfa_char range_end) {
210 yaz_nfa_transition *t = s->lasttrans;
215 if ( ( t->range_start == range_start ) && ( t->range_end == range_end) )
217 } while (t != s->lasttrans );
222 yaz_nfa_state *yaz_nfa_add_range(yaz_nfa *n,
224 yaz_nfa_char range_start,
225 yaz_nfa_char range_end) {
226 yaz_nfa_state *nextstate;
227 if (!s) /* default to top-level of the nfa */
229 nextstate = find_single_trans(s, range_start, range_end);
231 nextstate = yaz_nfa_add_state(n);
232 yaz_nfa_add_transition(n, s, nextstate, range_start, range_end);
237 yaz_nfa_state *yaz_nfa_add_sequence(yaz_nfa *n,
240 yaz_nfa_state *nextstate;
241 if (!s) /* default to top-level of the nfa */
243 nextstate = find_single_trans(s, *seq, *seq);
246 if (!*seq) /* whole sequence matched */
249 return yaz_nfa_add_sequence(n, nextstate, seq);
250 } else { /* no next state, build the rest */
252 s = yaz_nfa_add_range(n, s, *seq, *seq);
257 return 0; /* compiler shut up, we return somewhere above */
268 yaz_nfa_char *longest;
272 int empties; /* count how many consecutive empty transitions */
275 /* Finds the longest match. In case of ties, chooses the one with the
276 * lowest numbered state. Keep track of the back references. Recursively
277 * traverses the NFA. Keeps track of the best hit it has found. */
279 static void match_state(
281 yaz_nfa_char *inchar,
283 struct matcher *m ) {
284 yaz_nfa_transition *t = s->lasttrans;
285 if (s->backref_start)
286 m->n->curr_backrefs[s->backref_start].start = inchar;
288 m->n->curr_backrefs[s->backref_end].end = inchar;
293 if ( (( t->range_start <= *inchar ) && ( t->range_end >= *inchar )) ){
295 if (t->range_start!=t->range_end){
296 /* backref 0 is special: the last range operation */
297 m->n->curr_backrefs[0].start = inchar;
298 m->n->curr_backrefs[0].end = inchar;
300 match_state(t->to_state, inchar+1, incharsleft-1, m);
301 /* yes, descent to all matching nodes, even if overrun, */
302 /* since we can find a better one later */
303 } else if (( t->range_start==EMPTY_START) && (t->range_end==EMPTY_END)) {
304 if ( m->empties++ > LOOP_LIMIT )
305 m->errorcode= YAZ_NFA_LOOP;
307 match_state(t->to_state, inchar, incharsleft, m);
309 } while (t != s->lasttrans );
311 m->errorcode = YAZ_NFA_OVERRUN;
314 if (s->result) { /* terminal node */
315 if ( (m->longest < inchar) || /* longer result */
316 ((m->longest == inchar)&&(m->bestnode<s->num)) ){
317 /* or as long, but with lower node number. Still better */
320 m->bestnode = s->num;
321 m->result = s->result;
322 if (m->n->curr_backrefs)
323 for (i = 0; i<m->n->nbackrefs; i++) {
324 m->n->best_backrefs[i]=m->n->curr_backrefs[i];
328 if (s->backref_start)
329 m->n->curr_backrefs[s->backref_start].start = 0;
331 m->n->curr_backrefs[s->backref_end].end = 0;
332 m->n->curr_backrefs[0].start = 0;
333 m->n->curr_backrefs[0].end = 0;
336 int yaz_nfa_match(yaz_nfa *n,
337 yaz_nfa_char **inbuff,
343 if (!n->firststate) {
344 n->lastmatch = YAZ_NFA_NOMATCH;
349 m.bestnode = n->nstates;
352 sz = sizeof( struct yaz_nfa_backref_info) * n->nbackrefs;
353 if (!n->curr_backrefs) {
354 n->curr_backrefs = nmem_malloc( n->nmem, sz);
355 n->best_backrefs = nmem_malloc( n->nmem, sz);
357 for (i = 0; i<n->nbackrefs; i++) {
358 n->curr_backrefs[i].start = 0;
359 n->curr_backrefs[i].end = 0;
360 n->best_backrefs[i].start = 0;
361 n->best_backrefs[i].end = 0;
364 match_state(n->firststate, *inbuff, incharsleft, &m);
369 n->lastmatch = m.errorcode;
371 n->lastmatch= YAZ_NFA_SUCCESS;
374 n->lastmatch = YAZ_NFA_NOMATCH;
379 int yaz_nfa_get_backref( yaz_nfa *n,
381 yaz_nfa_char **start,
382 yaz_nfa_char **end) {
383 if (backref_no>=n->nbackrefs)
387 if (n->lastmatch== YAZ_NFA_NOMATCH)
388 return 1; /* accept other errors, they return partial matches*/
390 *start = n->best_backrefs[backref_no].start;
391 *end = n->best_backrefs[backref_no].end;
400 yaz_nfa_state *yaz_nfa_get_first(yaz_nfa *n){
403 return n->firststate;
406 yaz_nfa_state *yaz_nfa_get_next(yaz_nfa *n, yaz_nfa_state *s){
416 static void dump_trans(FILE *F, yaz_nfa_transition *t ) {
423 if ( (t->range_start <= ' ') || (t->range_start>'z'))
425 if ( (t->range_end <= ' ') || (t->range_end>'z'))
427 if ((t->range_start==EMPTY_START) && (t->range_end==EMPTY_END)) {
430 fprintf(F, " -> [%d] %s '%c' %x - '%c' %x \n",
436 static void dump_state(FILE *F, yaz_nfa_state *s,
437 char *(*strfunc)(void *) ) {
438 yaz_nfa_transition *t;
439 char *resultstring = "";
442 resultstring = (*strfunc)(s->result);
445 resultstring = s->result;
447 fprintf(F, " state [%d] %s %s",
448 s->num, s->result?"(FINAL)":"", resultstring );
449 if (s->backref_start) {
450 fprintf(F, " start-%d", s->backref_start);
452 if (s->backref_end) {
453 fprintf(F, " end-%d", s->backref_end);
458 fprintf(F, " (no transitions)\n");
463 } while (t != s->lasttrans);
468 void yaz_nfa_dump(FILE *F, yaz_nfa *n,
469 char *(*strfunc)(void *) ) {
471 if (!F) /* lazy programmers can just pass 0 for F */
473 fprintf(F, "The NFA has %d states and %d backrefs\n",
474 n->nstates, n->nbackrefs);
479 dump_state(F, s, strfunc);
480 } while (s != n->laststate);
488 * indent-tabs-mode: nil
490 * vim: shiftwidth=4 tabstop=8 expandtab