2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1995-01-25 11:30:50 adam
8 * Simple error reporting when parsing regular expressions.
9 * Memory usage reduced.
11 * Revision 1.1 1995/01/24 16:02:52 adam
12 * New private header file in dfa module (dfap.h).
13 * Module no longer uses yacc to parse regular expressions.
35 struct Tnode *p[2]; /* CAT,OR,STAR,PLUS (left,right) */
36 short ch[2]; /* if ch[0] >= 0 then this Tnode represents */
37 /* a character in range [ch[0]..ch[1]] */
38 /* otherwise ch[0] represents */
39 /* accepting no (-acceptno) */
41 unsigned pos : 15; /* CAT/OR/STAR/EPSILON or non-neg. position */
42 unsigned nullable : 1;
43 Set firstpos; /* first positions */
44 Set lastpos; /* last positions */
47 struct Tblock { /* Tnode bucket (block) */
48 struct Tblock *next; /* pointer to next bucket */
49 struct Tnode *tarray; /* array of nodes in bucket */
53 #define STATE_HASH 199
54 #define POSET_CHUNK 100
56 int debug_dfa_trav = 0;
57 int debug_dfa_tran = 0;
58 int debug_dfa_followpos = 0;
62 static struct DFA_parse *parse_info = NULL;
65 static int inside_string;
66 static const unsigned char *expr_ptr;
67 static unsigned short *ctrl_chars;
68 static struct Tnode **posar;
71 static Set *followpos;
73 static struct Tnode *mk_Tnode (void);
74 static struct Tnode *mk_Tnode_cset (BSet charset);
75 static void term_Tnode (void);
81 mk_dfa_tran (struct DFA_states *dfas),
82 add_follow (Set lastpos, Set firstpos),
83 dfa_trav (struct Tnode *n),
84 init_followpos (void),
85 mk_dfa_tran (struct DFA_states *dfas),
86 pr_tran (struct DFA_states *dfas),
87 pr_verbose (struct DFA_states *dfas),
97 *str_char (unsigned c);
113 static int lookahead;
114 static unsigned look_ch;
115 static BSet look_chars;
117 static struct Tnode *expr_1 (void),
122 static struct Tnode *expr_1 (void)
124 struct Tnode *t1, *t2, *tn;
126 if (!(t1 = expr_2 ()))
128 while (lookahead == L_ALT)
131 if (!(t2 = expr_2 ()))
143 static struct Tnode *expr_2 (void)
145 struct Tnode *t1, *t2, *tn;
147 if (!(t1 = expr_3 ()))
149 while (lookahead == L_WILD ||
150 lookahead == L_ANYZ ||
151 lookahead == L_ANY ||
153 lookahead == L_CHAR ||
154 lookahead == L_CHARS)
156 if (!(t2 = expr_3 ()))
168 static struct Tnode *expr_3 (void)
170 struct Tnode *t1, *tn;
172 if (!(t1 = expr_4 ()))
174 if (lookahead == L_CLOS0)
182 else if (lookahead == L_CLOS1)
190 else if (lookahead == L_QUEST)
196 tn->u.p[1] = mk_Tnode();
197 tn->u.p[1]->pos = EPSILON;
203 static struct Tnode *expr_4 (void)
211 if (!(t1 = expr_1 ()))
213 if (lookahead == L_RP)
220 t1->pos = ++(parse_info->position);
221 t1->u.ch[1] = t1->u.ch[0] = look_ch;
225 t1 = mk_Tnode_cset (look_chars);
229 t1 = mk_Tnode_cset (parse_info->anyset);
235 t1->u.p[0] = mk_Tnode_cset (parse_info->anyset);
236 t1->u.p[1] = mk_Tnode();
237 t1->u.p[1]->pos = EPSILON;
243 t1->u.p[0] = mk_Tnode_cset (parse_info->anyset);
251 static void do_parse (dfap, s, cc, tnp)
252 struct DFA_parse *dfap;
254 const unsigned short *cc;
258 struct Tnode *t1, *t2;
260 for (i=0; cc[i]; i +=2)
262 ctrl_chars = imalloc ((i+2) * sizeof(*ctrl_chars));
263 for (i=0; (ctrl_chars[i] = cc[i]); i ++)
267 ctrl_chars[i] = L_LP;
270 ctrl_chars[i] = L_RP;
273 ctrl_chars[i] = L_ANY;
276 ctrl_chars[i] = L_ALT;
279 ctrl_chars[i] = L_QUEST;
282 ctrl_chars[i] = L_CLOS1;
285 ctrl_chars[i] = L_CLOS0;
288 ctrl_chars[i] = L_END;
291 ctrl_chars[i] = L_START;
294 ctrl_chars[i] = L_ANY;
297 ctrl_chars[i] = L_ANYZ;
300 ctrl_chars[i] = L_WILD;
307 expr_ptr = (unsigned char *) *s;
312 if (t1 && lookahead == 0)
315 t2->pos = ++parse_info->position;
316 t2->u.ch[0] = -(++parse_info->rule);
327 if (lookahead == L_RP)
328 err_code = DFA_ERR_RP;
329 else if (lookahead == L_LP)
330 err_code = DFA_ERR_LP;
332 err_code = DFA_ERR_SYNTAX;
335 *s = (char *) expr_ptr;
339 static int nextchar (int *esc)
342 if (*expr_ptr == '\0' || isspace(*expr_ptr))
344 else if (*expr_ptr != '\\')
371 static int nextchar_set (int *esc)
373 if (*expr_ptr == ' ')
378 return nextchar (esc);
381 static int read_charset (void)
383 int i, ch0, ch1, esc0, esc1, cc = 0;
384 look_chars = mk_BSet (&parse_info->charset);
385 res_BSet (parse_info->charset, look_chars);
387 ch0 = nextchar_set (&esc0);
388 if (!esc0 && ch0 == '^')
391 ch0 = nextchar_set (&esc0);
395 if (!esc0 && ch0 == ']')
397 add_BSet (parse_info->charset, look_chars, ch0);
398 ch1 = nextchar_set (&esc1);
399 if (!esc1 && ch1 == '-')
401 if ((ch1 = nextchar_set (&esc1)) == 0)
403 if (!esc1 && ch1 == ']')
405 add_BSet (parse_info->charset, look_chars, '-');
408 for (i=ch0; ++i<=ch1;)
409 add_BSet (parse_info->charset, look_chars, i);
410 ch0 = nextchar_set (&esc0);
419 com_BSet (parse_info->charset, look_chars);
423 unsigned short dfa_thompson_chars[] =
437 unsigned short dfa_ccl_chars[] =
445 static int lex_sub(void)
448 const unsigned short *cc;
449 while ((look_ch = nextchar (&esc)) != 0)
454 inside_string = !inside_string;
456 else if (esc || inside_string)
458 else if (look_ch == '[')
459 return read_charset();
460 else if (look_ch == ' ')
464 for (cc = ctrl_chars; *cc; cc += 2)
472 static void lex (void)
474 lookahead = lex_sub ();
477 static const char *str_char (unsigned c)
494 sprintf (s+1, "x%02x", c);
516 static void out_char (int c)
518 printf ("%s", str_char (c));
521 static void term_Tnode (void)
523 struct Tblock *t, *t1;
524 for (t = parse_info->start; (t1 = t);)
532 static struct Tnode *mk_Tnode (void)
536 if (parse_info->use_Tnode == parse_info->max_Tnode)
538 tnew = (struct Tblock *) imalloc (sizeof(struct Tblock));
539 tnew->tarray = (struct Tnode *) imalloc (TADD*sizeof(struct Tnode));
542 if (parse_info->use_Tnode == 0)
543 parse_info->start = tnew;
545 parse_info->end->next = tnew;
546 parse_info->end = tnew;
548 parse_info->max_Tnode += TADD;
550 return parse_info->end->tarray+(parse_info->use_Tnode++ % TADD);
553 struct Tnode *mk_Tnode_cset (BSet charset)
555 struct Tnode *tn1, *tn0 = mk_Tnode();
556 int ch1, ch0 = trav_BSet (parse_info->charset, charset, 0);
562 tn0->pos = ++parse_info->position;
563 ch1 = travi_BSet (parse_info->charset, charset, ch0+1) ;
565 tn0->u.ch[1] = parse_info->charset->size;
568 tn0->u.ch[1] = ch1-1;
569 while ((ch0=trav_BSet (parse_info->charset, charset, ch1)) != -1)
575 tn1 = tn0->u.p[1] = mk_Tnode();
577 tn1->pos = ++(parse_info->position);
578 if ((ch1 = travi_BSet (parse_info->charset, charset,
581 tn1->u.ch[1] = parse_info->charset->size;
584 tn1->u.ch[1] = ch1-1;
591 static void del_followpos (void)
596 static void init_pos (void)
598 posar = (struct Tnode **) imalloc (sizeof(struct Tnode*)
599 * (1+parse_info->position));
602 static void del_pos (void)
607 static void add_follow (Set lastpos, Set firstpos)
611 followpos[lastpos->value] =
612 union_Set (poset, followpos[lastpos->value], firstpos);
613 lastpos = lastpos->next;
617 static void dfa_trav (struct Tnode *n)
622 dfa_trav (n->u.p[0]);
623 dfa_trav (n->u.p[1]);
624 n->nullable = n->u.p[0]->nullable & n->u.p[1]->nullable;
625 n->firstpos = mk_Set (poset);
626 n->firstpos = union_Set (poset, n->firstpos, n->u.p[0]->firstpos);
627 if (n->u.p[0]->nullable)
628 n->firstpos = union_Set (poset, n->firstpos, n->u.p[1]->firstpos);
629 n->lastpos = mk_Set (poset);
630 n->lastpos = union_Set (poset, n->lastpos, n->u.p[1]->lastpos);
631 if (n->u.p[1]->nullable)
632 n->lastpos = union_Set (poset, n->lastpos, n->u.p[0]->lastpos);
634 add_follow (n->u.p[0]->lastpos, n->u.p[1]->firstpos);
636 n->u.p[0]->firstpos = rm_Set (poset, n->u.p[0]->firstpos);
637 n->u.p[0]->lastpos = rm_Set (poset, n->u.p[0]->lastpos);
638 n->u.p[1]->firstpos = rm_Set (poset, n->u.p[1]->firstpos);
639 n->u.p[1]->lastpos = rm_Set (poset, n->u.p[1]->lastpos);
644 dfa_trav (n->u.p[0]);
645 dfa_trav (n->u.p[1]);
646 n->nullable = n->u.p[0]->nullable | n->u.p[1]->nullable;
648 n->firstpos = merge_Set (poset, n->u.p[0]->firstpos,
649 n->u.p[1]->firstpos);
650 n->lastpos = merge_Set (poset, n->u.p[0]->lastpos,
652 n->u.p[0]->firstpos = rm_Set (poset, n->u.p[0]->firstpos);
653 n->u.p[0]->lastpos = rm_Set (poset, n->u.p[0]->lastpos);
654 n->u.p[1]->firstpos = rm_Set (poset, n->u.p[1]->firstpos);
655 n->u.p[1]->lastpos = rm_Set (poset, n->u.p[1]->lastpos);
660 dfa_trav (n->u.p[0]);
661 n->nullable = n->u.p[0]->nullable;
662 n->firstpos = n->u.p[0]->firstpos;
663 n->lastpos = n->u.p[0]->lastpos;
664 add_follow (n->lastpos, n->firstpos);
669 dfa_trav (n->u.p[0]);
671 n->firstpos = n->u.p[0]->firstpos;
672 n->lastpos = n->u.p[0]->lastpos;
673 add_follow (n->lastpos, n->firstpos);
679 n->lastpos = mk_Set (poset);
680 n->firstpos = mk_Set (poset);
687 n->firstpos = mk_Set (poset);
688 n->firstpos = add_Set (poset, n->firstpos, n->pos);
689 n->lastpos = mk_Set (poset);
690 n->lastpos = add_Set (poset, n->lastpos, n->pos);
693 printf ("#%d", -n->u.ch[0]);
694 else if (n->u.ch[1] > n->u.ch[0])
697 out_char (n->u.ch[0]);
698 if (n->u.ch[1] > n->u.ch[0]+1)
700 out_char (n->u.ch[1]);
704 out_char (n->u.ch[0]);
708 printf ("\n nullable : %c\n", n->nullable ? '1' : '0');
709 printf (" firstpos :");
710 pr_Set (poset, n->firstpos);
711 printf (" lastpos :");
712 pr_Set (poset, n->lastpos);
716 static void init_followpos (void)
720 followpos = fa = (Set *) imalloc (sizeof(Set) * (1+parse_info->position));
721 for (i = parse_info->position+1; --i >= 0; fa++)
722 *fa = mk_Set (poset);
725 static void mk_dfa_tran (struct DFA_states *dfas)
732 struct DFA_state *dfa_from, *dfa_to;
735 max_char = parse_info->charset->size;
736 pos = (short *) imalloc (sizeof(*pos) * (parse_info->position+1));
738 tran_set = cp_Set (poset, parse_info->root->firstpos);
739 i = add_DFA_state (dfas, &tran_set, &dfa_from);
741 dfa_from->rule_no = 0;
742 while ((dfa_from = get_DFA_state (dfas)))
746 for (tran_set = dfa_from->set; tran_set; tran_set = tran_set->next)
747 if ((c = posar[tran_set->value]->u.ch[0]) >= 0 && c <= max_char)
748 *pos_i++ = tran_set->value;
749 else if (c < 0 && (i == 0 || c > i))
752 dfa_from->rule_no = -i;
754 for (char_1 = 0; char_1 <= max_char; char_1++)
757 for (pos_i = pos; (i = *pos_i) != -1; ++pos_i)
758 if (posar[i]->u.ch[1] >= char_1)
759 if ((c=posar[i]->u.ch[0]) < char_0)
766 if (char_0 > max_char)
768 tran_set = mk_Set (poset);
769 for (pos_i = pos; (i = *pos_i) != -1; ++pos_i)
770 if ((c=posar[i]->u.ch[1]) >= char_0)
771 if (posar[i]->u.ch[0] <= char_0)
775 tran_set = union_Set (poset, tran_set, followpos[i]);
777 else if (c <= char_1)
781 add_DFA_state (dfas, &tran_set, &dfa_to);
782 add_DFA_tran (dfas, dfa_from, char_0, char_1, dfa_to->no);
787 sort_DFA_states (dfas);
790 static void pr_tran (struct DFA_states *dfas)
793 struct DFA_tran *tran;
794 int prev_no, i, c, no;
796 for (no=0; no < dfas->no; ++no)
798 s = dfas->sortarray[no];
799 assert (s->no == no);
800 printf ("DFA state %d", no);
802 printf ("#%d", s->rule_no);
804 pr_Set (poset, s->set);
806 for (i=s->tran_no, tran=s->trans; --i >= 0; tran++)
808 if (prev_no != tran->to)
812 printf (" goto %d on [", tran->to);
815 for (c = tran->ch[0]; c <= tran->ch[1]; c++)
816 printf ("%s", str_char(c));
824 static void pr_verbose (struct DFA_states *dfas)
828 printf ("%d/%d tree nodes used, %d bytes each\n",
829 parse_info->use_Tnode, parse_info->max_Tnode, sizeof (struct Tnode));
830 k = inf_BSetHandle (parse_info->charset, &i, &j);
831 printf ("%ld/%ld character sets, %d bytes each\n",
832 i/k, j/k, k*sizeof(BSetWord));
833 k = inf_SetType (poset, &i, &j);
834 printf ("%ld/%ld poset items, %d bytes each\n", i, j, k);
835 printf ("%d DFA states\n", dfas->no);
838 static void pr_followpos (void)
841 printf ("\nfollowsets:\n");
842 for (i=1; i <= parse_info->position; i++)
845 pr_Set (poset, followpos[i]);
848 if (posar[i]->u.ch[0] < 0)
849 printf ("#%d", -posar[i]->u.ch[0]);
850 else if (posar[i]->u.ch[1] > posar[i]->u.ch[0])
853 out_char (posar[i]->u.ch[0]);
854 if (posar[i]->u.ch[1] > posar[i]->u.ch[0]+1)
856 out_char (posar[i]->u.ch[1]);
860 out_char (posar[i]->u.ch[0]);
866 static struct DFA_parse *dfa_parse_init (void)
868 parse_info = (struct DFA_parse *) imalloc (sizeof (struct DFA_parse));
870 parse_info->charset = mk_BSetHandle (255, 20);
871 parse_info->position = 0;
872 parse_info->rule = 0;
873 parse_info->root = NULL;
875 parse_info->anyset = mk_BSet (&parse_info->charset);
876 res_BSet (parse_info->charset, parse_info->anyset);
877 add_BSet (parse_info->charset, parse_info->anyset, '\n');
878 com_BSet (parse_info->charset, parse_info->anyset);
879 parse_info->use_Tnode = parse_info->max_Tnode = 0;
883 static void rm_dfa_parse (struct DFA_parse **dfap)
888 rm_BSetHandle (&parse_info->charset);
893 static struct DFA_states *mk_dfas (struct DFA_parse *dfap, int poset_chunk)
895 struct DFA_states *dfas;
897 assert (poset_chunk > 10);
901 poset = mk_SetType (poset_chunk);
904 dfa_trav (parse_info->root);
906 if (debug_dfa_followpos)
908 init_DFA_states (&dfas, poset, STATE_HASH);
920 struct DFA *dfa_init (void)
924 dfa = imalloc (sizeof(*dfa));
925 dfa->parse_info = dfa_parse_init ();
926 dfa->state_info = NULL;
931 int dfa_parse (struct DFA *dfa, char **pattern)
936 do_parse (dfa->parse_info, pattern, dfa_thompson_chars, &top);
939 if ( !dfa->parse_info->root )
940 dfa->parse_info->root = top;
947 n->u.p[0] = dfa->parse_info->root;
949 dfa->parse_info->root = n;
954 void dfa_mkstate (struct DFA *dfa)
958 dfa->state_info = mk_dfas (dfa->parse_info, POSET_CHUNK);
959 dfa->no_states = dfa->state_info->no;
960 dfa->states = dfa->state_info->sortarray;
961 rm_dfa_parse (&dfa->parse_info);
964 void dfa_delete (struct DFA **dfap)
968 if ((*dfap)->parse_info)
969 rm_dfa_parse (&(*dfap)->parse_info);
970 rm_DFA_states (&(*dfap)->state_info);