/[eliot]/eliot/dic/automaton.c
ViewVC logotype

Diff of /eliot/dic/automaton.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.9.2.2 by afrab, Sun Oct 23 18:38:18 2005 UTC revision 1.9.2.3 by ipkiss, Sun Oct 23 20:58:19 2005 UTC
# Line 17  Line 17 
17  /* along with this program; if not, write to the Free Software               */  /* along with this program; if not, write to the Free Software               */
18  /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */  /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
19    
 /* $Id$ */  
   
20  /**  /**
21   *  \file   automaton.c   *  \file   automaton.c
22   *  \brief  (Non)Deterministic Finite Automaton for Regexp   *  \brief  (Non)Deterministic Finite Automaton for Regexp
# Line 56  typedef struct Automaton_t       *Automa Line 54  typedef struct Automaton_t       *Automa
54  /* ************************************************** *  /* ************************************************** *
55     exported functions for static automata     exported functions for static automata
56   * ************************************************** */   * ************************************************** */
57    
58  automaton automaton_build          (int init_state, int *ptl, int *PS, struct search_RegE_list_t *list);  automaton automaton_build          (int init_state, int *ptl, int *PS, struct search_RegE_list_t *list);
59  void      automaton_delete         (automaton a);  void      automaton_delete         (automaton a);
60  int       automaton_get_nstate     (automaton a);  int       automaton_get_nstate     (automaton a);
# Line 64  int       automaton_get_init       (auto Line 62  int       automaton_get_init       (auto
62  int       automaton_get_accept     (automaton a, int state);  int       automaton_get_accept     (automaton a, int state);
63  int       automaton_get_next_state (automaton a, int start, char l);  int       automaton_get_next_state (automaton a, int start, char l);
64  void      automaton_dump           (automaton a, char* filename);  void      automaton_dump           (automaton a, char* filename);
65        
66    
67  /* ************************************************** *  /* ************************************************** *
68     static functions for dynamic automata     static functions for dynamic automata
# Line 116  struct automaton_t { Line 114  struct automaton_t {
114     exported functions for static automata     exported functions for static automata
115   * ************************************************** */   * ************************************************** */
116    
117  automaton  automaton
118  automaton_build(int init_state, int *ptl, int *PS, struct search_RegE_list_t *list)  automaton_build(int init_state, int *ptl, int *PS, struct search_RegE_list_t *list)
119  {  {
120    Automaton nfa,dfa;    Automaton nfa,dfa;
# Line 139  automaton_build(int init_state, int *ptl Line 137  automaton_build(int init_state, int *ptl
137    return final;    return final;
138  }  }
139    
140  void  void
141  automaton_delete(automaton a)  automaton_delete(automaton a)
142  {  {
143    int i;    int i;
# Line 174  automaton_get_next_state(automaton a, in Line 172  automaton_get_next_state(automaton a, in
172    return a->trans[state][(int)l];    return a->trans[state][(int)l];
173  }  }
174    
175  void  void
176  automaton_dump(automaton a, char* filename)  automaton_dump(automaton a, char* filename)
177  {  {
178    int i,l;    int i,l;
# Line 235  state_delete_fun(void* ps) Line 233  state_delete_fun(void* ps)
233    
234  static Automaton  static Automaton
235  s_automaton_create()  s_automaton_create()
236  {    {
237    Automaton a;    Automaton a;
238    a = (Automaton)malloc(sizeof(struct Automaton_t));    a = (Automaton)malloc(sizeof(struct Automaton_t));
239    a->nstates      = 0;    a->nstates      = 0;
240    a->init_state   = NULL;    a->init_state   = NULL;
# Line 287  s_automaton_state_create(alist id) Line 285  s_automaton_state_create(alist id)
285    return s;    return s;
286  }  }
287    
288  static void  static void
289  s_automaton_add_state(Automaton a, astate s)  s_automaton_add_state(Automaton a, astate s)
290  {  {
291    a->nstates ++;    a->nstates ++;
292    alist_add(a->states,(void*)s);    alist_add(a->states,(void*)s);
293    DMSG(printf("** state %s added to automaton\n",s_automaton_id_to_str(s->id)));    DMSG(printf("** state %s added to automaton\n",s_automaton_id_to_str(s->id)));
# Line 308  s_automaton_get_state(Automaton a, alist Line 306  s_automaton_get_state(Automaton a, alist
306            //DMSG(printf("** get state %s ok\n",s_automaton_id_to_str(s->id)));            //DMSG(printf("** get state %s ok\n",s_automaton_id_to_str(s->id)));
307            return s;            return s;
308          }          }
309      }      }
310    return NULL;    return NULL;
311  }  }
312    
# Line 344  s_automaton_PS_to_NFA(int init_state_id, Line 342  s_automaton_PS_to_NFA(int init_state_id,
342        DMSG(printf("** current state = %s\n",s_automaton_id_to_str(current_state->id)));        DMSG(printf("** current state = %s\n",s_automaton_id_to_str(current_state->id)));
343        memset(used_letter,0,sizeof(used_letter));        memset(used_letter,0,sizeof(used_letter));
344        /* 3: \foreach l in \sigma | l \neq # */        /* 3: \foreach l in \sigma | l \neq # */
345        for(p=1; p < maxpos; p++)        for(p=1; p < maxpos; p++)
346          {          {
347            int current_letter = ptl[p];            int current_letter = ptl[p];
348            if (used_letter[current_letter] == 0)            if (used_letter[current_letter] == 0)
# Line 353  s_automaton_PS_to_NFA(int init_state_id, Line 351  s_automaton_PS_to_NFA(int init_state_id,
351                int pos, ens = 0;                int pos, ens = 0;
352                for(pos = 1; pos <= maxpos; pos++)                for(pos = 1; pos <= maxpos; pos++)
353                  {                  {
354                    if (ptl[pos] == current_letter &&                    if (ptl[pos] == current_letter &&
355                        (int)alist_elt_get_value(alist_get_first(current_state->id)) & (1 << (pos - 1)))                        (int)alist_elt_get_value(alist_get_first(current_state->id)) & (1 << (pos - 1)))
356                      ens |= PS[pos];                      ens |= PS[pos];
357                  }                  }
# Line 369  s_automaton_PS_to_NFA(int init_state_id, Line 367  s_automaton_PS_to_NFA(int init_state_id,
367                        current_state->next[current_letter] = temp_state;                        current_state->next[current_letter] = temp_state;
368                        alist_add(L,temp_state);                        alist_add(L,temp_state);
369                      }                      }
370                    else                    else
371                      {                      {
372                        alist_delete(temp_id);                        alist_delete(temp_id);
373                        current_state->next[current_letter] = temp_state;                        current_state->next[current_letter] = temp_state;
# Line 417  s_automaton_successor(alist S, int lette Line 415  s_automaton_successor(alist S, int lette
415    
416        if ((z = y->next[letter]) != NULL)                   /* \delta (y,z) = l        */        if ((z = y->next[letter]) != NULL)                   /* \delta (y,z) = l        */
417          {          {
418            r = s_automaton_successor(z->id,RE_EPSILON,nfa, list);                r = s_automaton_successor(z->id,RE_EPSILON,nfa, list);
419            alist_insert(Ry,r);            alist_insert(Ry,r);
420            alist_delete(r);            alist_delete(r);
421            alist_insert(Ry,z->id);                          /* Ry = Ry \cup succ(z)    */            alist_insert(Ry,z->id);                          /* Ry = Ry \cup succ(z)    */
# Line 426  s_automaton_successor(alist S, int lette Line 424  s_automaton_successor(alist S, int lette
424        /* \epsilon transition from start node */        /* \epsilon transition from start node */
425        if ((z = y->next[RE_EPSILON]) != NULL)               /* \delta (y,z) = \epsilon */        if ((z = y->next[RE_EPSILON]) != NULL)               /* \delta (y,z) = \epsilon */
426          {          {
427            r = s_automaton_successor(z->id,letter,nfa, list);                r = s_automaton_successor(z->id,letter,nfa, list);
428            alist_insert(Ry,r);                              /* Ry = Ry \cup succ(z)    */            alist_insert(Ry,r);                              /* Ry = Ry \cup succ(z)    */
429            alist_delete(r);            alist_delete(r);
430          }          }
# Line 443  s_automaton_successor(alist S, int lette Line 441  s_automaton_successor(alist S, int lette
441                      DMSG(printf("is in "));                      DMSG(printf("is in "));
442                      DMSG(regexp_print_letter(stdout,i));                      DMSG(regexp_print_letter(stdout,i));
443    
444                      r = s_automaton_successor(z->id,RE_EPSILON,nfa, list);                          r = s_automaton_successor(z->id,RE_EPSILON,nfa, list);
445                      alist_insert(Ry,r);                      alist_insert(Ry,r);
446                      alist_delete(r);                      alist_delete(r);
447                      alist_insert(Ry,z->id);                      alist_insert(Ry,z->id);
# Line 477  s_automaton_node_set_accept(astate s, Au Line 475  s_automaton_node_set_accept(astate s, Au
475        DMSG(printf("%s ",s_automaton_id_to_str(ns->id)));        DMSG(printf("%s ",s_automaton_id_to_str(ns->id)));
476        if (ns->accept && alist_is_in(s->id,idx))        if (ns->accept && alist_is_in(s->id,idx))
477          {          {
478            DMSG(printf("(ok) "));            DMSG(printf("(ok) "));
479            s->accept = 1;            s->accept = 1;
480          }          }
481      }      }
482    DMSG(printf("\n"));    DMSG(printf("\n"));
483  }  }
484    
485  static Automaton  static Automaton
486  s_automaton_NFA_to_DFA(Automaton nfa, struct search_RegE_list_t *list)  s_automaton_NFA_to_DFA(Automaton nfa, struct search_RegE_list_t *list)
487  {  {
488    Automaton dfa = NULL;    Automaton dfa = NULL;
# Line 514  s_automaton_NFA_to_DFA(Automaton nfa, st Line 512  s_automaton_NFA_to_DFA(Automaton nfa, st
512    
513            if (! alist_is_empty(temp_id))            if (! alist_is_empty(temp_id))
514              {              {
515                  
516                DMSG(printf("*** successor of %s for ",s_automaton_id_to_str(current_state->id)));                DMSG(printf("*** successor of %s for ",s_automaton_id_to_str(current_state->id)));
517                DMSG(regexp_print_letter(stdout,letter));                DMSG(regexp_print_letter(stdout,letter));
518                DMSG(printf(" = %s\n", s_automaton_id_to_str(temp_id)));                DMSG(printf(" = %s\n", s_automaton_id_to_str(temp_id)));
# Line 522  s_automaton_NFA_to_DFA(Automaton nfa, st Line 520  s_automaton_NFA_to_DFA(Automaton nfa, st
520                temp_state = s_automaton_get_state(dfa,temp_id);                temp_state = s_automaton_get_state(dfa,temp_id);
521    
522                //          DMSG(printf("*** automaton get state -%s- ok\n",s_automaton_id_to_str(temp_id)));                //          DMSG(printf("*** automaton get state -%s- ok\n",s_automaton_id_to_str(temp_id)));
523                  
524                if (temp_state == NULL)                if (temp_state == NULL)
525                  {                  {
526                    temp_state = s_automaton_state_create(temp_id);                    temp_state = s_automaton_state_create(temp_id);
# Line 540  s_automaton_NFA_to_DFA(Automaton nfa, st Line 538  s_automaton_NFA_to_DFA(Automaton nfa, st
538              {              {
539                alist_delete(temp_id);                alist_delete(temp_id);
540              }              }
541          }          }
542      }      }
543    
544    for(ptr = alist_get_first(dfa->states) ; ptr ; ptr = alist_get_next(dfa->states,ptr))    for(ptr = alist_get_first(dfa->states) ; ptr ; ptr = alist_get_next(dfa->states,ptr))
# Line 548  s_automaton_NFA_to_DFA(Automaton nfa, st Line 546  s_automaton_NFA_to_DFA(Automaton nfa, st
546        astate s = (astate)alist_elt_get_value(ptr);        astate s = (astate)alist_elt_get_value(ptr);
547        s_automaton_node_set_accept(s,nfa);        s_automaton_node_set_accept(s,nfa);
548      }      }
549      
550    alist_delete(L);    alist_delete(L);
551    return dfa;    return dfa;
552  }  }
# Line 557  s_automaton_NFA_to_DFA(Automaton nfa, st Line 555  s_automaton_NFA_to_DFA(Automaton nfa, st
555   * ************************************************** *   * ************************************************** *
556   * ************************************************** */   * ************************************************** */
557    
558  static automaton  static automaton
559  s_automaton_finalize(Automaton a)  s_automaton_finalize(Automaton a)
560  {  {
561    int i,l;    int i,l;
562    automaton fa = NULL;    automaton fa = NULL;
563    alist_elt ptr;    alist_elt ptr;
564    astate s;    astate s;
565    
# Line 593  s_automaton_finalize(Automaton a) Line 591  s_automaton_finalize(Automaton a)
591        s = (astate)alist_elt_get_value(ptr);        s = (astate)alist_elt_get_value(ptr);
592        i = s->id_static;        i = s->id_static;
593    
594        if (s == a->init_state)        if (s == a->init_state)
595          fa->init = i;          fa->init = i;
596        if (s->accept == 1)            if (s->accept == 1)
597          fa->accept[i] = 1;          fa->accept[i] = 1;
598    
599        for(l=0; l < MAX_TRANSITION_LETTERS; l++)        for(l=0; l < MAX_TRANSITION_LETTERS; l++)
# Line 611  s_automaton_finalize(Automaton a) Line 609  s_automaton_finalize(Automaton a)
609   * ************************************************** *   * ************************************************** *
610   * ************************************************** */   * ************************************************** */
611    
612  static void  static void
613  s_automaton_print_nodes(FILE* f, Automaton a)  s_automaton_print_nodes(FILE* f, Automaton a)
614  {  {
615    char * sid;    char * sid;
# Line 635  s_automaton_print_nodes(FILE* f, Automat Line 633  s_automaton_print_nodes(FILE* f, Automat
633    fprintf(f,"\n");    fprintf(f,"\n");
634  }  }
635    
636  static void  static void
637  s_automaton_print_edges(FILE* f, Automaton a)  s_automaton_print_edges(FILE* f, Automaton a)
638  {  {
639    int letter;    int letter;
# Line 660  s_automaton_print_edges(FILE* f, Automat Line 658  s_automaton_print_edges(FILE* f, Automat
658      }      }
659  }  }
660    
661  static void  static void
662  s_automaton_dump(Automaton a, char* filename)  s_automaton_dump(Automaton a, char* filename)
663  {  {
664    FILE* f;    FILE* f;

Legend:
Removed from v.1.9.2.2  
changed lines
  Added in v.1.9.2.3

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26