2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1995-09-04 12:33:26 adam
8 * Various cleanup. YAZ util used instead.
10 * Revision 1.2 1995/01/25 11:30:50 adam
11 * Simple error reporting when parsing regular expressions.
12 * Memory usage reduced.
14 * Revision 1.1 1995/01/24 16:02:52 adam
15 * New private header file in dfa module (dfap.h).
16 * Module no longer uses yacc to parse regular expressions.
38 struct Tnode *p[2]; /* CAT,OR,STAR,PLUS (left,right) */
39 short ch[2]; /* if ch[0] >= 0 then this Tnode represents */
40 /* a character in range [ch[0]..ch[1]] */
41 /* otherwise ch[0] represents */
42 /* accepting no (-acceptno) */
44 unsigned pos : 15; /* CAT/OR/STAR/EPSILON or non-neg. position */
45 unsigned nullable : 1;
46 Set firstpos; /* first positions */
47 Set lastpos; /* last positions */
50 struct Tblock { /* Tnode bucket (block) */
51 struct Tblock *next; /* pointer to next bucket */
52 struct Tnode *tarray; /* array of nodes in bucket */
56 #define STATE_HASH 199
57 #define POSET_CHUNK 100
59 int debug_dfa_trav = 0;
60 int debug_dfa_tran = 0;
61 int debug_dfa_followpos = 0;
65 static struct DFA_parse *parse_info = NULL;
68 static int inside_string;
69 static const unsigned char *expr_ptr;
70 static unsigned short *ctrl_chars;
71 static struct Tnode **posar;
74 static Set *followpos;
76 static struct Tnode *mk_Tnode (void);
77 static struct Tnode *mk_Tnode_cset (BSet charset);
78 static void term_Tnode (void);
84 mk_dfa_tran (struct DFA_states *dfas),
85 add_follow (Set lastpos, Set firstpos),
86 dfa_trav (struct Tnode *n),
87 init_followpos (void),
88 mk_dfa_tran (struct DFA_states *dfas),
89 pr_tran (struct DFA_states *dfas),
90 pr_verbose (struct DFA_states *dfas),
100 *str_char (unsigned c);
116 static int lookahead;
117 static unsigned look_ch;
118 static BSet look_chars;
120 static struct Tnode *expr_1 (void),
125 static struct Tnode *expr_1 (void)
127 struct Tnode *t1, *t2, *tn;
129 if (!(t1 = expr_2 ()))
131 while (lookahead == L_ALT)
134 if (!(t2 = expr_2 ()))
146 static struct Tnode *expr_2 (void)
148 struct Tnode *t1, *t2, *tn;
150 if (!(t1 = expr_3 ()))
152 while (lookahead == L_WILD ||
153 lookahead == L_ANYZ ||
154 lookahead == L_ANY ||
156 lookahead == L_CHAR ||
157 lookahead == L_CHARS)
159 if (!(t2 = expr_3 ()))
171 static struct Tnode *expr_3 (void)
173 struct Tnode *t1, *tn;
175 if (!(t1 = expr_4 ()))
177 if (lookahead == L_CLOS0)
185 else if (lookahead == L_CLOS1)
193 else if (lookahead == L_QUEST)
199 tn->u.p[1] = mk_Tnode();
200 tn->u.p[1]->pos = EPSILON;
206 static struct Tnode *expr_4 (void)
214 if (!(t1 = expr_1 ()))
216 if (lookahead == L_RP)
223 t1->pos = ++(parse_info->position);
224 t1->u.ch[1] = t1->u.ch[0] = look_ch;
228 t1 = mk_Tnode_cset (look_chars);
232 t1 = mk_Tnode_cset (parse_info->anyset);
238 t1->u.p[0] = mk_Tnode_cset (parse_info->anyset);
239 t1->u.p[1] = mk_Tnode();
240 t1->u.p[1]->pos = EPSILON;
246 t1->u.p[0] = mk_Tnode_cset (parse_info->anyset);
254 static void do_parse (dfap, s, cc, tnp)
255 struct DFA_parse *dfap;
257 const unsigned short *cc;
261 struct Tnode *t1, *t2;
263 for (i=0; cc[i]; i +=2)
265 ctrl_chars = imalloc ((i+2) * sizeof(*ctrl_chars));
266 for (i=0; (ctrl_chars[i] = cc[i]); i ++)
270 ctrl_chars[i] = L_LP;
273 ctrl_chars[i] = L_RP;
276 ctrl_chars[i] = L_ANY;
279 ctrl_chars[i] = L_ALT;
282 ctrl_chars[i] = L_QUEST;
285 ctrl_chars[i] = L_CLOS1;
288 ctrl_chars[i] = L_CLOS0;
291 ctrl_chars[i] = L_END;
294 ctrl_chars[i] = L_START;
297 ctrl_chars[i] = L_ANY;
300 ctrl_chars[i] = L_ANYZ;
303 ctrl_chars[i] = L_WILD;
310 expr_ptr = (unsigned char *) *s;
315 if (t1 && lookahead == 0)
318 t2->pos = ++parse_info->position;
319 t2->u.ch[0] = -(++parse_info->rule);
330 if (lookahead == L_RP)
331 err_code = DFA_ERR_RP;
332 else if (lookahead == L_LP)
333 err_code = DFA_ERR_LP;
335 err_code = DFA_ERR_SYNTAX;
338 *s = (char *) expr_ptr;
342 static int nextchar (int *esc)
345 if (*expr_ptr == '\0' || isspace(*expr_ptr))
347 else if (*expr_ptr != '\\')
374 static int nextchar_set (int *esc)
376 if (*expr_ptr == ' ')
381 return nextchar (esc);
384 static int read_charset (void)
386 int i, ch0, ch1, esc0, esc1, cc = 0;
387 look_chars = mk_BSet (&parse_info->charset);
388 res_BSet (parse_info->charset, look_chars);
390 ch0 = nextchar_set (&esc0);
391 if (!esc0 && ch0 == '^')
394 ch0 = nextchar_set (&esc0);
398 if (!esc0 && ch0 == ']')
400 add_BSet (parse_info->charset, look_chars, ch0);
401 ch1 = nextchar_set (&esc1);
402 if (!esc1 && ch1 == '-')
404 if ((ch1 = nextchar_set (&esc1)) == 0)
406 if (!esc1 && ch1 == ']')
408 add_BSet (parse_info->charset, look_chars, '-');
411 for (i=ch0; ++i<=ch1;)
412 add_BSet (parse_info->charset, look_chars, i);
413 ch0 = nextchar_set (&esc0);
422 com_BSet (parse_info->charset, look_chars);
426 unsigned short dfa_thompson_chars[] =
440 unsigned short dfa_ccl_chars[] =
448 static int lex_sub(void)
451 const unsigned short *cc;
452 while ((look_ch = nextchar (&esc)) != 0)
457 inside_string = !inside_string;
459 else if (esc || inside_string)
461 else if (look_ch == '[')
462 return read_charset();
463 else if (look_ch == ' ')
467 for (cc = ctrl_chars; *cc; cc += 2)
475 static void lex (void)
477 lookahead = lex_sub ();
480 static const char *str_char (unsigned c)
497 sprintf (s+1, "x%02x", c);
519 static void out_char (int c)
521 printf ("%s", str_char (c));
524 static void term_Tnode (void)
526 struct Tblock *t, *t1;
527 for (t = parse_info->start; (t1 = t);)
535 static struct Tnode *mk_Tnode (void)
539 if (parse_info->use_Tnode == parse_info->max_Tnode)
541 tnew = (struct Tblock *) imalloc (sizeof(struct Tblock));
542 tnew->tarray = (struct Tnode *) imalloc (TADD*sizeof(struct Tnode));
545 if (parse_info->use_Tnode == 0)
546 parse_info->start = tnew;
548 parse_info->end->next = tnew;
549 parse_info->end = tnew;
551 parse_info->max_Tnode += TADD;
553 return parse_info->end->tarray+(parse_info->use_Tnode++ % TADD);
556 struct Tnode *mk_Tnode_cset (BSet charset)
558 struct Tnode *tn1, *tn0 = mk_Tnode();
559 int ch1, ch0 = trav_BSet (parse_info->charset, charset, 0);
565 tn0->pos = ++parse_info->position;
566 ch1 = travi_BSet (parse_info->charset, charset, ch0+1) ;
568 tn0->u.ch[1] = parse_info->charset->size;
571 tn0->u.ch[1] = ch1-1;
572 while ((ch0=trav_BSet (parse_info->charset, charset, ch1)) != -1)
578 tn1 = tn0->u.p[1] = mk_Tnode();
580 tn1->pos = ++(parse_info->position);
581 if ((ch1 = travi_BSet (parse_info->charset, charset,
584 tn1->u.ch[1] = parse_info->charset->size;
587 tn1->u.ch[1] = ch1-1;
594 static void del_followpos (void)
599 static void init_pos (void)
601 posar = (struct Tnode **) imalloc (sizeof(struct Tnode*)
602 * (1+parse_info->position));
605 static void del_pos (void)
610 static void add_follow (Set lastpos, Set firstpos)
614 followpos[lastpos->value] =
615 union_Set (poset, followpos[lastpos->value], firstpos);
616 lastpos = lastpos->next;
620 static void dfa_trav (struct Tnode *n)
625 dfa_trav (n->u.p[0]);
626 dfa_trav (n->u.p[1]);
627 n->nullable = n->u.p[0]->nullable & n->u.p[1]->nullable;
628 n->firstpos = mk_Set (poset);
629 n->firstpos = union_Set (poset, n->firstpos, n->u.p[0]->firstpos);
630 if (n->u.p[0]->nullable)
631 n->firstpos = union_Set (poset, n->firstpos, n->u.p[1]->firstpos);
632 n->lastpos = mk_Set (poset);
633 n->lastpos = union_Set (poset, n->lastpos, n->u.p[1]->lastpos);
634 if (n->u.p[1]->nullable)
635 n->lastpos = union_Set (poset, n->lastpos, n->u.p[0]->lastpos);
637 add_follow (n->u.p[0]->lastpos, n->u.p[1]->firstpos);
639 n->u.p[0]->firstpos = rm_Set (poset, n->u.p[0]->firstpos);
640 n->u.p[0]->lastpos = rm_Set (poset, n->u.p[0]->lastpos);
641 n->u.p[1]->firstpos = rm_Set (poset, n->u.p[1]->firstpos);
642 n->u.p[1]->lastpos = rm_Set (poset, n->u.p[1]->lastpos);
647 dfa_trav (n->u.p[0]);
648 dfa_trav (n->u.p[1]);
649 n->nullable = n->u.p[0]->nullable | n->u.p[1]->nullable;
651 n->firstpos = merge_Set (poset, n->u.p[0]->firstpos,
652 n->u.p[1]->firstpos);
653 n->lastpos = merge_Set (poset, n->u.p[0]->lastpos,
655 n->u.p[0]->firstpos = rm_Set (poset, n->u.p[0]->firstpos);
656 n->u.p[0]->lastpos = rm_Set (poset, n->u.p[0]->lastpos);
657 n->u.p[1]->firstpos = rm_Set (poset, n->u.p[1]->firstpos);
658 n->u.p[1]->lastpos = rm_Set (poset, n->u.p[1]->lastpos);
663 dfa_trav (n->u.p[0]);
664 n->nullable = n->u.p[0]->nullable;
665 n->firstpos = n->u.p[0]->firstpos;
666 n->lastpos = n->u.p[0]->lastpos;
667 add_follow (n->lastpos, n->firstpos);
672 dfa_trav (n->u.p[0]);
674 n->firstpos = n->u.p[0]->firstpos;
675 n->lastpos = n->u.p[0]->lastpos;
676 add_follow (n->lastpos, n->firstpos);
682 n->lastpos = mk_Set (poset);
683 n->firstpos = mk_Set (poset);
690 n->firstpos = mk_Set (poset);
691 n->firstpos = add_Set (poset, n->firstpos, n->pos);
692 n->lastpos = mk_Set (poset);
693 n->lastpos = add_Set (poset, n->lastpos, n->pos);
696 printf ("#%d", -n->u.ch[0]);
697 else if (n->u.ch[1] > n->u.ch[0])
700 out_char (n->u.ch[0]);
701 if (n->u.ch[1] > n->u.ch[0]+1)
703 out_char (n->u.ch[1]);
707 out_char (n->u.ch[0]);
711 printf ("\n nullable : %c\n", n->nullable ? '1' : '0');
712 printf (" firstpos :");
713 pr_Set (poset, n->firstpos);
714 printf (" lastpos :");
715 pr_Set (poset, n->lastpos);
719 static void init_followpos (void)
723 followpos = fa = (Set *) imalloc (sizeof(Set) * (1+parse_info->position));
724 for (i = parse_info->position+1; --i >= 0; fa++)
725 *fa = mk_Set (poset);
728 static void mk_dfa_tran (struct DFA_states *dfas)
735 struct DFA_state *dfa_from, *dfa_to;
738 max_char = parse_info->charset->size;
739 pos = (short *) imalloc (sizeof(*pos) * (parse_info->position+1));
741 tran_set = cp_Set (poset, parse_info->root->firstpos);
742 i = add_DFA_state (dfas, &tran_set, &dfa_from);
744 dfa_from->rule_no = 0;
745 while ((dfa_from = get_DFA_state (dfas)))
749 for (tran_set = dfa_from->set; tran_set; tran_set = tran_set->next)
750 if ((c = posar[tran_set->value]->u.ch[0]) >= 0 && c <= max_char)
751 *pos_i++ = tran_set->value;
752 else if (c < 0 && (i == 0 || c > i))
755 dfa_from->rule_no = -i;
757 for (char_1 = 0; char_1 <= max_char; char_1++)
760 for (pos_i = pos; (i = *pos_i) != -1; ++pos_i)
761 if (posar[i]->u.ch[1] >= char_1)
762 if ((c=posar[i]->u.ch[0]) < char_0)
769 if (char_0 > max_char)
771 tran_set = mk_Set (poset);
772 for (pos_i = pos; (i = *pos_i) != -1; ++pos_i)
773 if ((c=posar[i]->u.ch[1]) >= char_0)
774 if (posar[i]->u.ch[0] <= char_0)
778 tran_set = union_Set (poset, tran_set, followpos[i]);
780 else if (c <= char_1)
784 add_DFA_state (dfas, &tran_set, &dfa_to);
785 add_DFA_tran (dfas, dfa_from, char_0, char_1, dfa_to->no);
790 sort_DFA_states (dfas);
793 static void pr_tran (struct DFA_states *dfas)
796 struct DFA_tran *tran;
797 int prev_no, i, c, no;
799 for (no=0; no < dfas->no; ++no)
801 s = dfas->sortarray[no];
802 assert (s->no == no);
803 printf ("DFA state %d", no);
805 printf ("#%d", s->rule_no);
807 pr_Set (poset, s->set);
809 for (i=s->tran_no, tran=s->trans; --i >= 0; tran++)
811 if (prev_no != tran->to)
815 printf (" goto %d on [", tran->to);
818 for (c = tran->ch[0]; c <= tran->ch[1]; c++)
819 printf ("%s", str_char(c));
827 static void pr_verbose (struct DFA_states *dfas)
831 printf ("%d/%d tree nodes used, %d bytes each\n",
832 parse_info->use_Tnode, parse_info->max_Tnode, sizeof (struct Tnode));
833 k = inf_BSetHandle (parse_info->charset, &i, &j);
834 printf ("%ld/%ld character sets, %d bytes each\n",
835 i/k, j/k, k*sizeof(BSetWord));
836 k = inf_SetType (poset, &i, &j);
837 printf ("%ld/%ld poset items, %d bytes each\n", i, j, k);
838 printf ("%d DFA states\n", dfas->no);
841 static void pr_followpos (void)
844 printf ("\nfollowsets:\n");
845 for (i=1; i <= parse_info->position; i++)
848 pr_Set (poset, followpos[i]);
851 if (posar[i]->u.ch[0] < 0)
852 printf ("#%d", -posar[i]->u.ch[0]);
853 else if (posar[i]->u.ch[1] > posar[i]->u.ch[0])
856 out_char (posar[i]->u.ch[0]);
857 if (posar[i]->u.ch[1] > posar[i]->u.ch[0]+1)
859 out_char (posar[i]->u.ch[1]);
863 out_char (posar[i]->u.ch[0]);
869 static struct DFA_parse *dfa_parse_init (void)
871 parse_info = (struct DFA_parse *) imalloc (sizeof (struct DFA_parse));
873 parse_info->charset = mk_BSetHandle (255, 20);
874 parse_info->position = 0;
875 parse_info->rule = 0;
876 parse_info->root = NULL;
878 parse_info->anyset = mk_BSet (&parse_info->charset);
879 res_BSet (parse_info->charset, parse_info->anyset);
880 add_BSet (parse_info->charset, parse_info->anyset, '\n');
881 com_BSet (parse_info->charset, parse_info->anyset);
882 parse_info->use_Tnode = parse_info->max_Tnode = 0;
886 static void rm_dfa_parse (struct DFA_parse **dfap)
891 rm_BSetHandle (&parse_info->charset);
896 static struct DFA_states *mk_dfas (struct DFA_parse *dfap, int poset_chunk)
898 struct DFA_states *dfas;
900 assert (poset_chunk > 10);
904 poset = mk_SetType (poset_chunk);
907 dfa_trav (parse_info->root);
909 if (debug_dfa_followpos)
911 init_DFA_states (&dfas, poset, STATE_HASH);
923 struct DFA *dfa_init (void)
927 dfa = imalloc (sizeof(*dfa));
928 dfa->parse_info = dfa_parse_init ();
929 dfa->state_info = NULL;
934 int dfa_parse (struct DFA *dfa, char **pattern)
939 do_parse (dfa->parse_info, pattern, dfa_thompson_chars, &top);
942 if ( !dfa->parse_info->root )
943 dfa->parse_info->root = top;
950 n->u.p[0] = dfa->parse_info->root;
952 dfa->parse_info->root = n;
957 void dfa_mkstate (struct DFA *dfa)
961 dfa->state_info = mk_dfas (dfa->parse_info, POSET_CHUNK);
962 dfa->no_states = dfa->state_info->no;
963 dfa->states = dfa->state_info->sortarray;
964 rm_dfa_parse (&dfa->parse_info);
967 void dfa_delete (struct DFA **dfap)
971 if ((*dfap)->parse_info)
972 rm_dfa_parse (&(*dfap)->parse_info);
973 rm_DFA_states (&(*dfap)->state_info);