2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1995-09-28 09:18:52 adam
8 * Removed various preprocessor defines.
10 * Revision 1.3 1995/09/04 12:33:26 adam
11 * Various cleanup. YAZ util used instead.
13 * Revision 1.2 1995/01/25 11:30:50 adam
14 * Simple error reporting when parsing regular expressions.
15 * Memory usage reduced.
17 * Revision 1.1 1995/01/24 16:02:52 adam
18 * New private header file in dfa module (dfap.h).
19 * Module no longer uses yacc to parse regular expressions.
41 struct Tnode *p[2]; /* CAT,OR,STAR,PLUS (left,right) */
42 short ch[2]; /* if ch[0] >= 0 then this Tnode represents */
43 /* a character in range [ch[0]..ch[1]] */
44 /* otherwise ch[0] represents */
45 /* accepting no (-acceptno) */
47 unsigned pos : 15; /* CAT/OR/STAR/EPSILON or non-neg. position */
48 unsigned nullable : 1;
49 Set firstpos; /* first positions */
50 Set lastpos; /* last positions */
53 struct Tblock { /* Tnode bucket (block) */
54 struct Tblock *next; /* pointer to next bucket */
55 struct Tnode *tarray; /* array of nodes in bucket */
59 #define STATE_HASH 199
60 #define POSET_CHUNK 100
62 int debug_dfa_trav = 0;
63 int debug_dfa_tran = 0;
64 int debug_dfa_followpos = 0;
67 static struct DFA_parse *parse_info = NULL;
70 static int inside_string;
71 static const unsigned char *expr_ptr;
72 static unsigned short *ctrl_chars;
73 static struct Tnode **posar;
76 static Set *followpos;
78 static struct Tnode *mk_Tnode (void);
79 static struct Tnode *mk_Tnode_cset (BSet charset);
80 static void term_Tnode (void);
86 mk_dfa_tran (struct DFA_states *dfas),
87 add_follow (Set lastpos, Set firstpos),
88 dfa_trav (struct Tnode *n),
89 init_followpos (void),
90 mk_dfa_tran (struct DFA_states *dfas),
91 pr_tran (struct DFA_states *dfas),
92 pr_verbose (struct DFA_states *dfas),
102 *str_char (unsigned c);
118 static int lookahead;
119 static unsigned look_ch;
120 static BSet look_chars;
122 static struct Tnode *expr_1 (void),
127 static struct Tnode *expr_1 (void)
129 struct Tnode *t1, *t2, *tn;
131 if (!(t1 = expr_2 ()))
133 while (lookahead == L_ALT)
136 if (!(t2 = expr_2 ()))
148 static struct Tnode *expr_2 (void)
150 struct Tnode *t1, *t2, *tn;
152 if (!(t1 = expr_3 ()))
154 while (lookahead == L_WILD ||
155 lookahead == L_ANYZ ||
156 lookahead == L_ANY ||
158 lookahead == L_CHAR ||
159 lookahead == L_CHARS)
161 if (!(t2 = expr_3 ()))
173 static struct Tnode *expr_3 (void)
175 struct Tnode *t1, *tn;
177 if (!(t1 = expr_4 ()))
179 if (lookahead == L_CLOS0)
187 else if (lookahead == L_CLOS1)
195 else if (lookahead == L_QUEST)
201 tn->u.p[1] = mk_Tnode();
202 tn->u.p[1]->pos = EPSILON;
208 static struct Tnode *expr_4 (void)
216 if (!(t1 = expr_1 ()))
218 if (lookahead == L_RP)
225 t1->pos = ++(parse_info->position);
226 t1->u.ch[1] = t1->u.ch[0] = look_ch;
230 t1 = mk_Tnode_cset (look_chars);
234 t1 = mk_Tnode_cset (parse_info->anyset);
240 t1->u.p[0] = mk_Tnode_cset (parse_info->anyset);
241 t1->u.p[1] = mk_Tnode();
242 t1->u.p[1]->pos = EPSILON;
248 t1->u.p[0] = mk_Tnode_cset (parse_info->anyset);
256 static void do_parse (dfap, s, cc, tnp)
257 struct DFA_parse *dfap;
259 const unsigned short *cc;
263 struct Tnode *t1, *t2;
265 for (i=0; cc[i]; i +=2)
267 ctrl_chars = imalloc ((i+2) * sizeof(*ctrl_chars));
268 for (i=0; (ctrl_chars[i] = cc[i]); i ++)
272 ctrl_chars[i] = L_LP;
275 ctrl_chars[i] = L_RP;
278 ctrl_chars[i] = L_ANY;
281 ctrl_chars[i] = L_ALT;
284 ctrl_chars[i] = L_QUEST;
287 ctrl_chars[i] = L_CLOS1;
290 ctrl_chars[i] = L_CLOS0;
293 ctrl_chars[i] = L_END;
296 ctrl_chars[i] = L_START;
299 ctrl_chars[i] = L_ANY;
302 ctrl_chars[i] = L_ANYZ;
305 ctrl_chars[i] = L_WILD;
312 expr_ptr = (unsigned char *) *s;
317 if (t1 && lookahead == 0)
320 t2->pos = ++parse_info->position;
321 t2->u.ch[0] = -(++parse_info->rule);
332 if (lookahead == L_RP)
333 err_code = DFA_ERR_RP;
334 else if (lookahead == L_LP)
335 err_code = DFA_ERR_LP;
337 err_code = DFA_ERR_SYNTAX;
340 *s = (char *) expr_ptr;
344 static int nextchar (int *esc)
347 if (*expr_ptr == '\0' || isspace(*expr_ptr))
349 else if (*expr_ptr != '\\')
376 static int nextchar_set (int *esc)
378 if (*expr_ptr == ' ')
383 return nextchar (esc);
386 static int read_charset (void)
388 int i, ch0, ch1, esc0, esc1, cc = 0;
389 look_chars = mk_BSet (&parse_info->charset);
390 res_BSet (parse_info->charset, look_chars);
392 ch0 = nextchar_set (&esc0);
393 if (!esc0 && ch0 == '^')
396 ch0 = nextchar_set (&esc0);
400 if (!esc0 && ch0 == ']')
402 add_BSet (parse_info->charset, look_chars, ch0);
403 ch1 = nextchar_set (&esc1);
404 if (!esc1 && ch1 == '-')
406 if ((ch1 = nextchar_set (&esc1)) == 0)
408 if (!esc1 && ch1 == ']')
410 add_BSet (parse_info->charset, look_chars, '-');
413 for (i=ch0; ++i<=ch1;)
414 add_BSet (parse_info->charset, look_chars, i);
415 ch0 = nextchar_set (&esc0);
424 com_BSet (parse_info->charset, look_chars);
428 unsigned short dfa_thompson_chars[] =
442 unsigned short dfa_ccl_chars[] =
450 static int lex_sub(void)
453 const unsigned short *cc;
454 while ((look_ch = nextchar (&esc)) != 0)
459 inside_string = !inside_string;
461 else if (esc || inside_string)
463 else if (look_ch == '[')
464 return read_charset();
465 else if (look_ch == ' ')
469 for (cc = ctrl_chars; *cc; cc += 2)
477 static void lex (void)
479 lookahead = lex_sub ();
482 static const char *str_char (unsigned c)
499 sprintf (s+1, "x%02x", c);
521 static void out_char (int c)
523 printf ("%s", str_char (c));
526 static void term_Tnode (void)
528 struct Tblock *t, *t1;
529 for (t = parse_info->start; (t1 = t);)
537 static struct Tnode *mk_Tnode (void)
541 if (parse_info->use_Tnode == parse_info->max_Tnode)
543 tnew = (struct Tblock *) imalloc (sizeof(struct Tblock));
544 tnew->tarray = (struct Tnode *) imalloc (TADD*sizeof(struct Tnode));
547 if (parse_info->use_Tnode == 0)
548 parse_info->start = tnew;
550 parse_info->end->next = tnew;
551 parse_info->end = tnew;
553 parse_info->max_Tnode += TADD;
555 return parse_info->end->tarray+(parse_info->use_Tnode++ % TADD);
558 struct Tnode *mk_Tnode_cset (BSet charset)
560 struct Tnode *tn1, *tn0 = mk_Tnode();
561 int ch1, ch0 = trav_BSet (parse_info->charset, charset, 0);
567 tn0->pos = ++parse_info->position;
568 ch1 = travi_BSet (parse_info->charset, charset, ch0+1) ;
570 tn0->u.ch[1] = parse_info->charset->size;
573 tn0->u.ch[1] = ch1-1;
574 while ((ch0=trav_BSet (parse_info->charset, charset, ch1)) != -1)
580 tn1 = tn0->u.p[1] = mk_Tnode();
582 tn1->pos = ++(parse_info->position);
583 if ((ch1 = travi_BSet (parse_info->charset, charset,
586 tn1->u.ch[1] = parse_info->charset->size;
589 tn1->u.ch[1] = ch1-1;
596 static void del_followpos (void)
601 static void init_pos (void)
603 posar = (struct Tnode **) imalloc (sizeof(struct Tnode*)
604 * (1+parse_info->position));
607 static void del_pos (void)
612 static void add_follow (Set lastpos, Set firstpos)
616 followpos[lastpos->value] =
617 union_Set (poset, followpos[lastpos->value], firstpos);
618 lastpos = lastpos->next;
622 static void dfa_trav (struct Tnode *n)
627 dfa_trav (n->u.p[0]);
628 dfa_trav (n->u.p[1]);
629 n->nullable = n->u.p[0]->nullable & n->u.p[1]->nullable;
630 n->firstpos = mk_Set (poset);
631 n->firstpos = union_Set (poset, n->firstpos, n->u.p[0]->firstpos);
632 if (n->u.p[0]->nullable)
633 n->firstpos = union_Set (poset, n->firstpos, n->u.p[1]->firstpos);
634 n->lastpos = mk_Set (poset);
635 n->lastpos = union_Set (poset, n->lastpos, n->u.p[1]->lastpos);
636 if (n->u.p[1]->nullable)
637 n->lastpos = union_Set (poset, n->lastpos, n->u.p[0]->lastpos);
639 add_follow (n->u.p[0]->lastpos, n->u.p[1]->firstpos);
641 n->u.p[0]->firstpos = rm_Set (poset, n->u.p[0]->firstpos);
642 n->u.p[0]->lastpos = rm_Set (poset, n->u.p[0]->lastpos);
643 n->u.p[1]->firstpos = rm_Set (poset, n->u.p[1]->firstpos);
644 n->u.p[1]->lastpos = rm_Set (poset, n->u.p[1]->lastpos);
649 dfa_trav (n->u.p[0]);
650 dfa_trav (n->u.p[1]);
651 n->nullable = n->u.p[0]->nullable | n->u.p[1]->nullable;
653 n->firstpos = merge_Set (poset, n->u.p[0]->firstpos,
654 n->u.p[1]->firstpos);
655 n->lastpos = merge_Set (poset, n->u.p[0]->lastpos,
657 n->u.p[0]->firstpos = rm_Set (poset, n->u.p[0]->firstpos);
658 n->u.p[0]->lastpos = rm_Set (poset, n->u.p[0]->lastpos);
659 n->u.p[1]->firstpos = rm_Set (poset, n->u.p[1]->firstpos);
660 n->u.p[1]->lastpos = rm_Set (poset, n->u.p[1]->lastpos);
665 dfa_trav (n->u.p[0]);
666 n->nullable = n->u.p[0]->nullable;
667 n->firstpos = n->u.p[0]->firstpos;
668 n->lastpos = n->u.p[0]->lastpos;
669 add_follow (n->lastpos, n->firstpos);
674 dfa_trav (n->u.p[0]);
676 n->firstpos = n->u.p[0]->firstpos;
677 n->lastpos = n->u.p[0]->lastpos;
678 add_follow (n->lastpos, n->firstpos);
684 n->lastpos = mk_Set (poset);
685 n->firstpos = mk_Set (poset);
692 n->firstpos = mk_Set (poset);
693 n->firstpos = add_Set (poset, n->firstpos, n->pos);
694 n->lastpos = mk_Set (poset);
695 n->lastpos = add_Set (poset, n->lastpos, n->pos);
698 printf ("#%d", -n->u.ch[0]);
699 else if (n->u.ch[1] > n->u.ch[0])
702 out_char (n->u.ch[0]);
703 if (n->u.ch[1] > n->u.ch[0]+1)
705 out_char (n->u.ch[1]);
709 out_char (n->u.ch[0]);
713 printf ("\n nullable : %c\n", n->nullable ? '1' : '0');
714 printf (" firstpos :");
715 pr_Set (poset, n->firstpos);
716 printf (" lastpos :");
717 pr_Set (poset, n->lastpos);
721 static void init_followpos (void)
725 followpos = fa = (Set *) imalloc (sizeof(Set) * (1+parse_info->position));
726 for (i = parse_info->position+1; --i >= 0; fa++)
727 *fa = mk_Set (poset);
730 static void mk_dfa_tran (struct DFA_states *dfas)
737 struct DFA_state *dfa_from, *dfa_to;
740 max_char = parse_info->charset->size;
741 pos = (short *) imalloc (sizeof(*pos) * (parse_info->position+1));
743 tran_set = cp_Set (poset, parse_info->root->firstpos);
744 i = add_DFA_state (dfas, &tran_set, &dfa_from);
746 dfa_from->rule_no = 0;
747 while ((dfa_from = get_DFA_state (dfas)))
751 for (tran_set = dfa_from->set; tran_set; tran_set = tran_set->next)
752 if ((c = posar[tran_set->value]->u.ch[0]) >= 0 && c <= max_char)
753 *pos_i++ = tran_set->value;
754 else if (c < 0 && (i == 0 || c > i))
757 dfa_from->rule_no = -i;
759 for (char_1 = 0; char_1 <= max_char; char_1++)
762 for (pos_i = pos; (i = *pos_i) != -1; ++pos_i)
763 if (posar[i]->u.ch[1] >= char_1)
764 if ((c=posar[i]->u.ch[0]) < char_0)
771 if (char_0 > max_char)
773 tran_set = mk_Set (poset);
774 for (pos_i = pos; (i = *pos_i) != -1; ++pos_i)
775 if ((c=posar[i]->u.ch[1]) >= char_0)
776 if (posar[i]->u.ch[0] <= char_0)
780 tran_set = union_Set (poset, tran_set, followpos[i]);
782 else if (c <= char_1)
786 add_DFA_state (dfas, &tran_set, &dfa_to);
787 add_DFA_tran (dfas, dfa_from, char_0, char_1, dfa_to->no);
792 sort_DFA_states (dfas);
795 static void pr_tran (struct DFA_states *dfas)
798 struct DFA_tran *tran;
799 int prev_no, i, c, no;
801 for (no=0; no < dfas->no; ++no)
803 s = dfas->sortarray[no];
804 assert (s->no == no);
805 printf ("DFA state %d", no);
807 printf ("#%d", s->rule_no);
809 pr_Set (poset, s->set);
811 for (i=s->tran_no, tran=s->trans; --i >= 0; tran++)
813 if (prev_no != tran->to)
817 printf (" goto %d on [", tran->to);
820 for (c = tran->ch[0]; c <= tran->ch[1]; c++)
821 printf ("%s", str_char(c));
829 static void pr_verbose (struct DFA_states *dfas)
833 printf ("%d/%d tree nodes used, %d bytes each\n",
834 parse_info->use_Tnode, parse_info->max_Tnode, sizeof (struct Tnode));
835 k = inf_BSetHandle (parse_info->charset, &i, &j);
836 printf ("%ld/%ld character sets, %d bytes each\n",
837 i/k, j/k, k*sizeof(BSetWord));
838 k = inf_SetType (poset, &i, &j);
839 printf ("%ld/%ld poset items, %d bytes each\n", i, j, k);
840 printf ("%d DFA states\n", dfas->no);
843 static void pr_followpos (void)
846 printf ("\nfollowsets:\n");
847 for (i=1; i <= parse_info->position; i++)
850 pr_Set (poset, followpos[i]);
853 if (posar[i]->u.ch[0] < 0)
854 printf ("#%d", -posar[i]->u.ch[0]);
855 else if (posar[i]->u.ch[1] > posar[i]->u.ch[0])
858 out_char (posar[i]->u.ch[0]);
859 if (posar[i]->u.ch[1] > posar[i]->u.ch[0]+1)
861 out_char (posar[i]->u.ch[1]);
865 out_char (posar[i]->u.ch[0]);
871 static struct DFA_parse *dfa_parse_init (void)
873 parse_info = (struct DFA_parse *) imalloc (sizeof (struct DFA_parse));
875 parse_info->charset = mk_BSetHandle (255, 20);
876 parse_info->position = 0;
877 parse_info->rule = 0;
878 parse_info->root = NULL;
880 parse_info->anyset = mk_BSet (&parse_info->charset);
881 res_BSet (parse_info->charset, parse_info->anyset);
882 add_BSet (parse_info->charset, parse_info->anyset, '\n');
883 com_BSet (parse_info->charset, parse_info->anyset);
884 parse_info->use_Tnode = parse_info->max_Tnode = 0;
888 static void rm_dfa_parse (struct DFA_parse **dfap)
893 rm_BSetHandle (&parse_info->charset);
898 static struct DFA_states *mk_dfas (struct DFA_parse *dfap, int poset_chunk)
900 struct DFA_states *dfas;
902 assert (poset_chunk > 10);
906 poset = mk_SetType (poset_chunk);
909 dfa_trav (parse_info->root);
911 if (debug_dfa_followpos)
913 init_DFA_states (&dfas, poset, STATE_HASH);
925 struct DFA *dfa_init (void)
929 dfa = imalloc (sizeof(*dfa));
930 dfa->parse_info = dfa_parse_init ();
931 dfa->state_info = NULL;
936 int dfa_parse (struct DFA *dfa, char **pattern)
941 do_parse (dfa->parse_info, pattern, dfa_thompson_chars, &top);
944 if ( !dfa->parse_info->root )
945 dfa->parse_info->root = top;
952 n->u.p[0] = dfa->parse_info->root;
954 dfa->parse_info->root = n;
959 void dfa_mkstate (struct DFA *dfa)
963 dfa->state_info = mk_dfas (dfa->parse_info, POSET_CHUNK);
964 dfa->no_states = dfa->state_info->no;
965 dfa->states = dfa->state_info->sortarray;
966 rm_dfa_parse (&dfa->parse_info);
969 void dfa_delete (struct DFA **dfap)
973 if ((*dfap)->parse_info)
974 rm_dfa_parse (&(*dfap)->parse_info);
975 rm_DFA_states (&(*dfap)->state_info);