home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / yaccsrc2 / yclsur.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-19  |  4.0 KB  |  169 lines

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