2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1995-11-27 09:23:02 adam
8 * New berbatim hook in regular expressions. "[]n ..".
10 * Revision 1.6 1995/10/16 09:31:25 adam
13 * Revision 1.5 1995/10/02 15:17:58 adam
14 * Bug fix in dfa_delete.
16 * Revision 1.4 1995/09/28 09:18:52 adam
17 * Removed various preprocessor defines.
19 * Revision 1.3 1995/09/04 12:33:26 adam
20 * Various cleanup. YAZ util used instead.
22 * Revision 1.2 1995/01/25 11:30:50 adam
23 * Simple error reporting when parsing regular expressions.
24 * Memory usage reduced.
26 * Revision 1.1 1995/01/24 16:02:52 adam
27 * New private header file in dfa module (dfap.h).
28 * Module no longer uses yacc to parse regular expressions.
50 struct Tnode *p[2]; /* CAT,OR,STAR,PLUS (left,right) */
51 short ch[2]; /* if ch[0] >= 0 then this Tnode represents */
52 /* a character in range [ch[0]..ch[1]] */
53 /* otherwise ch[0] represents */
54 /* accepting no (-acceptno) */
56 unsigned pos : 15; /* CAT/OR/STAR/EPSILON or non-neg. position */
57 unsigned nullable : 1;
58 Set firstpos; /* first positions */
59 Set lastpos; /* last positions */
62 struct Tblock { /* Tnode bucket (block) */
63 struct Tblock *next; /* pointer to next bucket */
64 struct Tnode *tarray; /* array of nodes in bucket */
68 #define STATE_HASH 199
69 #define POSET_CHUNK 100
71 int debug_dfa_trav = 0;
72 int debug_dfa_tran = 0;
73 int debug_dfa_followpos = 0;
76 static struct DFA_parse *parse_info = NULL;
79 static int inside_string;
80 static const unsigned char *expr_ptr;
81 static int expr_verbatim;
82 static unsigned short *ctrl_chars;
83 static struct Tnode **posar;
86 static Set *followpos;
88 static struct Tnode *mk_Tnode (void);
89 static struct Tnode *mk_Tnode_cset (BSet charset);
90 static void term_Tnode (void);
96 mk_dfa_tran (struct DFA_states *dfas),
97 add_follow (Set lastpos, Set firstpos),
98 dfa_trav (struct Tnode *n),
99 init_followpos (void),
100 mk_dfa_tran (struct DFA_states *dfas),
101 pr_tran (struct DFA_states *dfas),
102 pr_verbose (struct DFA_states *dfas),
112 *str_char (unsigned c);
128 static int lookahead;
129 static unsigned look_ch;
130 static BSet look_chars;
132 static struct Tnode *expr_1 (void),
137 static struct Tnode *expr_1 (void)
139 struct Tnode *t1, *t2, *tn;
141 if (!(t1 = expr_2 ()))
143 while (lookahead == L_ALT)
146 if (!(t2 = expr_2 ()))
158 static struct Tnode *expr_2 (void)
160 struct Tnode *t1, *t2, *tn;
162 if (!(t1 = expr_3 ()))
164 while (lookahead == L_WILD ||
165 lookahead == L_ANYZ ||
166 lookahead == L_ANY ||
168 lookahead == L_CHAR ||
169 lookahead == L_CHARS)
171 if (!(t2 = expr_3 ()))
183 static struct Tnode *expr_3 (void)
185 struct Tnode *t1, *tn;
187 if (!(t1 = expr_4 ()))
189 if (lookahead == L_CLOS0)
197 else if (lookahead == L_CLOS1)
205 else if (lookahead == L_QUEST)
211 tn->u.p[1] = mk_Tnode();
212 tn->u.p[1]->pos = EPSILON;
218 static struct Tnode *expr_4 (void)
226 if (!(t1 = expr_1 ()))
228 if (lookahead == L_RP)
235 t1->pos = ++(parse_info->position);
236 t1->u.ch[1] = t1->u.ch[0] = look_ch;
240 t1 = mk_Tnode_cset (look_chars);
244 t1 = mk_Tnode_cset (parse_info->anyset);
250 t1->u.p[0] = mk_Tnode_cset (parse_info->anyset);
251 t1->u.p[1] = mk_Tnode();
252 t1->u.p[1]->pos = EPSILON;
258 t1->u.p[0] = mk_Tnode_cset (parse_info->anyset);
266 static void do_parse (dfap, s, cc, tnp)
267 struct DFA_parse *dfap;
269 const unsigned short *cc;
273 struct Tnode *t1, *t2;
275 for (i=0; cc[i]; i +=2)
277 ctrl_chars = imalloc ((i+2) * sizeof(*ctrl_chars));
278 for (i=0; (ctrl_chars[i] = cc[i]); i ++)
282 ctrl_chars[i] = L_LP;
285 ctrl_chars[i] = L_RP;
288 ctrl_chars[i] = L_ANY;
291 ctrl_chars[i] = L_ALT;
294 ctrl_chars[i] = L_QUEST;
297 ctrl_chars[i] = L_CLOS1;
300 ctrl_chars[i] = L_CLOS0;
303 ctrl_chars[i] = L_END;
306 ctrl_chars[i] = L_START;
309 ctrl_chars[i] = L_ANY;
312 ctrl_chars[i] = L_ANYZ;
315 ctrl_chars[i] = L_WILD;
322 expr_ptr = (unsigned char *) *s;
328 if (t1 && lookahead == 0)
331 t2->pos = ++parse_info->position;
332 t2->u.ch[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 = (char *) expr_ptr;
355 static int nextchar (int *esc)
358 if (*expr_ptr == '\0' || isspace(*expr_ptr))
360 else if (*expr_ptr != '\\' || expr_verbatim)
362 if (*expr_ptr == '[' && expr_ptr[1] == ']' && !expr_verbatim)
366 while (expr_ptr[i] >= '0' && expr_ptr[i] <= '9')
367 val = val*10 + expr_ptr[i++]-'0';
370 if (expr_ptr[i] == ' ')
378 assert (expr_verbatim > 0);
409 static int nextchar_set (int *esc)
411 if (*expr_ptr == ' ')
416 return nextchar (esc);
419 static int read_charset (void)
421 int i, ch0, ch1, esc0, esc1, cc = 0;
422 look_chars = mk_BSet (&parse_info->charset);
423 res_BSet (parse_info->charset, look_chars);
425 ch0 = nextchar_set (&esc0);
426 if (!esc0 && ch0 == '^')
429 ch0 = nextchar_set (&esc0);
433 if (!esc0 && ch0 == ']')
435 add_BSet (parse_info->charset, look_chars, ch0);
436 ch1 = nextchar_set (&esc1);
437 if (!esc1 && ch1 == '-')
439 if ((ch1 = nextchar_set (&esc1)) == 0)
441 if (!esc1 && ch1 == ']')
443 add_BSet (parse_info->charset, look_chars, '-');
446 for (i=ch0; ++i<=ch1;)
447 add_BSet (parse_info->charset, look_chars, i);
448 ch0 = nextchar_set (&esc0);
457 com_BSet (parse_info->charset, look_chars);
461 unsigned short dfa_thompson_chars[] =
475 unsigned short dfa_ccl_chars[] =
483 static int lex_sub(void)
486 const unsigned short *cc;
487 while ((look_ch = nextchar (&esc)) != 0)
492 inside_string = !inside_string;
494 else if (esc || inside_string)
496 else if (look_ch == '[')
497 return read_charset();
498 else if (look_ch == ' ')
502 for (cc = ctrl_chars; *cc; cc += 2)
510 static void lex (void)
512 lookahead = lex_sub ();
515 static const char *str_char (unsigned c)
532 sprintf (s+1, "x%02x", c);
554 static void out_char (int c)
556 printf ("%s", str_char (c));
559 static void term_Tnode (void)
561 struct Tblock *t, *t1;
562 for (t = parse_info->start; (t1 = t);)
570 static struct Tnode *mk_Tnode (void)
574 if (parse_info->use_Tnode == parse_info->max_Tnode)
576 tnew = (struct Tblock *) imalloc (sizeof(struct Tblock));
577 tnew->tarray = (struct Tnode *) imalloc (TADD*sizeof(struct Tnode));
580 if (parse_info->use_Tnode == 0)
581 parse_info->start = tnew;
583 parse_info->end->next = tnew;
584 parse_info->end = tnew;
586 parse_info->max_Tnode += TADD;
588 return parse_info->end->tarray+(parse_info->use_Tnode++ % TADD);
591 struct Tnode *mk_Tnode_cset (BSet charset)
593 struct Tnode *tn1, *tn0 = mk_Tnode();
594 int ch1, ch0 = trav_BSet (parse_info->charset, charset, 0);
600 tn0->pos = ++parse_info->position;
601 ch1 = travi_BSet (parse_info->charset, charset, ch0+1) ;
603 tn0->u.ch[1] = parse_info->charset->size;
606 tn0->u.ch[1] = ch1-1;
607 while ((ch0=trav_BSet (parse_info->charset, charset, ch1)) != -1)
613 tn1 = tn0->u.p[1] = mk_Tnode();
615 tn1->pos = ++(parse_info->position);
616 if ((ch1 = travi_BSet (parse_info->charset, charset,
619 tn1->u.ch[1] = parse_info->charset->size;
622 tn1->u.ch[1] = ch1-1;
629 static void del_followpos (void)
634 static void init_pos (void)
636 posar = (struct Tnode **) imalloc (sizeof(struct Tnode*)
637 * (1+parse_info->position));
640 static void del_pos (void)
645 static void add_follow (Set lastpos, Set firstpos)
649 followpos[lastpos->value] =
650 union_Set (poset, followpos[lastpos->value], firstpos);
651 lastpos = lastpos->next;
655 static void dfa_trav (struct Tnode *n)
660 dfa_trav (n->u.p[0]);
661 dfa_trav (n->u.p[1]);
662 n->nullable = n->u.p[0]->nullable & n->u.p[1]->nullable;
663 n->firstpos = mk_Set (poset);
664 n->firstpos = union_Set (poset, n->firstpos, n->u.p[0]->firstpos);
665 if (n->u.p[0]->nullable)
666 n->firstpos = union_Set (poset, n->firstpos, n->u.p[1]->firstpos);
667 n->lastpos = mk_Set (poset);
668 n->lastpos = union_Set (poset, n->lastpos, n->u.p[1]->lastpos);
669 if (n->u.p[1]->nullable)
670 n->lastpos = union_Set (poset, n->lastpos, n->u.p[0]->lastpos);
672 add_follow (n->u.p[0]->lastpos, n->u.p[1]->firstpos);
674 n->u.p[0]->firstpos = rm_Set (poset, n->u.p[0]->firstpos);
675 n->u.p[0]->lastpos = rm_Set (poset, n->u.p[0]->lastpos);
676 n->u.p[1]->firstpos = rm_Set (poset, n->u.p[1]->firstpos);
677 n->u.p[1]->lastpos = rm_Set (poset, n->u.p[1]->lastpos);
682 dfa_trav (n->u.p[0]);
683 dfa_trav (n->u.p[1]);
684 n->nullable = n->u.p[0]->nullable | n->u.p[1]->nullable;
686 n->firstpos = merge_Set (poset, n->u.p[0]->firstpos,
687 n->u.p[1]->firstpos);
688 n->lastpos = merge_Set (poset, n->u.p[0]->lastpos,
690 n->u.p[0]->firstpos = rm_Set (poset, n->u.p[0]->firstpos);
691 n->u.p[0]->lastpos = rm_Set (poset, n->u.p[0]->lastpos);
692 n->u.p[1]->firstpos = rm_Set (poset, n->u.p[1]->firstpos);
693 n->u.p[1]->lastpos = rm_Set (poset, n->u.p[1]->lastpos);
698 dfa_trav (n->u.p[0]);
699 n->nullable = n->u.p[0]->nullable;
700 n->firstpos = n->u.p[0]->firstpos;
701 n->lastpos = n->u.p[0]->lastpos;
702 add_follow (n->lastpos, n->firstpos);
707 dfa_trav (n->u.p[0]);
709 n->firstpos = n->u.p[0]->firstpos;
710 n->lastpos = n->u.p[0]->lastpos;
711 add_follow (n->lastpos, n->firstpos);
717 n->lastpos = mk_Set (poset);
718 n->firstpos = mk_Set (poset);
725 n->firstpos = mk_Set (poset);
726 n->firstpos = add_Set (poset, n->firstpos, n->pos);
727 n->lastpos = mk_Set (poset);
728 n->lastpos = add_Set (poset, n->lastpos, n->pos);
731 printf ("#%d", -n->u.ch[0]);
732 else if (n->u.ch[1] > n->u.ch[0])
735 out_char (n->u.ch[0]);
736 if (n->u.ch[1] > n->u.ch[0]+1)
738 out_char (n->u.ch[1]);
742 out_char (n->u.ch[0]);
746 printf ("\n nullable : %c\n", n->nullable ? '1' : '0');
747 printf (" firstpos :");
748 pr_Set (poset, n->firstpos);
749 printf (" lastpos :");
750 pr_Set (poset, n->lastpos);
754 static void init_followpos (void)
758 followpos = fa = (Set *) imalloc (sizeof(Set) * (1+parse_info->position));
759 for (i = parse_info->position+1; --i >= 0; fa++)
760 *fa = mk_Set (poset);
763 static void mk_dfa_tran (struct DFA_states *dfas)
770 struct DFA_state *dfa_from, *dfa_to;
773 max_char = parse_info->charset->size;
774 pos = (short *) imalloc (sizeof(*pos) * (parse_info->position+1));
776 tran_set = cp_Set (poset, parse_info->root->firstpos);
777 i = add_DFA_state (dfas, &tran_set, &dfa_from);
779 dfa_from->rule_no = 0;
780 while ((dfa_from = get_DFA_state (dfas)))
784 for (tran_set = dfa_from->set; tran_set; tran_set = tran_set->next)
785 if ((c = posar[tran_set->value]->u.ch[0]) >= 0 && c <= max_char)
786 *pos_i++ = tran_set->value;
787 else if (c < 0 && (i == 0 || c > i))
790 dfa_from->rule_no = -i;
792 for (char_1 = 0; char_1 <= max_char; char_1++)
795 for (pos_i = pos; (i = *pos_i) != -1; ++pos_i)
796 if (posar[i]->u.ch[1] >= char_1
797 && (c=posar[i]->u.ch[0]) < char_0)
803 if (char_0 > max_char)
808 tran_set = mk_Set (poset);
809 for (pos_i = pos; (i = *pos_i) != -1; ++pos_i)
811 if ((c=posar[i]->u.ch[0]) > char_0 && c <= char_1)
812 char_1 = c - 1; /* forward chunk */
813 else if ((c=posar[i]->u.ch[1]) >= char_0 && c < char_1)
814 char_1 = c; /* backward chunk */
815 if (posar[i]->u.ch[1] >= char_0 && posar[i]->u.ch[0] <= char_0)
816 tran_set = union_Set (poset, tran_set, followpos[i]);
820 add_DFA_state (dfas, &tran_set, &dfa_to);
821 add_DFA_tran (dfas, dfa_from, char_0, char_1, dfa_to->no);
826 sort_DFA_states (dfas);
829 static void pr_tran (struct DFA_states *dfas)
832 struct DFA_tran *tran;
833 int prev_no, i, c, no;
835 for (no=0; no < dfas->no; ++no)
837 s = dfas->sortarray[no];
838 assert (s->no == no);
839 printf ("DFA state %d", no);
841 printf ("#%d", s->rule_no);
843 pr_Set (poset, s->set);
845 for (i=s->tran_no, tran=s->trans; --i >= 0; tran++)
847 if (prev_no != tran->to)
851 printf (" goto %d on [", tran->to);
854 for (c = tran->ch[0]; c <= tran->ch[1]; c++)
855 printf ("%s", str_char(c));
863 static void pr_verbose (struct DFA_states *dfas)
867 printf ("%d/%d tree nodes used, %d bytes each\n",
868 parse_info->use_Tnode, parse_info->max_Tnode, sizeof (struct Tnode));
869 k = inf_BSetHandle (parse_info->charset, &i, &j);
870 printf ("%ld/%ld character sets, %d bytes each\n",
871 i/k, j/k, k*sizeof(BSetWord));
872 k = inf_SetType (poset, &i, &j);
873 printf ("%ld/%ld poset items, %d bytes each\n", i, j, k);
874 printf ("%d DFA states\n", dfas->no);
877 static void pr_followpos (void)
880 printf ("\nfollowsets:\n");
881 for (i=1; i <= parse_info->position; i++)
884 pr_Set (poset, followpos[i]);
887 if (posar[i]->u.ch[0] < 0)
888 printf ("#%d", -posar[i]->u.ch[0]);
889 else if (posar[i]->u.ch[1] > posar[i]->u.ch[0])
892 out_char (posar[i]->u.ch[0]);
893 if (posar[i]->u.ch[1] > posar[i]->u.ch[0]+1)
895 out_char (posar[i]->u.ch[1]);
899 out_char (posar[i]->u.ch[0]);
905 static struct DFA_parse *dfa_parse_init (void)
907 parse_info = (struct DFA_parse *) imalloc (sizeof (struct DFA_parse));
909 parse_info->charset = mk_BSetHandle (255, 20);
910 parse_info->position = 0;
911 parse_info->rule = 0;
912 parse_info->root = NULL;
914 parse_info->anyset = mk_BSet (&parse_info->charset);
915 res_BSet (parse_info->charset, parse_info->anyset);
916 add_BSet (parse_info->charset, parse_info->anyset, '\n');
917 com_BSet (parse_info->charset, parse_info->anyset);
918 parse_info->use_Tnode = parse_info->max_Tnode = 0;
922 static void rm_dfa_parse (struct DFA_parse **dfap)
927 rm_BSetHandle (&parse_info->charset);
932 static struct DFA_states *mk_dfas (struct DFA_parse *dfap, int poset_chunk)
934 struct DFA_states *dfas;
936 assert (poset_chunk > 10);
940 poset = mk_SetType (poset_chunk);
943 dfa_trav (parse_info->root);
945 if (debug_dfa_followpos)
947 init_DFA_states (&dfas, poset, STATE_HASH);
959 struct DFA *dfa_init (void)
963 dfa = imalloc (sizeof(*dfa));
964 dfa->parse_info = dfa_parse_init ();
965 dfa->state_info = NULL;
970 int dfa_parse (struct DFA *dfa, char **pattern)
975 do_parse (dfa->parse_info, pattern, dfa_thompson_chars, &top);
978 if ( !dfa->parse_info->root )
979 dfa->parse_info->root = top;
986 n->u.p[0] = dfa->parse_info->root;
988 dfa->parse_info->root = n;
993 void dfa_mkstate (struct DFA *dfa)
997 dfa->state_info = mk_dfas (dfa->parse_info, POSET_CHUNK);
998 dfa->no_states = dfa->state_info->no;
999 dfa->states = dfa->state_info->sortarray;
1000 rm_dfa_parse (&dfa->parse_info);
1003 void dfa_delete (struct DFA **dfap)
1007 if ((*dfap)->parse_info)
1008 rm_dfa_parse (&(*dfap)->parse_info);
1009 if ((*dfap)->state_info)
1010 rm_DFA_states (&(*dfap)->state_info);