2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1996-10-29 13:57:22 adam
8 * Include of zebrautl.h instead of alexutil.h.
10 * Revision 1.13 1996/06/17 14:24:08 adam
11 * Bug fix: read_charset didn't handle character mapping.
13 * Revision 1.12 1996/06/04 10:20:02 adam
14 * Added support for character mapping.
16 * Revision 1.11 1996/01/08 19:15:24 adam
17 * Allow single $ in expressions.
19 * Revision 1.10 1996/01/08 09:09:17 adam
20 * Function dfa_parse got 'const' string argument.
21 * New functions to define char mappings made public.
23 * Revision 1.9 1995/12/06 12:24:58 adam
24 * Removed verbatim mode code.
26 * Revision 1.8 1995/12/06 09:09:58 adam
27 * Work on left and right anchors.
29 * Revision 1.7 1995/11/27 09:23:02 adam
30 * New berbatim hook in regular expressions. "[]n ..".
32 * Revision 1.6 1995/10/16 09:31:25 adam
35 * Revision 1.5 1995/10/02 15:17:58 adam
36 * Bug fix in dfa_delete.
38 * Revision 1.4 1995/09/28 09:18:52 adam
39 * Removed various preprocessor defines.
41 * Revision 1.3 1995/09/04 12:33:26 adam
42 * Various cleanup. YAZ util used instead.
44 * Revision 1.2 1995/01/25 11:30:50 adam
45 * Simple error reporting when parsing regular expressions.
46 * Memory usage reduced.
48 * Revision 1.1 1995/01/24 16:02:52 adam
49 * New private header file in dfa module (dfap.h).
50 * Module no longer uses yacc to parse regular expressions.
72 struct Tnode *p[2]; /* CAT,OR,STAR,PLUS (left,right) */
73 short ch[2]; /* if ch[0] >= 0 then this Tnode represents */
74 /* a character in range [ch[0]..ch[1]] */
75 /* otherwise ch[0] represents */
76 /* accepting no (-acceptno) */
78 unsigned pos : 15; /* CAT/OR/STAR/EPSILON or non-neg. position */
79 unsigned nullable : 1;
80 Set firstpos; /* first positions */
81 Set lastpos; /* last positions */
84 struct Tblock { /* Tnode bucket (block) */
85 struct Tblock *next; /* pointer to next bucket */
86 struct Tnode *tarray; /* array of nodes in bucket */
90 #define STATE_HASH 199
91 #define POSET_CHUNK 100
93 int debug_dfa_trav = 0;
94 int debug_dfa_tran = 0;
95 int debug_dfa_followpos = 0;
98 static struct DFA_parse *parse_info = NULL;
101 static int inside_string;
102 static const unsigned char *expr_ptr;
103 static struct Tnode **posar;
105 static SetType poset;
106 static Set *followpos;
108 static struct Tnode *mk_Tnode (void);
109 static struct Tnode *mk_Tnode_cset (BSet charset);
110 static void term_Tnode (void);
113 del_followpos (void),
116 mk_dfa_tran (struct DFA_states *dfas),
117 add_follow (Set lastpos, Set firstpos),
118 dfa_trav (struct Tnode *n),
119 init_followpos (void),
120 pr_tran (struct DFA_states *dfas),
121 pr_verbose (struct DFA_states *dfas),
131 *str_char (unsigned c);
147 static int lookahead;
148 static unsigned look_ch;
149 static BSet look_chars;
151 static struct Tnode *expr_1 (void),
156 static struct Tnode *expr_1 (void)
158 struct Tnode *t1, *t2, *tn;
160 if (!(t1 = expr_2 ()))
162 while (lookahead == L_ALT)
165 if (!(t2 = expr_2 ()))
177 static struct Tnode *expr_2 (void)
179 struct Tnode *t1, *t2, *tn;
181 if (!(t1 = expr_3 ()))
183 while (lookahead == L_WILD ||
184 lookahead == L_ANYZ ||
185 lookahead == L_ANY ||
187 lookahead == L_CHAR ||
188 lookahead == L_CHARS)
190 if (!(t2 = expr_3 ()))
202 static struct Tnode *expr_3 (void)
204 struct Tnode *t1, *tn;
206 if (!(t1 = expr_4 ()))
208 if (lookahead == L_CLOS0)
216 else if (lookahead == L_CLOS1)
224 else if (lookahead == L_QUEST)
230 tn->u.p[1] = mk_Tnode();
231 tn->u.p[1]->pos = EPSILON;
237 static struct Tnode *expr_4 (void)
245 if (!(t1 = expr_1 ()))
247 if (lookahead == L_RP)
254 t1->pos = ++parse_info->position;
255 t1->u.ch[1] = t1->u.ch[0] = look_ch;
259 t1 = mk_Tnode_cset (look_chars);
263 t1 = mk_Tnode_cset (parse_info->anyset);
269 t1->u.p[0] = mk_Tnode_cset (parse_info->anyset);
270 t1->u.p[1] = mk_Tnode();
271 t1->u.p[1]->pos = EPSILON;
277 t1->u.p[0] = mk_Tnode_cset (parse_info->anyset);
285 static void do_parse (struct DFA_parse *dfap, const char **s,
288 int start_anchor_flag = 0;
289 struct Tnode *t1, *t2, *tn;
293 expr_ptr = (const unsigned char *) *s;
297 if (lookahead == L_START)
299 start_anchor_flag = 1;
302 if (lookahead == L_END)
305 t1->pos = ++parse_info->position;
306 t1->u.ch[1] = t1->u.ch[0] = '\n';
312 if (t1 && lookahead == L_END)
315 t2->pos = ++parse_info->position;
316 t2->u.ch[1] = t2->u.ch[0] = '\n';
327 if (t1 && lookahead == 0)
330 t2->pos = ++parse_info->position;
331 t2->u.ch[0] = -(++parse_info->rule);
332 t2->u.ch[1] = start_anchor_flag ? 0 : -(parse_info->rule);
343 if (lookahead == L_RP)
344 err_code = DFA_ERR_RP;
345 else if (lookahead == L_LP)
346 err_code = DFA_ERR_LP;
348 err_code = DFA_ERR_SYNTAX;
351 *s = (const char *) expr_ptr;
354 static int nextchar (int *esc)
357 if (*expr_ptr == '\0')
359 else if (*expr_ptr != '\\')
388 static int nextchar_set (int *esc)
390 if (*expr_ptr == ' ')
395 return nextchar (esc);
398 static int read_charset (void)
400 int i, ch0, ch1, esc0, esc1, cc = 0;
401 look_chars = mk_BSet (&parse_info->charset);
402 res_BSet (parse_info->charset, look_chars);
404 ch0 = nextchar_set (&esc0);
405 if (!esc0 && ch0 == '^')
408 ch0 = nextchar_set (&esc0);
412 if (!esc0 && ch0 == ']')
414 if (parse_info->cmap)
416 char **mapto, mapfrom[2];
417 const char *mcp = mapfrom;
419 mapto = (*parse_info->cmap)(&mcp, 1);
423 add_BSet (parse_info->charset, look_chars, ch0);
424 ch1 = nextchar_set (&esc1);
425 if (!esc1 && ch1 == '-')
427 if ((ch1 = nextchar_set (&esc1)) == 0)
429 if (!esc1 && ch1 == ']')
431 add_BSet (parse_info->charset, look_chars, '-');
434 if (parse_info->cmap)
436 char **mapto, mapfrom[2];
437 const char *mcp = mapfrom;
439 mapto = (*parse_info->cmap) (&mcp, 1);
443 for (i=ch0; ++i<=ch1;)
444 add_BSet (parse_info->charset, look_chars, i);
445 ch0 = nextchar_set (&esc0);
454 com_BSet (parse_info->charset, look_chars);
458 static int map_l_char (void)
461 const char *cp0 = (const char *) (expr_ptr-1);
462 int i = 0, len = strlen(cp0);
464 if (cp0[0] == 1 && cp0[1])
470 if (!parse_info->cmap)
473 mapto = (*parse_info->cmap) (&cp0, len);
476 expr_ptr = (const unsigned char *) cp0;
477 look_ch = mapto[i][0];
478 logf (LOG_DEBUG, "map from %c to %d", expr_ptr[-1], look_ch);
482 static int lex_sub(void)
485 while ((look_ch = nextchar (&esc)) != 0)
489 return map_l_char ();
490 inside_string = !inside_string;
492 else if (esc || inside_string)
493 return map_l_char ();
494 else if (look_ch == '[')
495 return read_charset();
499 for (cc = parse_info->charMap; *cc; cc += 2)
506 return map_l_char ();
511 static void lex (void)
513 lookahead = lex_sub ();
516 static const char *str_char (unsigned c)
533 sprintf (s+1, "x%02x", c);
555 static void out_char (int c)
557 printf ("%s", str_char (c));
560 static void term_Tnode (void)
562 struct Tblock *t, *t1;
563 for (t = parse_info->start; (t1 = t);)
571 static struct Tnode *mk_Tnode (void)
575 if (parse_info->use_Tnode == parse_info->max_Tnode)
577 tnew = (struct Tblock *) imalloc (sizeof(struct Tblock));
578 tnew->tarray = (struct Tnode *) imalloc (TADD*sizeof(struct Tnode));
581 if (parse_info->use_Tnode == 0)
582 parse_info->start = tnew;
584 parse_info->end->next = tnew;
585 parse_info->end = tnew;
587 parse_info->max_Tnode += TADD;
589 return parse_info->end->tarray+(parse_info->use_Tnode++ % TADD);
592 struct Tnode *mk_Tnode_cset (BSet charset)
594 struct Tnode *tn1, *tn0 = mk_Tnode();
595 int ch1, ch0 = trav_BSet (parse_info->charset, charset, 0);
601 tn0->pos = ++parse_info->position;
602 ch1 = travi_BSet (parse_info->charset, charset, ch0+1) ;
604 tn0->u.ch[1] = parse_info->charset->size;
607 tn0->u.ch[1] = ch1-1;
608 while ((ch0=trav_BSet (parse_info->charset, charset, ch1)) != -1)
614 tn1 = tn0->u.p[1] = mk_Tnode();
616 tn1->pos = ++(parse_info->position);
617 if ((ch1 = travi_BSet (parse_info->charset, charset,
620 tn1->u.ch[1] = parse_info->charset->size;
623 tn1->u.ch[1] = ch1-1;
630 static void del_followpos (void)
635 static void init_pos (void)
637 posar = (struct Tnode **) imalloc (sizeof(struct Tnode*)
638 * (1+parse_info->position));
641 static void del_pos (void)
646 static void add_follow (Set lastpos, Set firstpos)
650 followpos[lastpos->value] =
651 union_Set (poset, followpos[lastpos->value], firstpos);
652 lastpos = lastpos->next;
656 static void dfa_trav (struct Tnode *n)
661 dfa_trav (n->u.p[0]);
662 dfa_trav (n->u.p[1]);
663 n->nullable = n->u.p[0]->nullable & n->u.p[1]->nullable;
664 n->firstpos = mk_Set (poset);
665 n->firstpos = union_Set (poset, n->firstpos, n->u.p[0]->firstpos);
666 if (n->u.p[0]->nullable)
667 n->firstpos = union_Set (poset, n->firstpos, n->u.p[1]->firstpos);
668 n->lastpos = mk_Set (poset);
669 n->lastpos = union_Set (poset, n->lastpos, n->u.p[1]->lastpos);
670 if (n->u.p[1]->nullable)
671 n->lastpos = union_Set (poset, n->lastpos, n->u.p[0]->lastpos);
673 add_follow (n->u.p[0]->lastpos, n->u.p[1]->firstpos);
675 n->u.p[0]->firstpos = rm_Set (poset, n->u.p[0]->firstpos);
676 n->u.p[0]->lastpos = rm_Set (poset, n->u.p[0]->lastpos);
677 n->u.p[1]->firstpos = rm_Set (poset, n->u.p[1]->firstpos);
678 n->u.p[1]->lastpos = rm_Set (poset, n->u.p[1]->lastpos);
683 dfa_trav (n->u.p[0]);
684 dfa_trav (n->u.p[1]);
685 n->nullable = n->u.p[0]->nullable | n->u.p[1]->nullable;
687 n->firstpos = merge_Set (poset, n->u.p[0]->firstpos,
688 n->u.p[1]->firstpos);
689 n->lastpos = merge_Set (poset, n->u.p[0]->lastpos,
691 n->u.p[0]->firstpos = rm_Set (poset, n->u.p[0]->firstpos);
692 n->u.p[0]->lastpos = rm_Set (poset, n->u.p[0]->lastpos);
693 n->u.p[1]->firstpos = rm_Set (poset, n->u.p[1]->firstpos);
694 n->u.p[1]->lastpos = rm_Set (poset, n->u.p[1]->lastpos);
699 dfa_trav (n->u.p[0]);
700 n->nullable = n->u.p[0]->nullable;
701 n->firstpos = n->u.p[0]->firstpos;
702 n->lastpos = n->u.p[0]->lastpos;
703 add_follow (n->lastpos, n->firstpos);
708 dfa_trav (n->u.p[0]);
710 n->firstpos = n->u.p[0]->firstpos;
711 n->lastpos = n->u.p[0]->lastpos;
712 add_follow (n->lastpos, n->firstpos);
718 n->lastpos = mk_Set (poset);
719 n->firstpos = mk_Set (poset);
726 n->firstpos = mk_Set (poset);
727 n->firstpos = add_Set (poset, n->firstpos, n->pos);
728 n->lastpos = mk_Set (poset);
729 n->lastpos = add_Set (poset, n->lastpos, n->pos);
732 printf ("#%d (n#%d)", -n->u.ch[0], -n->u.ch[1]);
733 else if (n->u.ch[1] > n->u.ch[0])
736 out_char (n->u.ch[0]);
737 if (n->u.ch[1] > n->u.ch[0]+1)
739 out_char (n->u.ch[1]);
743 out_char (n->u.ch[0]);
747 printf ("\n nullable : %c\n", n->nullable ? '1' : '0');
748 printf (" firstpos :");
749 pr_Set (poset, n->firstpos);
750 printf (" lastpos :");
751 pr_Set (poset, n->lastpos);
755 static void init_followpos (void)
759 followpos = fa = (Set *) imalloc (sizeof(Set) * (1+parse_info->position));
760 for (i = parse_info->position+1; --i >= 0; fa++)
761 *fa = mk_Set (poset);
764 static void mk_dfa_tran (struct DFA_states *dfas)
771 struct DFA_state *dfa_from, *dfa_to;
774 max_char = parse_info->charset->size;
775 pos = (short *) imalloc (sizeof(*pos) * (parse_info->position+1));
777 tran_set = cp_Set (poset, parse_info->root->firstpos);
778 i = add_DFA_state (dfas, &tran_set, &dfa_from);
780 dfa_from->rule_no = 0;
781 while ((dfa_from = get_DFA_state (dfas)))
785 for (tran_set = dfa_from->set; tran_set; tran_set = tran_set->next)
786 if ((c = posar[tran_set->value]->u.ch[0]) >= 0 && c <= max_char)
787 *pos_i++ = tran_set->value;
792 c = posar[tran_set->value]->u.ch[1];
797 dfa_from->rule_no = -i;
798 dfa_from->rule_nno = -j;
800 for (char_1 = 0; char_1 <= max_char; char_1++)
803 for (pos_i = pos; (i = *pos_i) != -1; ++pos_i)
804 if (posar[i]->u.ch[1] >= char_1
805 && (c=posar[i]->u.ch[0]) < char_0)
811 if (char_0 > max_char)
816 tran_set = mk_Set (poset);
817 for (pos_i = pos; (i = *pos_i) != -1; ++pos_i)
819 if ((c=posar[i]->u.ch[0]) > char_0 && c <= char_1)
820 char_1 = c - 1; /* forward chunk */
821 else if ((c=posar[i]->u.ch[1]) >= char_0 && c < char_1)
822 char_1 = c; /* backward chunk */
823 if (posar[i]->u.ch[1] >= char_0 && posar[i]->u.ch[0] <= char_0)
824 tran_set = union_Set (poset, tran_set, followpos[i]);
828 add_DFA_state (dfas, &tran_set, &dfa_to);
829 add_DFA_tran (dfas, dfa_from, char_0, char_1, dfa_to->no);
834 sort_DFA_states (dfas);
837 static void pr_tran (struct DFA_states *dfas)
840 struct DFA_tran *tran;
841 int prev_no, i, c, no;
843 for (no=0; no < dfas->no; ++no)
845 s = dfas->sortarray[no];
846 assert (s->no == no);
847 printf ("DFA state %d", no);
849 printf ("#%d", s->rule_no);
851 printf (" #%d", s->rule_nno);
854 pr_Set (poset, s->set);
856 for (i=s->tran_no, tran=s->trans; --i >= 0; tran++)
858 if (prev_no != tran->to)
862 printf (" goto %d on [", tran->to);
865 for (c = tran->ch[0]; c <= tran->ch[1]; c++)
866 printf ("%s", str_char(c));
874 static void pr_verbose (struct DFA_states *dfas)
878 printf ("%d/%d tree nodes used, %d bytes each\n",
879 parse_info->use_Tnode, parse_info->max_Tnode, sizeof (struct Tnode));
880 k = inf_BSetHandle (parse_info->charset, &i, &j);
881 printf ("%ld/%ld character sets, %d bytes each\n",
882 i/k, j/k, k*sizeof(BSetWord));
883 k = inf_SetType (poset, &i, &j);
884 printf ("%ld/%ld poset items, %d bytes each\n", i, j, k);
885 printf ("%d DFA states\n", dfas->no);
888 static void pr_followpos (void)
891 printf ("\nfollowsets:\n");
892 for (i=1; i <= parse_info->position; i++)
895 pr_Set (poset, followpos[i]);
898 if (posar[i]->u.ch[0] < 0)
899 printf ("#%d", -posar[i]->u.ch[0]);
900 else if (posar[i]->u.ch[1] > posar[i]->u.ch[0])
903 out_char (posar[i]->u.ch[0]);
904 if (posar[i]->u.ch[1] > posar[i]->u.ch[0]+1)
906 out_char (posar[i]->u.ch[1]);
910 out_char (posar[i]->u.ch[0]);
916 void dfa_parse_cmap_clean (struct DFA *d)
918 struct DFA_parse *dfa = d->parse_info;
923 dfa->charMapSize = 7;
924 dfa->charMap = imalloc (dfa->charMapSize * sizeof(*dfa->charMap));
929 void dfa_parse_cmap_new (struct DFA *d, const int *cmap)
931 struct DFA_parse *dfa = d->parse_info;
936 for (cp = cmap; *cp; cp += 2)
938 size = cp - cmap + 1;
939 if (size > dfa->charMapSize)
942 ifree (dfa->charMap);
943 dfa->charMapSize = size;
944 dfa->charMap = imalloc (size * sizeof(*dfa->charMap));
946 memcpy (dfa->charMap, cmap, size * sizeof(*dfa->charMap));
949 void dfa_parse_cmap_del (struct DFA *d, int from)
951 struct DFA_parse *dfa = d->parse_info;
955 for (cc = dfa->charMap; *cc; cc += 2)
958 while ((cc[0] = cc[2]))
967 void dfa_parse_cmap_add (struct DFA *d, int from, int to)
969 struct DFA_parse *dfa = d->parse_info;
974 for (cc = dfa->charMap; *cc; cc += 2)
980 indx = cc - dfa->charMap;
981 size = dfa->charMapSize;
984 int *cn = imalloc ((size+16) * sizeof(*dfa->charMap));
985 memcpy (cn, dfa->charMap, indx*sizeof(*dfa->charMap));
986 ifree (dfa->charMap);
988 dfa->charMapSize = size+16;
990 dfa->charMap[indx] = from;
991 dfa->charMap[indx+1] = to;
992 dfa->charMap[indx+2] = 0;
995 void dfa_parse_cmap_thompson (struct DFA *d)
997 static int thompson_chars[] =
1013 dfa_parse_cmap_new (d, thompson_chars);
1016 static struct DFA_parse *dfa_parse_init (void)
1018 parse_info = (struct DFA_parse *) imalloc (sizeof (struct DFA_parse));
1020 parse_info->charset = mk_BSetHandle (255, 20);
1021 parse_info->position = 0;
1022 parse_info->rule = 0;
1023 parse_info->root = NULL;
1025 parse_info->anyset = mk_BSet (&parse_info->charset);
1026 res_BSet (parse_info->charset, parse_info->anyset);
1027 add_BSet (parse_info->charset, parse_info->anyset, '\n');
1028 com_BSet (parse_info->charset, parse_info->anyset);
1029 parse_info->use_Tnode = parse_info->max_Tnode = 0;
1030 parse_info->charMap = NULL;
1031 parse_info->charMapSize = 0;
1032 parse_info->cmap = NULL;
1036 static void rm_dfa_parse (struct DFA_parse **dfap)
1039 assert (parse_info);
1041 rm_BSetHandle (&parse_info->charset);
1042 ifree (parse_info->charMap);
1047 static struct DFA_states *mk_dfas (struct DFA_parse *dfap, int poset_chunk)
1049 struct DFA_states *dfas;
1051 assert (poset_chunk > 10);
1055 poset = mk_SetType (poset_chunk);
1058 assert (parse_info->root);
1059 dfa_trav (parse_info->root);
1061 if (debug_dfa_followpos)
1063 init_DFA_states (&dfas, poset, STATE_HASH);
1075 struct DFA *dfa_init (void)
1079 dfa = imalloc (sizeof(*dfa));
1080 dfa->parse_info = dfa_parse_init ();
1081 dfa->state_info = NULL;
1083 dfa_parse_cmap_thompson (dfa);
1087 void dfa_set_cmap (struct DFA *dfa, char **(*cmap)(const char **from, int len))
1089 dfa->parse_info->cmap = cmap;
1092 int dfa_parse (struct DFA *dfa, const char **pattern)
1097 assert (dfa->parse_info);
1098 do_parse (dfa->parse_info, pattern, &top);
1101 if ( !dfa->parse_info->root )
1102 dfa->parse_info->root = top;
1109 n->u.p[0] = dfa->parse_info->root;
1111 dfa->parse_info->root = n;
1116 void dfa_mkstate (struct DFA *dfa)
1120 dfa->state_info = mk_dfas (dfa->parse_info, POSET_CHUNK);
1121 dfa->no_states = dfa->state_info->no;
1122 dfa->states = dfa->state_info->sortarray;
1123 rm_dfa_parse (&dfa->parse_info);
1126 void dfa_delete (struct DFA **dfap)
1130 if ((*dfap)->parse_info)
1131 rm_dfa_parse (&(*dfap)->parse_info);
1132 if ((*dfap)->state_info)
1133 rm_DFA_states (&(*dfap)->state_info);