home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OS2-YACC.ZIP / YCLOPT.4C < prev    next >
Text File  |  1989-09-30  |  4KB  |  161 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:      YCLOPT.4C
  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. /* Edits:
  20.  *      06-Dec-80 Broken out of y4.c, impure data in y4imp.c.
  21.  *      18-Dec-80 ZAPFILE not used for decus compiler, fmkdl() used.
  22.  */
  23.  
  24. #include "y1.h"
  25. #include "y4.h"
  26.  
  27. void callopt( void )
  28.    {
  29.  
  30.    register i, *p, j, k, *q;
  31.  
  32.    /* read the arrays from tempfile and set parameters */
  33.  
  34.    
  35.    if( (finput=fopen(TEMPNAME,"r")) == NULL ) error( "optimizer cannot open tempfile" );
  36.    pgo[0] = 0;
  37.    yypact[0] = 0;
  38.    nstate = 0;
  39.    nnonter = 0;
  40.    for(;;)
  41.       {
  42.       switch( gtnm() )
  43.          {
  44.  
  45.       case '\n':
  46.          yypact[++nstate] = (--pmem) - mem0;
  47.  
  48.       case ',':
  49.          continue;
  50.  
  51.       case '$':
  52.          break;
  53.  
  54.       default:
  55.          error( "bad tempfile" );
  56.          }
  57.       break;
  58.       }
  59.  
  60.    yypact[nstate] = yypgo[0] = (--pmem) - mem0;
  61.  
  62.    for(;;)
  63.       {
  64.       switch( gtnm() )
  65.          {
  66.  
  67.       case '\n':
  68.          yypgo[++nnonter]= pmem-mem0;
  69.       case '\r':
  70.       case ',' :
  71.          continue;
  72.  
  73.       case -1: /* EOF */
  74.          break;
  75.  
  76.       default:
  77.          error( "bad tempfile" );
  78.          }
  79.       break;
  80.       }
  81.  
  82.    yypgo[nnonter--] = (--pmem) - mem0;
  83.  
  84.    for( i=0; i<nstate; ++i )
  85.       {
  86.  
  87.       k = 32000;
  88.       j = 0;
  89.       q = mem0 + yypact[i+1];
  90.       for( p = mem0 + yypact[i]; p<q ; p += 2 )
  91.          {
  92.          if( *p > j ) j = *p;
  93.          if( *p < k ) k = *p;
  94.          }
  95.       if( k <= j )
  96.          {
  97.          /* nontrivial situation */
  98.          /* temporarily, kill this for compatibility
  99.                                 j -= k;  j is now the range */
  100.          if( k > maxoff ) maxoff = k;
  101.          }
  102.       greed[i] = (yypact[i+1]-yypact[i]) + 2*j;
  103.       if( j > maxspr ) maxspr = j;
  104.       }
  105.  
  106.    /* initialize ggreed table */
  107.  
  108.    for( i=1; i<=nnonter; ++i )
  109.       {
  110.       ggreed[i] = 1;
  111.       j = 0;
  112.       /* minimum entry index is always 0 */
  113.       q = mem0 + yypgo[i+1] -1;
  114.       for( p = mem0+yypgo[i]; p<q ; p += 2 ) 
  115.          {
  116.          ggreed[i] += 2;
  117.          if( *p > j ) j = *p;
  118.          }
  119.       ggreed[i] = ggreed[i] + 2*j;
  120.       if( j > maxoff ) maxoff = j;
  121.       }
  122.  
  123.    /* now, prepare to put the shift actions into the a array */
  124.  
  125.    for( i=0; i<ACTSIZE; ++i ) a[i] = 0;
  126.    maxa = a;
  127.  
  128.    for( i=0; i<nstate; ++i ) 
  129.       {
  130.       if( greed[i]==0 && adb>1 ) fprintf( ftable, "State %d: null\n", i );
  131.       pa[i] = YYFLAG1;
  132.       }
  133.  
  134.    while( (i = nxti()) != NOMORE ) 
  135.       {
  136.       if( i >= 0 ) stin(i);
  137.       else gin(-i);
  138.  
  139.       }
  140.  
  141.    if( adb>2 )
  142.  
  143.       {
  144.       /* print a array */
  145.       for( p=a; p <= maxa; p += 10)
  146.          {
  147.          fprintf( ftable, "%4d  ", p-a );
  148.          for( i=0; i<10; ++i ) fprintf( ftable, "%4d  ", p[i] );
  149.          fprintf( ftable, "\n" );
  150.          }
  151.       }
  152.    /* write out the output appropriate to the language */
  153.  
  154.    aoutput();
  155.  
  156.    osummary();
  157.  
  158.    fclose(finput);
  159.    ZAPFILE(TEMPNAME);
  160.    }
  161.