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

  1. /***********************************************************
  2.  
  3. Copyright 1995 by Tom Lord
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the name of the copyright holder not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. Tom Lord DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  16. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17. EVENT SHALL TOM LORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  18. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  19. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  20. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21. PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25.  
  26.  
  27. #include "rxall.h"
  28. #include "rxsimp.h"
  29.  
  30.  
  31.  
  32.  
  33. /* Could reasonably hashcons instead of in rxunfa.c */
  34.  
  35. #ifdef __STDC__
  36. int
  37. rx_simple_rexp (struct rexp_node ** answer,
  38.         int cset_size,
  39.         struct rexp_node *node,
  40.         struct rexp_node ** subexps)
  41. #else
  42. int
  43. rx_simple_rexp (answer, cset_size, node, subexps)
  44.      struct rexp_node ** answer;
  45.      int cset_size;
  46.      struct rexp_node *node;
  47.      struct rexp_node ** subexps;
  48. #endif
  49. {
  50.   int stat;
  51.  
  52.   if (!node)
  53.     {
  54.       *answer = 0;
  55.       return 0;
  56.     }
  57.  
  58.   if (!node->observed)
  59.     {
  60.       rx_save_rexp (node);
  61.       *answer = node;
  62.       return 0;
  63.     }
  64.  
  65.   if (node->simplified)
  66.     {
  67.       rx_save_rexp (node->simplified);
  68.       *answer = node->simplified;
  69.       return 0;
  70.     }
  71.  
  72.   switch (node->type)
  73.     {
  74.     default:
  75.     case r_cset:
  76.       return -2;        /* an internal error, really */
  77.  
  78.     case r_parens:
  79.       stat = rx_simple_rexp (answer, cset_size,
  80.                  node->params.pair.left,
  81.                  subexps);
  82.       break;
  83.  
  84.     case r_context:
  85.       if (isdigit (node->params.intval))
  86.      stat = rx_simple_rexp (answer, cset_size,
  87.                 subexps [node->params.intval - '0'],
  88.                 subexps);
  89.       else
  90.     {
  91.       *answer = 0;
  92.       stat = 0;
  93.     }
  94.       break;
  95.  
  96.     case r_concat:
  97.     case r_alternate:
  98.     case r_opt:
  99.     case r_star:
  100.     case r_plus:
  101.     case r_interval:
  102.       {
  103.     struct rexp_node *n;
  104.     n = rexp_node (node->type);
  105.     if (!n)
  106.       return -1;
  107.  
  108.     if (node->params.cset)
  109.       {
  110.         n->params.cset = rx_copy_cset (cset_size,
  111.                        node->params.cset);
  112.         if (!n->params.cset)
  113.           {
  114.         rx_free_rexp (n);
  115.         return -1;
  116.           }
  117.       }
  118.     n->params.intval = node->params.intval;
  119.     n->params.intval2 = node->params.intval2;
  120.     {
  121.       int s;
  122.     
  123.       if (!(s = rx_simple_rexp (&n->params.pair.left, cset_size,
  124.                     node->params.pair.left, subexps))
  125.           && !( s= rx_simple_rexp (&n->params.pair.right, cset_size,
  126.                        node->params.pair.right, subexps)))
  127.         {
  128.           *answer = n;
  129.           stat = 0;
  130.         }
  131.       else
  132.         {
  133.           rx_free_rexp  (n);
  134.           stat = s;
  135.         }
  136.     }
  137.       }      
  138.       break;
  139.     }
  140.  
  141.   if (!stat)
  142.     {
  143.       node->simplified = *answer;
  144.       rx_save_rexp (node->simplified);
  145.     }
  146.   return stat;
  147. }  
  148.  
  149.  
  150.