home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / MAWK113.ZIP / mawk113 / rexp / rexp1.c < prev    next >
C/C++ Source or Header  |  1992-02-20  |  4KB  |  217 lines

  1.  
  2. /********************************************
  3. rexp1.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the AWK programming language.
  8.  
  9. Mawk is distributed without warranty under the terms of
  10. the GNU General Public License, version 2, 1991.
  11. ********************************************/
  12.  
  13. /*$Log: rexp1.c,v $
  14.  * Revision 3.4  1992/02/20  16:08:12  brennan
  15.  * change new_TWO() to work around sun acc bug
  16.  *
  17.  * Revision 3.3  91/10/29  10:54:01  brennan
  18.  * SIZE_T
  19.  * 
  20.  * Revision 3.2  91/08/13  09:10:11  brennan
  21.  * VERSION .9994
  22.  * 
  23.  * Revision 3.1  91/06/07  10:33:22  brennan
  24.  * VERSION 0.995
  25.  * 
  26. */
  27.  
  28. /*  re machine  operations  */
  29.  
  30. #include  "rexp.h"
  31.  
  32. static void PROTO( new_TWO , (int, MACHINE *) ) ;
  33.  
  34.  
  35.  
  36. /* initialize a two state machine */
  37. static  void new_TWO(type, mp)
  38.   int type ;
  39.   MACHINE *mp ; /* init mp-> */
  40.   mp->start = (STATE *) RE_malloc(2*STATESZ) ;
  41.   mp->stop = mp->start + 1 ;
  42.   mp->start->type = type ;
  43.   mp->stop->type = M_ACCEPT ;
  44. }
  45.  
  46. /*  build a machine that recognizes any  */
  47. MACHINE  RE_any()
  48. {
  49.   MACHINE x ;
  50.  
  51.   new_TWO(M_ANY, &x) ;
  52.   return x ;
  53. }
  54.  
  55. /*  build a machine that recognizes the start of string  */
  56. MACHINE  RE_start()
  57. {
  58.   MACHINE x ;
  59.  
  60.   new_TWO(M_START, &x) ;
  61.   return x ;
  62. }
  63.  
  64. MACHINE  RE_end()
  65. {
  66.   MACHINE x ;
  67.  
  68.   new_TWO(M_END, &x) ;
  69.   return x ;
  70. }
  71.  
  72. /*  build a machine that recognizes a class  */
  73. MACHINE  RE_class( bvp )
  74.   BV *bvp  ;
  75.   MACHINE x ;
  76.  
  77.   new_TWO(M_CLASS, &x) ;
  78.   x.start->data.bvp = bvp ;
  79.   return x ;
  80. }
  81.  
  82. MACHINE  RE_u()
  83. {
  84.   MACHINE x ;
  85.  
  86.   new_TWO(M_U, &x) ;
  87.   return x ;
  88. }
  89.  
  90. MACHINE  RE_str( str, len)
  91.   char *str ;
  92.   unsigned len ;
  93.   MACHINE x ;
  94.  
  95.   new_TWO(M_STR, &x) ;
  96.   x.start->len = len ;
  97.   x.start->data.str = str ;
  98.   return x ;
  99. }
  100.  
  101.  
  102. /*  replace m and n by a machine that recognizes  mn   */
  103. void  RE_cat( mp, np)
  104.   MACHINE  *mp, *np ;
  105. { unsigned sz1, sz2, sz ;
  106.  
  107.   sz1 = mp->stop - mp->start  ;
  108.   sz2 = np->stop - np->start + 1 ;
  109.   sz  = sz1 + sz2 ;
  110.  
  111.   mp->start = (STATE *) RE_realloc( mp->start, sz * STATESZ ) ;
  112.   mp->stop = mp->start + (sz - 1) ;
  113.   (void)  memcpy( mp->start + sz1, np->start, SIZE_T(sz2 * STATESZ) ) ;
  114.   free( np->start ) ;
  115. }
  116.  
  117.  /*  replace m by a machine that recognizes m|n  */
  118.  
  119. void  RE_or( mp, np)
  120.   MACHINE  *mp, *np ;
  121. { register STATE *p ;
  122.   unsigned szm, szn ;
  123.  
  124.   szm = mp->stop - mp->start + 1 ;
  125.   szn = np->stop - np->start + 1 ;
  126.  
  127.   p = (STATE *) RE_malloc( (szm+szn+1) * STATESZ ) ;
  128.   (void) memcpy( p+1, mp->start, SIZE_T(szm * STATESZ) ) ;
  129.   free( mp->start) ;
  130.   mp->start = p ;
  131.   (mp->stop  = p + szm + szn) -> type = M_ACCEPT ;
  132.   p->type = M_2JA ;
  133.   p->data.jump = szm+1 ;
  134.   (void) memcpy( p + szm + 1 , np->start, SIZE_T(szn * STATESZ)) ;
  135.   free( np->start ) ;
  136.   (p += szm)->type = M_1J ;
  137.   p->data.jump = szn ;
  138. }
  139.  
  140. /*  UNARY  OPERATIONS     */
  141.  
  142. /*  replace m by m*   */
  143.  
  144. void  RE_close( mp )
  145.   MACHINE  *mp ;
  146. { register STATE *p ;
  147.   unsigned sz ;
  148.  
  149.   sz = mp->stop - mp->start + 1 ;
  150.   p = (STATE *) RE_malloc( (sz+2) * STATESZ ) ;
  151.   (void) memcpy( p+1, mp->start, SIZE_T(sz * STATESZ)) ;
  152.   free( mp->start ) ;
  153.   mp->start = p ;
  154.   mp->stop  = p + (sz+1) ;
  155.   p->type = M_2JA ;
  156.   p->data.jump = sz + 1 ;
  157.   (p += sz) -> type = M_2JB ;
  158.   p->data.jump = -(sz-1) ;
  159.   (p+1)->type = M_ACCEPT ;
  160. }
  161.  
  162. /*  replace m  by  m+  (positive closure)   */
  163.  
  164. void  RE_poscl( mp )
  165.   MACHINE  *mp ;
  166. { register STATE *p ;
  167.   unsigned  sz ;
  168.  
  169.   sz = mp->stop - mp->start + 1 ;
  170.   mp->start = p = (STATE *) RE_realloc(mp->start ,  (sz+1) * STATESZ ) ;
  171.   mp->stop  = p + sz ;
  172.   p +=  --sz ;
  173.   p->type = M_2JB ;
  174.   p->data.jump = -sz ;
  175.   (p+1)->type = M_ACCEPT ;
  176. }
  177.  
  178. /* replace  m  by  m? (zero or one)  */
  179.  
  180. void  RE_01( mp )
  181.   MACHINE  *mp ;
  182. { unsigned  sz ;
  183.   register  STATE *p ;
  184.  
  185.   sz = mp->stop - mp->start + 1 ;
  186.   p = (STATE *) RE_malloc( (sz+1) * STATESZ ) ;
  187.   (void) memcpy( p+1, mp->start, SIZE_T(sz * STATESZ)) ;
  188.   free( mp->start ) ;
  189.   mp->start = p ;
  190.   mp->stop = p + sz ;
  191.   p->type = M_2JB ;
  192.   p->data.jump = sz ;
  193. }
  194.  
  195. /*===================================
  196. MEMORY  ALLOCATION
  197.  *==============================*/
  198.  
  199.  
  200. VOID *RE_malloc( sz ) 
  201.   unsigned sz ;
  202. { register VOID *p ;
  203.  
  204.   if ( ! ( p = malloc(SIZE_T(sz)) ) )  RE_error_trap(MEMORY_FAILURE) ;
  205.   return p ;
  206. }
  207.  
  208. VOID *RE_realloc( p, sz)
  209.   register VOID *p ; unsigned sz ;
  210. { if ( ! ( p = realloc( p, SIZE_T(sz)) ) )  RE_error_trap(MEMORY_FAILURE) ;
  211.   return p ;
  212. }
  213.  
  214.