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 / yapack.3c < prev    next >
Encoding:
Text File  |  1983-12-23  |  1.7 KB  |  74 lines

  1. #include "y3.h"
  2.  
  3. /*
  4.  * yapack.3c
  5.  *
  6.  * Modified to make debug code conditionally compile.
  7.  * 28-Aug-81
  8.  * Bob Denny
  9.  */
  10. apack(p, n ) int *p;
  11.    {
  12.    /* pack state i from temp1 into amem */
  13.    int off;
  14.    register *pp, *qq, *rr;
  15.    int *q, *r;
  16.  
  17.    /* we don't need to worry about checking because we
  18.                    we will only look entries known to be there... */
  19.  
  20.    /* eliminate leading and trailing 0's */
  21.  
  22.    q = p+n;
  23.    for( pp=p,off=0 ; *pp==0 && pp<=q; ++pp,--off ) /* VOID */ ;
  24.    if( pp > q ) return(0);  /* no actions */
  25.    p = pp;
  26.  
  27.    /* now, find a place for the elements from p to q, inclusive */
  28.  
  29.    r = &amem[ACTSIZE-1];
  30.    for( rr=amem; rr<=r; ++rr,++off )
  31.       {
  32.       /* try rr */
  33.       for( qq=rr,pp=p ; pp<=q ; ++pp,++qq)
  34.          {
  35.          if( *pp != 0 )
  36.             {
  37.             if( *pp != *qq && *qq != 0 ) goto nextk;
  38.             }
  39.          }
  40.  
  41.       /* we have found an acceptable k */
  42.  
  43. #ifdef debug
  44.       if(foutput!=NULL) fprintf(foutput,"off = %d, k = %d\n",off,rr-amem);
  45. #endif
  46.       for( qq=rr,pp=p; pp<=q; ++pp,++qq )
  47.          {
  48.          if( *pp )
  49.             {
  50.             if( qq > r ) error( "action table overflow" );
  51.             if( qq>memp ) memp = qq;
  52.             *qq = *pp;
  53.             }
  54.          }
  55. #ifdef debug
  56.       if( foutput!=NULL )
  57.          {
  58.          for( pp=amem; pp<= memp; pp+=10 )
  59.             {
  60.             fprintf( foutput, "\t");
  61.             for( qq=pp; qq<=pp+9; ++qq ) fprintf( foutput, "%d ", *qq );
  62.             fprintf( foutput, "\n");
  63.             }
  64.          }
  65. #endif
  66.       return( off );
  67.  
  68. nextk: 
  69.       ;
  70.       }
  71.    error("no space in action table" );
  72.    /* NOTREACHED */
  73.    }
  74.