2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1996-06-17 14:24:08 adam
8 * Bug fix: read_charset didn't handle character mapping.
10 * Revision 1.12 1996/06/04 10:20:02 adam
11 * Added support for character mapping.
13 * Revision 1.11 1996/01/08 19:15:24 adam
14 * Allow single $ in expressions.
16 * Revision 1.10 1996/01/08 09:09:17 adam
17 * Function dfa_parse got 'const' string argument.
18 * New functions to define char mappings made public.
20 * Revision 1.9 1995/12/06 12:24:58 adam
21 * Removed verbatim mode code.
23 * Revision 1.8 1995/12/06 09:09:58 adam
24 * Work on left and right anchors.
26 * Revision 1.7 1995/11/27 09:23:02 adam
27 * New berbatim hook in regular expressions. "[]n ..".
29 * Revision 1.6 1995/10/16 09:31:25 adam
32 * Revision 1.5 1995/10/02 15:17:58 adam
33 * Bug fix in dfa_delete.
35 * Revision 1.4 1995/09/28 09:18:52 adam
36 * Removed various preprocessor defines.
38 * Revision 1.3 1995/09/04 12:33:26 adam
39 * Various cleanup. YAZ util used instead.
41 * Revision 1.2 1995/01/25 11:30:50 adam
42 * Simple error reporting when parsing regular expressions.
43 * Memory usage reduced.
45 * Revision 1.1 1995/01/24 16:02:52 adam
46 * New private header file in dfa module (dfap.h).
47 * Module no longer uses yacc to parse regular expressions.
69 struct Tnode *p[2]; /* CAT,OR,STAR,PLUS (left,right) */
70 short ch[2]; /* if ch[0] >= 0 then this Tnode represents */
71 /* a character in range [ch[0]..ch[1]] */
72 /* otherwise ch[0] represents */
73 /* accepting no (-acceptno) */
75 unsigned pos : 15; /* CAT/OR/STAR/EPSILON or non-neg. position */
76 unsigned nullable : 1;
77 Set firstpos; /* first positions */
78 Set lastpos; /* last positions */
81 struct Tblock { /* Tnode bucket (block) */
82 struct Tblock *next; /* pointer to next bucket */
83 struct Tnode *tarray; /* array of nodes in bucket */
87 #define STATE_HASH 199
88 #define POSET_CHUNK 100
90 int debug_dfa_trav = 0;
91 int debug_dfa_tran = 0;
92 int debug_dfa_followpos = 0;
95 static struct DFA_parse *parse_info = NULL;
98 static int inside_string;
99 static const unsigned char *expr_ptr;
100 static struct Tnode **posar;
102 static SetType poset;
103 static Set *followpos;
105 static struct Tnode *mk_Tnode (void);
106 static struct Tnode *mk_Tnode_cset (BSet charset);
107 static void term_Tnode (void);
110 del_followpos (void),
113 mk_dfa_tran (struct DFA_states *dfas),
114 add_follow (Set lastpos, Set firstpos),
115 dfa_trav (struct Tnode *n),
116 init_followpos (void),
117 pr_tran (struct DFA_states *dfas),
118 pr_verbose (struct DFA_states *dfas),
128 *str_char (unsigned c);
144 static int lookahead;
145 static unsigned look_ch;
146 static BSet look_chars;
148 static struct Tnode *expr_1 (void),
153 static struct Tnode *expr_1 (void)
155 struct Tnode *t1, *t2, *tn;
157 if (!(t1 = expr_2 ()))
159 while (lookahead == L_ALT)
162 if (!(t2 = expr_2 ()))
174 static struct Tnode *expr_2 (void)
176 struct Tnode *t1, *t2, *tn;
178 if (!(t1 = expr_3 ()))
180 while (lookahead == L_WILD ||
181 lookahead == L_ANYZ ||
182 lookahead == L_ANY ||
184 lookahead == L_CHAR ||
185 lookahead == L_CHARS)
187 if (!(t2 = expr_3 ()))
199 static struct Tnode *expr_3 (void)
201 struct Tnode *t1, *tn;
203 if (!(t1 = expr_4 ()))
205 if (lookahead == L_CLOS0)
213 else if (lookahead == L_CLOS1)
221 else if (lookahead == L_QUEST)
227 tn->u.p[1] = mk_Tnode();
228 tn->u.p[1]->pos = EPSILON;
234 static struct Tnode *expr_4 (void)
242 if (!(t1 = expr_1 ()))
244 if (lookahead == L_RP)
251 t1->pos = ++parse_info->position;
252 t1->u.ch[1] = t1->u.ch[0] = look_ch;
256 t1 = mk_Tnode_cset (look_chars);
260 t1 = mk_Tnode_cset (parse_info->anyset);
266 t1->u.p[0] = mk_Tnode_cset (parse_info->anyset);
267 t1->u.p[1] = mk_Tnode();
268 t1->u.p[1]->pos = EPSILON;
274 t1->u.p[0] = mk_Tnode_cset (parse_info->anyset);
282 static void do_parse (struct DFA_parse *dfap, const char **s,
285 int start_anchor_flag = 0;
286 struct Tnode *t1, *t2, *tn;
290 expr_ptr = (const unsigned char *) *s;
294 if (lookahead == L_START)
296 start_anchor_flag = 1;
299 if (lookahead == L_END)
302 t1->pos = ++parse_info->position;
303 t1->u.ch[1] = t1->u.ch[0] = '\n';
309 if (t1 && lookahead == L_END)
312 t2->pos = ++parse_info->position;
313 t2->u.ch[1] = t2->u.ch[0] = '\n';
324 if (t1 && lookahead == 0)
327 t2->pos = ++parse_info->position;
328 t2->u.ch[0] = -(++parse_info->rule);
329 t2->u.ch[1] = start_anchor_flag ? 0 : -(parse_info->rule);
340 if (lookahead == L_RP)
341 err_code = DFA_ERR_RP;
342 else if (lookahead == L_LP)
343 err_code = DFA_ERR_LP;
345 err_code = DFA_ERR_SYNTAX;
348 *s = (const char *) expr_ptr;
351 static int nextchar (int *esc)
354 if (*expr_ptr == '\0')
356 else if (*expr_ptr != '\\')
385 static int nextchar_set (int *esc)
387 if (*expr_ptr == ' ')
392 return nextchar (esc);
395 static int read_charset (void)
397 int i, ch0, ch1, esc0, esc1, cc = 0;
398 look_chars = mk_BSet (&parse_info->charset);
399 res_BSet (parse_info->charset, look_chars);
401 ch0 = nextchar_set (&esc0);
402 if (!esc0 && ch0 == '^')
405 ch0 = nextchar_set (&esc0);
409 if (!esc0 && ch0 == ']')
411 if (parse_info->cmap)
413 char **mapto, mapfrom[2];
414 const char *mcp = mapfrom;
416 mapto = (*parse_info->cmap)(&mcp, 1);
420 add_BSet (parse_info->charset, look_chars, ch0);
421 ch1 = nextchar_set (&esc1);
422 if (!esc1 && ch1 == '-')
424 if ((ch1 = nextchar_set (&esc1)) == 0)
426 if (!esc1 && ch1 == ']')
428 add_BSet (parse_info->charset, look_chars, '-');
431 if (parse_info->cmap)
433 char **mapto, mapfrom[2];
434 const char *mcp = mapfrom;
436 mapto = (*parse_info->cmap) (&mcp, 1);
440 for (i=ch0; ++i<=ch1;)
441 add_BSet (parse_info->charset, look_chars, i);
442 ch0 = nextchar_set (&esc0);
451 com_BSet (parse_info->charset, look_chars);
455 static int map_l_char (void)
458 const char *cp0 = expr_ptr-1;
459 int i = 0, len = strlen(cp0);
461 if (cp0[0] == 1 && cp0[1])
467 if (!parse_info->cmap)
470 mapto = (*parse_info->cmap) (&cp0, len);
474 look_ch = mapto[i][0];
475 logf (LOG_DEBUG, "map from %c to %d", expr_ptr[-1], look_ch);
479 static int lex_sub(void)
482 while ((look_ch = nextchar (&esc)) != 0)
486 return map_l_char ();
487 inside_string = !inside_string;
489 else if (esc || inside_string)
490 return map_l_char ();
491 else if (look_ch == '[')
492 return read_charset();
496 for (cc = parse_info->charMap; *cc; cc += 2)
503 return map_l_char ();
508 static void lex (void)
510 lookahead = lex_sub ();
513 static const char *str_char (unsigned c)
530 sprintf (s+1, "x%02x", c);
552 static void out_char (int c)
554 printf ("%s", str_char (c));
557 static void term_Tnode (void)
559 struct Tblock *t, *t1;
560 for (t = parse_info->start; (t1 = t);)
568 static struct Tnode *mk_Tnode (void)
572 if (parse_info->use_Tnode == parse_info->max_Tnode)
574 tnew = (struct Tblock *) imalloc (sizeof(struct Tblock));
575 tnew->tarray = (struct Tnode *) imalloc (TADD*sizeof(struct Tnode));
578 if (parse_info->use_Tnode == 0)
579 parse_info->start = tnew;
581 parse_info->end->next = tnew;
582 parse_info->end = tnew;
584 parse_info->max_Tnode += TADD;
586 return parse_info->end->tarray+(parse_info->use_Tnode++ % TADD);
589 struct Tnode *mk_Tnode_cset (BSet charset)
591 struct Tnode *tn1, *tn0 = mk_Tnode();
592 int ch1, ch0 = trav_BSet (parse_info->charset, charset, 0);
598 tn0->pos = ++parse_info->position;
599 ch1 = travi_BSet (parse_info->charset, charset, ch0+1) ;
601 tn0->u.ch[1] = parse_info->charset->size;
604 tn0->u.ch[1] = ch1-1;
605 while ((ch0=trav_BSet (parse_info->charset, charset, ch1)) != -1)
611 tn1 = tn0->u.p[1] = mk_Tnode();
613 tn1->pos = ++(parse_info->position);
614 if ((ch1 = travi_BSet (parse_info->charset, charset,
617 tn1->u.ch[1] = parse_info->charset->size;
620 tn1->u.ch[1] = ch1-1;
627 static void del_followpos (void)
632 static void init_pos (void)
634 posar = (struct Tnode **) imalloc (sizeof(struct Tnode*)
635 * (1+parse_info->position));
638 static void del_pos (void)
643 static void add_follow (Set lastpos, Set firstpos)
647 followpos[lastpos->value] =
648 union_Set (poset, followpos[lastpos->value], firstpos);
649 lastpos = lastpos->next;
653 static void dfa_trav (struct Tnode *n)
658 dfa_trav (n->u.p[0]);
659 dfa_trav (n->u.p[1]);
660 n->nullable = n->u.p[0]->nullable & n->u.p[1]->nullable;
661 n->firstpos = mk_Set (poset);
662 n->firstpos = union_Set (poset, n->firstpos, n->u.p[0]->firstpos);
663 if (n->u.p[0]->nullable)
664 n->firstpos = union_Set (poset, n->firstpos, n->u.p[1]->firstpos);
665 n->lastpos = mk_Set (poset);
666 n->lastpos = union_Set (poset, n->lastpos, n->u.p[1]->lastpos);
667 if (n->u.p[1]->nullable)
668 n->lastpos = union_Set (poset, n->lastpos, n->u.p[0]->lastpos);
670 add_follow (n->u.p[0]->lastpos, n->u.p[1]->firstpos);
672 n->u.p[0]->firstpos = rm_Set (poset, n->u.p[0]->firstpos);
673 n->u.p[0]->lastpos = rm_Set (poset, n->u.p[0]->lastpos);
674 n->u.p[1]->firstpos = rm_Set (poset, n->u.p[1]->firstpos);
675 n->u.p[1]->lastpos = rm_Set (poset, n->u.p[1]->lastpos);
680 dfa_trav (n->u.p[0]);
681 dfa_trav (n->u.p[1]);
682 n->nullable = n->u.p[0]->nullable | n->u.p[1]->nullable;
684 n->firstpos = merge_Set (poset, n->u.p[0]->firstpos,
685 n->u.p[1]->firstpos);
686 n->lastpos = merge_Set (poset, n->u.p[0]->lastpos,
688 n->u.p[0]->firstpos = rm_Set (poset, n->u.p[0]->firstpos);
689 n->u.p[0]->lastpos = rm_Set (poset, n->u.p[0]->lastpos);
690 n->u.p[1]->firstpos = rm_Set (poset, n->u.p[1]->firstpos);
691 n->u.p[1]->lastpos = rm_Set (poset, n->u.p[1]->lastpos);
696 dfa_trav (n->u.p[0]);
697 n->nullable = n->u.p[0]->nullable;
698 n->firstpos = n->u.p[0]->firstpos;
699 n->lastpos = n->u.p[0]->lastpos;
700 add_follow (n->lastpos, n->firstpos);
705 dfa_trav (n->u.p[0]);
707 n->firstpos = n->u.p[0]->firstpos;
708 n->lastpos = n->u.p[0]->lastpos;
709 add_follow (n->lastpos, n->firstpos);
715 n->lastpos = mk_Set (poset);
716 n->firstpos = mk_Set (poset);
723 n->firstpos = mk_Set (poset);
724 n->firstpos = add_Set (poset, n->firstpos, n->pos);
725 n->lastpos = mk_Set (poset);
726 n->lastpos = add_Set (poset, n->lastpos, n->pos);
729 printf ("#%d (n#%d)", -n->u.ch[0], -n->u.ch[1]);
730 else if (n->u.ch[1] > n->u.ch[0])
733 out_char (n->u.ch[0]);
734 if (n->u.ch[1] > n->u.ch[0]+1)
736 out_char (n->u.ch[1]);
740 out_char (n->u.ch[0]);
744 printf ("\n nullable : %c\n", n->nullable ? '1' : '0');
745 printf (" firstpos :");
746 pr_Set (poset, n->firstpos);
747 printf (" lastpos :");
748 pr_Set (poset, n->lastpos);
752 static void init_followpos (void)
756 followpos = fa = (Set *) imalloc (sizeof(Set) * (1+parse_info->position));
757 for (i = parse_info->position+1; --i >= 0; fa++)
758 *fa = mk_Set (poset);
761 static void mk_dfa_tran (struct DFA_states *dfas)
768 struct DFA_state *dfa_from, *dfa_to;
771 max_char = parse_info->charset->size;
772 pos = (short *) imalloc (sizeof(*pos) * (parse_info->position+1));
774 tran_set = cp_Set (poset, parse_info->root->firstpos);
775 i = add_DFA_state (dfas, &tran_set, &dfa_from);
777 dfa_from->rule_no = 0;
778 while ((dfa_from = get_DFA_state (dfas)))
782 for (tran_set = dfa_from->set; tran_set; tran_set = tran_set->next)
783 if ((c = posar[tran_set->value]->u.ch[0]) >= 0 && c <= max_char)
784 *pos_i++ = tran_set->value;
789 c = posar[tran_set->value]->u.ch[1];
794 dfa_from->rule_no = -i;
795 dfa_from->rule_nno = -j;
797 for (char_1 = 0; char_1 <= max_char; char_1++)
800 for (pos_i = pos; (i = *pos_i) != -1; ++pos_i)
801 if (posar[i]->u.ch[1] >= char_1
802 && (c=posar[i]->u.ch[0]) < char_0)
808 if (char_0 > max_char)
813 tran_set = mk_Set (poset);
814 for (pos_i = pos; (i = *pos_i) != -1; ++pos_i)
816 if ((c=posar[i]->u.ch[0]) > char_0 && c <= char_1)
817 char_1 = c - 1; /* forward chunk */
818 else if ((c=posar[i]->u.ch[1]) >= char_0 && c < char_1)
819 char_1 = c; /* backward chunk */
820 if (posar[i]->u.ch[1] >= char_0 && posar[i]->u.ch[0] <= char_0)
821 tran_set = union_Set (poset, tran_set, followpos[i]);
825 add_DFA_state (dfas, &tran_set, &dfa_to);
826 add_DFA_tran (dfas, dfa_from, char_0, char_1, dfa_to->no);
831 sort_DFA_states (dfas);
834 static void pr_tran (struct DFA_states *dfas)
837 struct DFA_tran *tran;
838 int prev_no, i, c, no;
840 for (no=0; no < dfas->no; ++no)
842 s = dfas->sortarray[no];
843 assert (s->no == no);
844 printf ("DFA state %d", no);
846 printf ("#%d", s->rule_no);
848 printf (" #%d", s->rule_nno);
851 pr_Set (poset, s->set);
853 for (i=s->tran_no, tran=s->trans; --i >= 0; tran++)
855 if (prev_no != tran->to)
859 printf (" goto %d on [", tran->to);
862 for (c = tran->ch[0]; c <= tran->ch[1]; c++)
863 printf ("%s", str_char(c));
871 static void pr_verbose (struct DFA_states *dfas)
875 printf ("%d/%d tree nodes used, %d bytes each\n",
876 parse_info->use_Tnode, parse_info->max_Tnode, sizeof (struct Tnode));
877 k = inf_BSetHandle (parse_info->charset, &i, &j);
878 printf ("%ld/%ld character sets, %d bytes each\n",
879 i/k, j/k, k*sizeof(BSetWord));
880 k = inf_SetType (poset, &i, &j);
881 printf ("%ld/%ld poset items, %d bytes each\n", i, j, k);
882 printf ("%d DFA states\n", dfas->no);
885 static void pr_followpos (void)
888 printf ("\nfollowsets:\n");
889 for (i=1; i <= parse_info->position; i++)
892 pr_Set (poset, followpos[i]);
895 if (posar[i]->u.ch[0] < 0)
896 printf ("#%d", -posar[i]->u.ch[0]);
897 else if (posar[i]->u.ch[1] > posar[i]->u.ch[0])
900 out_char (posar[i]->u.ch[0]);
901 if (posar[i]->u.ch[1] > posar[i]->u.ch[0]+1)
903 out_char (posar[i]->u.ch[1]);
907 out_char (posar[i]->u.ch[0]);
913 void dfa_parse_cmap_clean (struct DFA *d)
915 struct DFA_parse *dfa = d->parse_info;
920 dfa->charMapSize = 7;
921 dfa->charMap = imalloc (dfa->charMapSize * sizeof(*dfa->charMap));
926 void dfa_parse_cmap_new (struct DFA *d, const int *cmap)
928 struct DFA_parse *dfa = d->parse_info;
933 for (cp = cmap; *cp; cp += 2)
935 size = cp - cmap + 1;
936 if (size > dfa->charMapSize)
939 ifree (dfa->charMap);
940 dfa->charMapSize = size;
941 dfa->charMap = imalloc (size * sizeof(*dfa->charMap));
943 memcpy (dfa->charMap, cmap, size * sizeof(*dfa->charMap));
946 void dfa_parse_cmap_del (struct DFA *d, int from)
948 struct DFA_parse *dfa = d->parse_info;
952 for (cc = dfa->charMap; *cc; cc += 2)
955 while ((cc[0] = cc[2]))
964 void dfa_parse_cmap_add (struct DFA *d, int from, int to)
966 struct DFA_parse *dfa = d->parse_info;
971 for (cc = dfa->charMap; *cc; cc += 2)
977 indx = cc - dfa->charMap;
978 size = dfa->charMapSize;
981 int *cn = imalloc ((size+16) * sizeof(*dfa->charMap));
982 memcpy (cn, dfa->charMap, indx*sizeof(*dfa->charMap));
983 ifree (dfa->charMap);
985 dfa->charMapSize = size+16;
987 dfa->charMap[indx] = from;
988 dfa->charMap[indx+1] = to;
989 dfa->charMap[indx+2] = 0;
992 void dfa_parse_cmap_thompson (struct DFA *d)
994 static int thompson_chars[] =
1010 dfa_parse_cmap_new (d, thompson_chars);
1013 static struct DFA_parse *dfa_parse_init (void)
1015 parse_info = (struct DFA_parse *) imalloc (sizeof (struct DFA_parse));
1017 parse_info->charset = mk_BSetHandle (255, 20);
1018 parse_info->position = 0;
1019 parse_info->rule = 0;
1020 parse_info->root = NULL;
1022 parse_info->anyset = mk_BSet (&parse_info->charset);
1023 res_BSet (parse_info->charset, parse_info->anyset);
1024 add_BSet (parse_info->charset, parse_info->anyset, '\n');
1025 com_BSet (parse_info->charset, parse_info->anyset);
1026 parse_info->use_Tnode = parse_info->max_Tnode = 0;
1027 parse_info->charMap = NULL;
1028 parse_info->charMapSize = 0;
1029 parse_info->cmap = NULL;
1033 static void rm_dfa_parse (struct DFA_parse **dfap)
1036 assert (parse_info);
1038 rm_BSetHandle (&parse_info->charset);
1039 ifree (parse_info->charMap);
1044 static struct DFA_states *mk_dfas (struct DFA_parse *dfap, int poset_chunk)
1046 struct DFA_states *dfas;
1048 assert (poset_chunk > 10);
1052 poset = mk_SetType (poset_chunk);
1055 assert (parse_info->root);
1056 dfa_trav (parse_info->root);
1058 if (debug_dfa_followpos)
1060 init_DFA_states (&dfas, poset, STATE_HASH);
1072 struct DFA *dfa_init (void)
1076 dfa = imalloc (sizeof(*dfa));
1077 dfa->parse_info = dfa_parse_init ();
1078 dfa->state_info = NULL;
1080 dfa_parse_cmap_thompson (dfa);
1084 void dfa_set_cmap (struct DFA *dfa, char **(*cmap)(const char **from, int len))
1086 dfa->parse_info->cmap = cmap;
1089 int dfa_parse (struct DFA *dfa, const char **pattern)
1094 assert (dfa->parse_info);
1095 do_parse (dfa->parse_info, pattern, &top);
1098 if ( !dfa->parse_info->root )
1099 dfa->parse_info->root = top;
1106 n->u.p[0] = dfa->parse_info->root;
1108 dfa->parse_info->root = n;
1113 void dfa_mkstate (struct DFA *dfa)
1117 dfa->state_info = mk_dfas (dfa->parse_info, POSET_CHUNK);
1118 dfa->no_states = dfa->state_info->no;
1119 dfa->states = dfa->state_info->sortarray;
1120 rm_dfa_parse (&dfa->parse_info);
1123 void dfa_delete (struct DFA **dfap)
1127 if ((*dfap)->parse_info)
1128 rm_dfa_parse (&(*dfap)->parse_info);
1129 if ((*dfap)->state_info)
1130 rm_DFA_states (&(*dfap)->state_info);