home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OS2-YACC.ZIP / YCLSUR.1C < prev    next >
Text File  |  1989-09-29  |  4KB  |  168 lines

  1. /*
  2.   HEADER: CUG     nnn.nn;
  3.   TITLE:     YACC - Yet Another Compilier-Compilier
  4.   VERSION:     1.0 for IBM-PC
  5.   DATE:      JAN 28, 1985
  6.   DESCRIPTION:     LALR(1) Parser Generator. From UNIX
  7.   KEYWORDS:     Parser Generator Compilier-Compilier YACC
  8.   SYSTEM:     IBM-PC and Compatiables
  9.   FILENAME:      YCLSUR.1C
  10.   WARNINGS:     This program is not for the casual user. It will
  11.          be useful primarily to expert developers.
  12.   CRC:         N/A
  13.   SEE-ALSO:     LEX and PREP
  14.   AUTHORS:     Scott Guthery 11100 leafwood lane Austin, TX 78750
  15.   COMPILERS:     DESMET-C
  16.   REFERENCES:     UNIX Systems Manuals
  17. */
  18.  
  19. #include "y1.h"
  20.  
  21. /*
  22.  * yclsur.1c
  23.  *
  24.  * Modified to make debug code conditionally compile.
  25.  * 28-Aug-81
  26.  * Bob Denny
  27.  */
  28.  
  29. void closure( int i )
  30.    {
  31.    /* generate the closure of state i */
  32.  
  33.    int c, ch, work, k;
  34.    register struct wset *u, *v;
  35.    int *pi;
  36.    int **s, **t;
  37.    struct item *q;
  38.    register struct item *p;
  39.  
  40.    ++zzclose;
  41.  
  42.    /* first, copy kernel of state i to wsets */
  43.  
  44.    cwp = wsets;
  45.    ITMLOOP(i,p,q)
  46.  
  47.       {
  48.       cwp->pitem = p->pitem;
  49.       cwp->flag = 1;    /* this item must get closed */
  50.       SETLOOP(k) cwp->ws.lset[k] = p->look->lset[k];
  51.       WSBUMP(cwp);
  52.       }
  53.  
  54.    /* now, go through the loop, closing each item */
  55.  
  56.    work = 1;
  57.    while( work )
  58.  
  59.       {
  60.       work = 0;
  61.       WSLOOP(wsets,u)
  62.  
  63.          {
  64.  
  65.          if( u->flag == 0 ) continue;
  66.          c = *(u->pitem);  /* dot is before c */
  67.  
  68.          if( c < NTBASE )
  69.  
  70.             {
  71.             u->flag = 0;
  72.             continue;  /* only interesting case is where . is before nonterminal */
  73.             }
  74.  
  75.          /* compute the lookahead */
  76.          aryfil( clset.lset, tbitset, 0 );
  77.  
  78.          /* find items involving c */
  79.  
  80.          WSLOOP(u,v)
  81.  
  82.             {
  83.             if( v->flag == 1 && *(pi=v->pitem) == c )
  84.  
  85.                {
  86.                v->flag = 0;
  87.                if( nolook ) continue;
  88.                while( (ch= *++pi)>0 )
  89.  
  90.                   {
  91.                   if( ch < NTBASE )
  92.  
  93.                      {
  94.                      /* terminal symbol */
  95.                      SETBIT( clset.lset, ch );
  96.                      break;
  97.                      }
  98.                   /* nonterminal symbol */
  99.                   setunion( clset.lset, pfirst[ch-NTBASE]->lset );
  100.                   if( !pempty[ch-NTBASE] ) break;
  101.                   }
  102.                if( ch<=0 ) setunion( clset.lset, v->ws.lset );
  103.                }
  104.             }
  105.  
  106.          /*  now loop over productions derived from c */
  107.  
  108.          c -= NTBASE; /* c is now nonterminal number */
  109.  
  110.          t = pres[c+1];
  111.          for( s=pres[c]; s<t; ++s )
  112.  
  113.             {
  114.             /* put these items into the closure */
  115.             WSLOOP(wsets,v)
  116.  
  117.                {
  118.                /* is the item there */
  119.                if( v->pitem == *s )
  120.  
  121.                   {
  122.                   /* yes, it is there */
  123.                   if( nolook ) goto nexts;
  124.                   if( setunion( v->ws.lset, clset.lset ) ) v->flag = work = 1;
  125.                   goto nexts;
  126.                   }
  127.                }
  128.  
  129.             /*  not there; make a new entry */
  130.             if( cwp-wsets+1 >= WSETSIZE ) error( "working set overflow" );
  131.             cwp->pitem = *s;
  132.             cwp->flag = 1;
  133.             if( !nolook )
  134.  
  135.                {
  136.                work = 1;
  137.                SETLOOP(k) cwp->ws.lset[k] = clset.lset[k];
  138.                }
  139.             WSBUMP(cwp);
  140. nexts: 
  141.             ;
  142.             }
  143.  
  144.          }
  145.       }
  146.  
  147.    /* have computed closure; flags are reset; return */
  148.  
  149.    if( cwp > zzcwp ) 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 ) fprintf( foutput, "flag set!\n");
  160.          u->flag = 0;
  161.          fprintf( foutput, "\t%s", writem(u->pitem));
  162.          prlook( &u->ws );
  163.          fprintf( foutput,  "\n" );
  164.          }
  165.       }
  166. #endif
  167.    }
  168.