home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OS2-YACC.ZIP / YCEMTY.1C < prev    next >
Text File  |  1989-09-29  |  3KB  |  103 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:      YCEMTY.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 <stdio.h>
  20. # include <stdarg.h>
  21. #include "y1.h"
  22.  
  23. /*
  24.  * 12-Apr-83 (RBD) Add symbolic exit status
  25.  */
  26. void cempty(void)
  27.    {
  28.    /* mark nonterminals which derive the empty string */
  29.    /* also, look for nonterminals which don't derive any token strings */
  30.  
  31. # define EMPTY 1
  32. # define WHOKNOWS 0
  33. # define OK 1
  34.  
  35.    register i, *p;
  36.  
  37.    /* first, use the array pempty to detect productions that can never be reduced */
  38.    /* set pempty to WHONOWS */
  39.    aryfil( pempty, nnonter+1, WHOKNOWS );
  40.  
  41.    /* now, look at productions, marking nonterminals which derive something */
  42.  
  43. more:
  44.    PLOOP(0,i)
  45.       {
  46.       if( pempty[ *prdptr[i] - NTBASE ] ) continue;
  47.       for( p=prdptr[i]+1; *p>=0; ++p )
  48.          {
  49.          if( *p>=NTBASE && pempty[ *p-NTBASE ] == WHOKNOWS ) break;
  50.          }
  51.       if( *p < 0 )
  52.          {
  53.          /* production can be derived */
  54.          pempty[ *prdptr[i]-NTBASE ] = OK;
  55.          goto more;
  56.          }
  57.       }
  58.  
  59.    /* now, look at the nonterminals, to see if they are all OK */
  60.  
  61.    NTLOOP(i)
  62.       {
  63.       /* the added production rises or falls as the start symbol ... */
  64.       if( i == 0 ) continue;
  65.       if( pempty[ i ] != OK )
  66.          {
  67.          fatfl = 0;
  68.          error( "nonterminal %s never derives any token string", nontrst[i].name );
  69.          }
  70.       }
  71.  
  72.    if( nerrors )
  73.       {
  74.       summary();
  75.       exit(EX_ERR);
  76.       }
  77.  
  78.    /* now, compute the pempty array, to see which nonterminals derive the empty string */
  79.  
  80.    /* set pempty to WHOKNOWS */
  81.  
  82.    aryfil( pempty, nnonter+1, WHOKNOWS );
  83.  
  84.    /* loop as long as we keep finding empty nonterminals */
  85.  
  86. again:
  87.    PLOOP(1,i)
  88.       {
  89.       if( pempty[ *prdptr[i]-NTBASE ]==WHOKNOWS )
  90.          {
  91.          /* not known to be empty */
  92.          for( p=prdptr[i]+1; *p>=NTBASE && pempty[*p-NTBASE]==EMPTY ; ++p ) ;
  93.          if( *p < 0 )
  94.             {
  95.             /* we have a nontrivially empty nonterminal */
  96.             pempty[*prdptr[i]-NTBASE] = EMPTY;
  97.             goto again; /* got one ... try for another */
  98.             }
  99.          }
  100.       }
  101.  
  102.    }
  103.