1 /* Copyright (C) 2006, Index Data ApS
2 * See the file LICENSE for details.
4 * $Id: nfa.c,v 1.6 2006-05-05 09:14:42 heikki 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
82 } yaz_nfa_converter_type;
84 struct yaz_nfa_converter {
85 yaz_nfa_converter *next;
86 yaz_nfa_converter_type type;
95 * Initializing and destroying whole nfa's
98 yaz_nfa *yaz_nfa_init() {
99 NMEM my_nmem = nmem_create();
100 yaz_nfa *n = nmem_malloc(my_nmem, sizeof(yaz_nfa));
106 n->curr_backrefs = 0;
107 n->best_backrefs = 0;
108 n->lastmatch = YAZ_NFA_NOMATCH;
112 void yaz_nfa_destroy(yaz_nfa *n) {
113 nmem_destroy(n->nmem);
118 * Low-level interface to building the NFA
121 yaz_nfa_state *yaz_nfa_add_state(yaz_nfa *n) {
122 yaz_nfa_state *s = nmem_malloc(n->nmem, sizeof(yaz_nfa_state));
123 s->num = (n->nstates)++;
126 s->backref_start = 0;
129 s->next = n->laststate->next;
130 n->laststate->next = s;
132 } else { /* first state */
140 int yaz_nfa_set_result(yaz_nfa *n, yaz_nfa_state *s, void *result) {
141 if ((s->result)&&result)
147 void *yaz_nfa_get_result(yaz_nfa *n, yaz_nfa_state *s) {
153 int yaz_nfa_set_backref_point(yaz_nfa *n, yaz_nfa_state *s,
157 if (s->backref_start)
159 s->backref_start = backref_number;
160 if (n->nbackrefs<=backref_number) {
161 n->nbackrefs = backref_number+1;
162 n->curr_backrefs = 0;
163 n->best_backrefs = 0;
164 /* clear them just in case we have already matched on */
165 /* with this nfa, and created a too small backref table */
166 /* we will reallocate when matching. */
171 if (n->nbackrefs<backref_number)
172 return 2; /* can't end a backref that has not been started */
173 s->backref_end = backref_number;
178 int yaz_nfa_get_backref_point(yaz_nfa *n, yaz_nfa_state *s,
183 return s->backref_start;
185 return s->backref_end;
188 void yaz_nfa_add_transition(yaz_nfa *n,
189 yaz_nfa_state *from_state,
190 yaz_nfa_state *to_state,
191 yaz_nfa_char range_start,
192 yaz_nfa_char range_end) {
193 yaz_nfa_transition *t = nmem_malloc(n->nmem, sizeof(yaz_nfa_transition));
195 from_state = n->firststate;
196 t->range_start = range_start;
197 t->range_end = range_end;
198 t->to_state = to_state;
199 if (from_state->lasttrans) {
200 t->next= from_state->lasttrans->next;
201 from_state->lasttrans->next = t;
202 from_state->lasttrans = t;
203 } else { /* first trans */
204 from_state->lasttrans = t;
209 void yaz_nfa_add_empty_transition( yaz_nfa *n,
210 yaz_nfa_state *from_state,
211 yaz_nfa_state *to_state) {
212 yaz_nfa_add_transition(n, from_state, to_state,
213 EMPTY_START, EMPTY_END);
217 * Medium-level interface
220 /* Finds a transition from s where the range is exactly c..c */
221 /* There should only be one such transition */
222 static yaz_nfa_state *find_single_trans(
224 yaz_nfa_char range_start,
225 yaz_nfa_char range_end) {
226 yaz_nfa_transition *t = s->lasttrans;
231 if ( ( t->range_start == range_start ) && ( t->range_end == range_end) )
233 } while (t != s->lasttrans );
238 yaz_nfa_state *yaz_nfa_add_range(yaz_nfa *n,
240 yaz_nfa_char range_start,
241 yaz_nfa_char range_end) {
242 yaz_nfa_state *nextstate;
243 if (!s) /* default to top-level of the nfa */
245 nextstate = find_single_trans(s, range_start, range_end);
247 nextstate = yaz_nfa_add_state(n);
248 yaz_nfa_add_transition(n, s, nextstate, range_start, range_end);
253 yaz_nfa_state *yaz_nfa_add_sequence(yaz_nfa *n,
256 yaz_nfa_state *nextstate;
257 if (!s) /* default to top-level of the nfa */
259 nextstate = find_single_trans(s, *seq, *seq);
262 if (!*seq) /* whole sequence matched */
265 return yaz_nfa_add_sequence(n, nextstate, seq);
266 } else { /* no next state, build the rest */
268 s = yaz_nfa_add_range(n, s, *seq, *seq);
273 return 0; /* compiler shut up, we return somewhere above */
284 yaz_nfa_char *longest;
288 int empties; /* count how many consecutive empty transitions */
291 /* Finds the longest match. In case of ties, chooses the one with the
292 * lowest numbered state. Keep track of the back references. Recursively
293 * traverses the NFA. Keeps track of the best hit it has found. */
295 static void match_state(
297 yaz_nfa_char *inchar,
299 struct matcher *m ) {
300 yaz_nfa_transition *t = s->lasttrans;
301 if (s->backref_start)
302 m->n->curr_backrefs[s->backref_start].start = inchar;
304 m->n->curr_backrefs[s->backref_end].end = inchar;
309 if ( (( t->range_start <= *inchar ) && ( t->range_end >= *inchar )) ){
311 if (t->range_start!=t->range_end){
312 /* backref 0 is special: the last range operation */
313 m->n->curr_backrefs[0].start = inchar;
314 m->n->curr_backrefs[0].end = inchar;
316 match_state(t->to_state, inchar+1, incharsleft-1, m);
317 /* yes, descent to all matching nodes, even if overrun, */
318 /* since we can find a better one later */
319 } else if (( t->range_start==EMPTY_START) && (t->range_end==EMPTY_END)) {
320 if ( m->empties++ > LOOP_LIMIT )
321 m->errorcode= YAZ_NFA_LOOP;
323 match_state(t->to_state, inchar, incharsleft, m);
325 } while (t != s->lasttrans );
327 m->errorcode = YAZ_NFA_OVERRUN;
330 if (s->result) { /* terminal node */
331 if ( (m->longest < inchar) || /* longer result */
332 ((m->longest == inchar)&&(m->bestnode<s->num)) ){
333 /* or as long, but with lower node number. Still better */
336 m->bestnode = s->num;
337 m->result = s->result;
338 if (m->n->curr_backrefs)
339 for (i = 0; i<m->n->nbackrefs; i++) {
340 m->n->best_backrefs[i]=m->n->curr_backrefs[i];
344 if (s->backref_start)
345 m->n->curr_backrefs[s->backref_start].start = 0;
347 m->n->curr_backrefs[s->backref_end].end = 0;
348 m->n->curr_backrefs[0].start = 0;
349 m->n->curr_backrefs[0].end = 0;
352 int yaz_nfa_match(yaz_nfa *n,
353 yaz_nfa_char **inbuff,
359 if (!n->firststate) {
360 n->lastmatch = YAZ_NFA_NOMATCH;
365 m.bestnode = n->nstates;
369 sz = sizeof( struct yaz_nfa_backref_info) * n->nbackrefs;
370 if (!n->curr_backrefs) {
371 n->curr_backrefs = nmem_malloc( n->nmem, sz);
372 n->best_backrefs = nmem_malloc( n->nmem, sz);
374 for (i = 0; i<n->nbackrefs; i++) {
375 n->curr_backrefs[i].start = 0;
376 n->curr_backrefs[i].end = 0;
377 n->best_backrefs[i].start = 0;
378 n->best_backrefs[i].end = 0;
381 match_state(n->firststate, *inbuff, *incharsleft, &m);
383 *incharsleft -= (m.longest-*inbuff);
387 n->lastmatch = m.errorcode;
389 n->lastmatch= YAZ_NFA_SUCCESS;
392 n->lastmatch = YAZ_NFA_NOMATCH;
397 int yaz_nfa_get_backref( yaz_nfa *n,
399 yaz_nfa_char **start,
400 yaz_nfa_char **end) {
401 if (backref_no>=n->nbackrefs)
405 if (n->lastmatch== YAZ_NFA_NOMATCH)
406 return 1; /* accept other errors, they return partial matches*/
408 *start = n->best_backrefs[backref_no].start;
409 *end = n->best_backrefs[backref_no].end;
413 /* * * * * * * * * * * * * *
415 * * * * * * * * * * * * * */
417 static yaz_nfa_converter *create_null_converter ( yaz_nfa *n) {
418 yaz_nfa_converter *c;
419 c=nmem_malloc(n->nmem, sizeof(yaz_nfa_converter));
429 yaz_nfa_converter *yaz_nfa_create_string_converter (
431 yaz_nfa_char *string,
433 yaz_nfa_converter *c;
435 c=create_null_converter(n);
437 c->string=nmem_malloc(n->nmem, length*sizeof(yaz_nfa_char));
438 for (i=0;i<length;i++)
439 c->string[i]=string[i];
444 yaz_nfa_converter *yaz_nfa_create_backref_converter (
445 yaz_nfa *n, int backref_no ) {
446 yaz_nfa_converter *c;
447 c=create_null_converter(n);
448 c->type=conv_backref;
449 c->backref_no=backref_no;
454 void yaz_nfa_append_converter (
456 yaz_nfa_converter *startpoint,
457 yaz_nfa_converter *newconverter) {
458 while (startpoint->next)
459 startpoint=startpoint->next;
460 startpoint->next=newconverter;
463 static int string_convert (
465 yaz_nfa_converter *c,
466 yaz_nfa_char **outbuff,
467 size_t *outcharsleft){
469 yaz_nfa_char *p=c->string;
471 if ((*outcharsleft)-- <= 0)
478 static int backref_convert (
480 yaz_nfa_converter *c,
481 yaz_nfa_char **outbuff,
482 size_t *outcharsleft){
483 yaz_nfa_char *cp1,*cp2;
485 i=yaz_nfa_get_backref(n,c->backref_no, &cp1, &cp2);
486 if (i==2) /* no backref, produce no output, that's ok */
488 if (i==1) /* no match in dfa */
489 return 1; /* should not happen */
491 if ((*outcharsleft)-- <= 0)
500 int yaz_nfa_run_converters(
502 yaz_nfa_converter *c,
503 yaz_nfa_char **outbuff,
504 size_t *outcharsleft){
506 // yaz_nfa_char *bufstart=*outbuff;
510 rc=string_convert(n,c,outbuff,outcharsleft);
513 rc=backref_convert(n,c,outbuff,outcharsleft);
516 rc=3; /* internal error */
527 yaz_nfa_state *yaz_nfa_get_first(yaz_nfa *n){
530 return n->firststate;
533 yaz_nfa_state *yaz_nfa_get_next(yaz_nfa *n, yaz_nfa_state *s){
543 static void dump_trans(FILE *F, yaz_nfa_transition *t ) {
550 if ( (t->range_start <= ' ') || (t->range_start>'z'))
552 if ( (t->range_end <= ' ') || (t->range_end>'z'))
554 if ((t->range_start==EMPTY_START) && (t->range_end==EMPTY_END)) {
557 fprintf(F, " -> [%d] %s '%c' %x - '%c' %x \n",
563 static void dump_state(FILE *F, yaz_nfa_state *s,
564 char *(*strfunc)(void *) ) {
565 yaz_nfa_transition *t;
566 char *resultstring = "";
569 resultstring = (*strfunc)(s->result);
572 resultstring = s->result;
574 fprintf(F, " state [%d] %s %s",
575 s->num, s->result?"(FINAL)":"", resultstring );
576 if (s->backref_start) {
577 fprintf(F, " start-%d", s->backref_start);
579 if (s->backref_end) {
580 fprintf(F, " end-%d", s->backref_end);
585 fprintf(F, " (no transitions)\n");
590 } while (t != s->lasttrans);
595 void yaz_nfa_dump(FILE *F, yaz_nfa *n,
596 char *(*strfunc)(void *) ) {
598 if (!F) /* lazy programmers can just pass 0 for F */
600 fprintf(F, "The NFA has %d states and %d backrefs\n",
601 n->nstates, n->nbackrefs);
606 dump_state(F, s, strfunc);
607 } while (s != n->laststate);
615 * indent-tabs-mode: nil
617 * vim: shiftwidth=4 tabstop=8 expandtab