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

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