home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / YACCUNX.ZIP / YCEMTY.1C < prev    next >
Encoding:
Text File  |  1983-12-23  |  2.0 KB  |  82 lines

  1. #include "y1.h"
  2.  
  3. /*
  4.  * 12-Apr-83 (RBD) Add symbolic exit status
  5.  */
  6. cempty()
  7.    {
  8.    /* mark nonterminals which derive the empty string */
  9.    /* also, look for nonterminals which don't derive any token strings */
  10.  
  11. # define EMPTY 1
  12. # define WHOKNOWS 0
  13. # define OK 1
  14.  
  15.    register i, *p;
  16.  
  17.    /* first, use the array pempty to detect productions that can never be reduced */
  18.    /* set pempty to WHONOWS */
  19.    aryfil( pempty, nnonter+1, WHOKNOWS );
  20.  
  21.    /* now, look at productions, marking nonterminals which derive something */
  22.  
  23. more:
  24.    PLOOP(0,i)
  25.       {
  26.       if( pempty[ *prdptr[i] - NTBASE ] ) continue;
  27.       for( p=prdptr[i]+1; *p>=0; ++p )
  28.          {
  29.          if( *p>=NTBASE && pempty[ *p-NTBASE ] == WHOKNOWS ) break;
  30.          }
  31.       if( *p < 0 )
  32.          {
  33.          /* production can be derived */
  34.          pempty[ *prdptr[i]-NTBASE ] = OK;
  35.          goto more;
  36.          }
  37.       }
  38.  
  39.    /* now, look at the nonterminals, to see if they are all OK */
  40.  
  41.    NTLOOP(i)
  42.       {
  43.       /* the added production rises or falls as the start symbol ... */
  44.       if( i == 0 ) continue;
  45.       if( pempty[ i ] != OK )
  46.          {
  47.          fatfl = 0;
  48.          error( "nonterminal %s never derives any token string", nontrst[i].name );
  49.          }
  50.       }
  51.  
  52.    if( nerrors )
  53.       {
  54.       summary();
  55.       exit(EX_ERR);
  56.       }
  57.  
  58.    /* now, compute the pempty array, to see which nonterminals derive the empty string */
  59.  
  60.    /* set pempty to WHOKNOWS */
  61.  
  62.    aryfil( pempty, nnonter+1, WHOKNOWS );
  63.    /* loop as long as we keep finding empty nonterminals */
  64.  
  65. again:
  66.    PLOOP(1,i)
  67.       {
  68.       if( pempty[ *prdptr[i]-NTBASE ]==WHOKNOWS )
  69.          {
  70.          /* not known to be empty */
  71.          for( p=prdptr[i]+1; *p>=NTBASE && pempty[*p-NTBASE]==EMPTY ; ++p ) ;
  72.          if( *p < 0 )
  73.             {
  74.             /* we have a nontrivially empty nonterminal */
  75.             pempty[*prdptr[i]-NTBASE] = EMPTY;
  76.             goto again; /* got one ... try for another */
  77.             }
  78.          }
  79.       }
  80.  
  81.    }
  82.