home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnurx.zip / rx / rxunfa.c < prev    next >
C/C++ Source or Header  |  1995-12-31  |  6KB  |  280 lines

  1. /* classes: src_files */
  2.  
  3. /***********************************************************
  4.  
  5. Copyright 1995 by Tom Lord
  6.  
  7.                         All Rights Reserved
  8.  
  9. Permission to use, copy, modify, and distribute this software and its 
  10. documentation for any purpose and without fee is hereby granted, 
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in 
  13. supporting documentation, and that the name of the copyright holder not be
  14. used in advertising or publicity pertaining to distribution of the
  15. software without specific, written prior permission.  
  16.  
  17. Tom Lord DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  18. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  19. EVENT SHALL TOM LORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  20. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  21. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  22. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  23. PERFORMANCE OF THIS SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27.  
  28.  
  29. #include "rxall.h"
  30. #include "rx.h"
  31. #include "rxunfa.h"
  32. #include "rxnfa.h"
  33.  
  34.  
  35. #ifdef __STDC__
  36. static int
  37. unfa_equal (void * va, void * vb)
  38. #else
  39. static int
  40. unfa_equal (va, vb)
  41.      void * va;
  42.      void * vb;
  43. #endif
  44. {
  45.   return rx_rexp_equal ((struct rexp_node *)va, (struct rexp_node *)vb);
  46. }
  47.  
  48.  
  49. struct rx_hash_rules unfa_rules = { unfa_equal, 0, 0, 0, 0 };
  50.  
  51.  
  52. #ifdef __STDC__
  53. static struct rx_cached_rexp *
  54. canonical_unfa (struct rx_hash * table, struct rexp_node * rexp, int cset_size)
  55. #else
  56. static struct rx_cached_rexp *
  57. canonical_unfa (table, rexp, cset_size)
  58.      struct rx_hash * table;
  59.      struct rexp_node * rexp;
  60.      int cset_size;
  61. #endif
  62. {
  63.   struct rx_hash_item * it;
  64.   it = rx_hash_store (table, rx_rexp_hash (rexp, 0), rexp, &unfa_rules);
  65.  
  66.   if (it->binding == 0)
  67.     {
  68.       static int rx_id = 0;
  69.       struct rx * new_rx;
  70.       struct rx_cached_rexp * cr;
  71.  
  72.       if (it->data == (void *)rexp)
  73.     rx_save_rexp (rexp);
  74.       
  75.       cr = (struct rx_cached_rexp *)malloc (sizeof (*cr));
  76.       bzero (cr, sizeof (*cr));
  77.       if (!cr)
  78.     return 0;
  79.       it->binding = (void *)cr;
  80.       cr->unfa.nfa = 0;
  81.       cr->unfa.exp = rexp;
  82.       cr->hash_item = it;
  83.       rx_save_rexp (rexp);
  84.     }
  85.   return (struct rx_cached_rexp *)it->binding;
  86. }
  87.  
  88.  
  89. #ifdef __STDC__
  90. struct rx *
  91. rx_unfa_rx (struct rx_cached_rexp * cr, struct rexp_node * exp, int cset_size)
  92. #else
  93. struct rx *
  94. rx_unfa_rx (cr, exp, cset_size)
  95.      struct rx_cached_rexp * cr;
  96.      struct rexp_node * exp;
  97.      int cset_size;
  98. #endif
  99. {
  100.   struct rx * new_rx;
  101.  
  102.   if (cr->unfa.nfa)
  103.     return cr->unfa.nfa;
  104.  
  105.   new_rx = rx_make_rx (cset_size);
  106.   if (!new_rx)
  107.     return 0;
  108.   {
  109.     struct rx_nfa_state * start;
  110.     struct rx_nfa_state * end;
  111.     start = end = 0;
  112.     if (!rx_build_nfa (new_rx, exp, &start, &end))
  113.       {
  114.     free (new_rx);
  115.     return 0;
  116.       }
  117.     new_rx->start_nfa_states = start;
  118.     end->is_final = 1;
  119.     start->is_start = 1;
  120.     {
  121.       struct rx_nfa_state * s;
  122.       int x;
  123.       x = 0;
  124.       for (s = new_rx->nfa_states; s; s = s->next)
  125.     s->id = x++;
  126.     }
  127.   }
  128.   cr->unfa.nfa = new_rx;
  129.   return new_rx;
  130. }
  131.  
  132.  
  133.  
  134.  
  135. #ifdef __STDC__
  136. struct rx_unfaniverse *
  137. rx_make_unfaniverse (int delay)
  138. #else
  139. struct rx_unfaniverse *
  140. rx_make_unfaniverse (delay)
  141.      int delay;
  142. #endif
  143. {
  144.   struct rx_unfaniverse * it;
  145.   it = (struct rx_unfaniverse *)malloc (sizeof (*it));
  146.   if (!it) return 0;
  147.   bzero (it, sizeof (*it));
  148.   it->delay = delay;
  149.   return it;
  150. }
  151.  
  152.  
  153. #ifdef __STDC__
  154. void
  155. rx_free_unfaniverse (struct rx_unfaniverse * it)
  156. #else
  157. void
  158. rx_free_unfaniverse (it)
  159.      struct rx_unfaniverse * it;
  160. #endif
  161. {
  162. }
  163.  
  164.  
  165.  
  166.  
  167.  
  168. #ifdef __STDC__
  169. struct rx_unfa *
  170. rx_unfa (struct rx_unfaniverse * unfaniverse, struct rexp_node * exp, int cset_size)
  171. #else
  172. struct rx_unfa *
  173. rx_unfa (unfaniverse, exp, cset_size)
  174.      struct rx_unfaniverse * unfaniverse;
  175.      struct rexp_node * exp;
  176.      int cset_size;
  177. #endif
  178. {
  179.   struct rx_cached_rexp * cr;
  180.   if (exp && exp->cr)
  181.     cr = exp->cr;
  182.   else
  183.     {
  184.       cr = canonical_unfa (&unfaniverse->table, exp, cset_size);
  185.       if (exp)
  186.     exp->cr = cr;
  187.     }
  188.   if (!cr)
  189.     return 0;
  190.   if (cr->next)
  191.     {
  192.       if (unfaniverse->free_queue == cr)
  193.     {
  194.       unfaniverse->free_queue = cr->next;
  195.       if (unfaniverse->free_queue == cr)
  196.         unfaniverse->free_queue = 0;
  197.     }
  198.       cr->next->prev = cr->prev;
  199.       cr->prev->next = cr->next;
  200.       cr->next = 0;
  201.       cr->prev = 0;
  202.       --unfaniverse->delayed;
  203.     }
  204.   ++cr->unfa.refs;
  205.   cr->unfa.cset_size = cset_size;
  206.   cr->unfa.verse = unfaniverse;
  207.   rx_unfa_rx (cr, exp, cset_size);
  208.   return &cr->unfa;
  209. }
  210.  
  211.  
  212. #ifdef __STDC__
  213. void
  214. rx_free_unfa (struct rx_unfa * unfa)
  215. #else
  216. void
  217. rx_free_unfa (unfa)
  218.      struct rx_unfa * unfa;
  219. #endif
  220. {
  221.   struct rx_cached_rexp * cr;
  222.  
  223.   cr = (struct rx_cached_rexp *)unfa;
  224.   if (!cr)
  225.     return;
  226.   if (!--cr->unfa.refs)
  227.     {
  228.       if (!unfa->verse->free_queue)
  229.     {
  230.       unfa->verse->free_queue = cr;
  231.       cr->next = cr->prev = cr;
  232.     }
  233.       else
  234.     {
  235.       cr->next = unfa->verse->free_queue;
  236.       cr->prev = unfa->verse->free_queue->prev;
  237.       cr->next->prev = cr;
  238.       cr->prev->next = cr;
  239.     }
  240.  
  241.       ++unfa->verse->delayed;
  242.       while (unfa->verse->delayed > unfa->verse->delay)
  243.     {
  244.       struct rx_cached_rexp * it;
  245.       it = unfa->verse->free_queue;
  246.       unfa->verse->free_queue = it->next;
  247.       if (!--unfa->verse->delayed)
  248.         unfa->verse->free_queue = 0;
  249.       it->prev->next = it->next;
  250.       it->next->prev = it->prev;
  251.       if (it->unfa.exp)
  252.         it->unfa.exp->cr = 0;
  253.       rx_free_rexp ((struct rexp_node *)it->hash_item->data);
  254.       rx_hash_free (it->hash_item, &unfa_rules);
  255.       rx_free_rx (it->unfa.nfa);
  256.       rx_free_rexp (it->unfa.exp);
  257.       free (it);
  258.       if (it == cr)
  259.         break;
  260.     }
  261.     }
  262.   else
  263.     return;
  264. }
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.