home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / comm / yaccunx / yclsur.1c < prev    next >
Encoding:
Text File  |  1983-12-23  |  3.4 KB  |  150 lines

  1. #include "y1.h"
  2.  
  3. /*
  4.  * yclsur.1c
  5.  *
  6.  * Modified to make debug code conditionally compile.
  7.  * 28-Aug-81
  8.  * Bob Denny
  9.  */
  10.  
  11. closure(i)
  12.  
  13.    {
  14.    /* generate the closure of state i */
  15.  
  16.    int c, ch, work, k;
  17.    register struct wset *u, *v;
  18.    int *pi;
  19.    int **s, **t;
  20.    struct item *q;
  21.    register struct item *p;
  22.  
  23.    ++zzclose;
  24.  
  25.    /* first, copy kernel of state i to wsets */
  26.  
  27.    cwp = wsets;
  28.    ITMLOOP(i,p,q)
  29.  
  30.       {
  31.       cwp->pitem = p->pitem;
  32.       cwp->flag = 1;    /* this item must get closed */
  33.       SETLOOP(k) cwp->ws.lset[k] = p->look->lset[k];
  34.       WSBUMP(cwp);
  35.       }
  36.  
  37.    /* now, go through the loop, closing each item */
  38.  
  39.    work = 1;
  40.    while( work )
  41.  
  42.       {
  43.       work = 0;
  44.       WSLOOP(wsets,u)
  45.  
  46.          {
  47.  
  48.          if( u->flag == 0 ) continue;
  49.          c = *(u->pitem);  /* dot is before c */
  50.  
  51.          if( c < NTBASE )
  52.  
  53.             {
  54.             u->flag = 0;
  55.             continue;  /* only interesting case is where . is before nonterminal */
  56.             }
  57.  
  58.          /* compute the lookahead */
  59.          aryfil( clset.lset, tbitset, 0 );
  60.  
  61.          /* find items involving c */
  62.          WSLOOP(u,v)
  63.  
  64.             {
  65.             if( v->flag == 1 && *(pi=v->pitem) == c )
  66.  
  67.                {
  68.                v->flag = 0;
  69.                if( nolook ) continue;
  70.                while( (ch= *++pi)>0 )
  71.  
  72.                   {
  73.                   if( ch < NTBASE )
  74.  
  75.                      {
  76.                      /* terminal symbol */
  77.                      SETBIT( clset.lset, ch );
  78.                      break;
  79.                      }
  80.                   /* nonterminal symbol */
  81.                   setunion( clset.lset, pfirst[ch-NTBASE]->lset );
  82.                   if( !pempty[ch-NTBASE] ) break;
  83.                   }
  84.                if( ch<=0 ) setunion( clset.lset, v->ws.lset );
  85.                }
  86.             }
  87.  
  88.          /*  now loop over productions derived from c */
  89.  
  90.          c -= NTBASE; /* c is now nonterminal number */
  91.  
  92.          t = pres[c+1];
  93.          for( s=pres[c]; s<t; ++s )
  94.  
  95.             {
  96.             /* put these items into the closure */
  97.             WSLOOP(wsets,v)
  98.  
  99.                {
  100.                /* is the item there */
  101.                if( v->pitem == *s )
  102.  
  103.                   {
  104.                   /* yes, it is there */
  105.                   if( nolook ) goto nexts;
  106.                   if( setunion( v->ws.lset, clset.lset ) ) v->flag = work = 1;
  107.                   goto nexts;
  108.                   }
  109.                }
  110.  
  111.             /*  not there; make a new entry */
  112.             if( cwp-wsets+1 >= WSETSIZE ) error( "working set overflow" );
  113.             cwp->pitem = *s;
  114.             cwp->flag = 1;
  115.             if( !nolook )
  116.  
  117.                {
  118.                work = 1;
  119.                SETLOOP(k) cwp->ws.lset[k] = clset.lset[k];
  120.                }
  121.             WSBUMP(cwp);
  122. nexts: 
  123.             ;
  124.             }
  125.  
  126.          }
  127.       }
  128.  
  129.    /* have computed closure; flags are reset; return */
  130.  
  131.    if( cwp > zzcwp ) zzcwp = cwp;
  132.  
  133. #ifdef debug
  134.    if( foutput!=NULL )
  135.  
  136.       {
  137.       fprintf( foutput, "\nState %d, nolook = %d\n", i, nolook );
  138.       WSLOOP(wsets,u)
  139.  
  140.          {
  141.          if( u->flag ) fprintf( foutput, "flag set!\n");
  142.          u->flag = 0;
  143.          fprintf( foutput, "\t%s", writem(u->pitem));
  144.          prlook( &u->ws );
  145.          fprintf( foutput,  "\n" );
  146.          }
  147.       }
  148. #endif
  149.    }
  150.