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 / ycpact.2c < prev    next >
Encoding:
Text File  |  1983-12-25  |  3.4 KB  |  154 lines

  1. #include <stdio.h>
  2. #include "y2.h"
  3.  
  4. cpyact(offset)
  5.    {
  6.    /* copy C action to the next ; or closing } */
  7.    int brac, c, match, j, s, tok;
  8.  
  9.    fprintf( faction, "\n# line %d\n", lineno );
  10.  
  11.    brac = 0;
  12.  
  13. loop:
  14.    c = unix_getc(finput);
  15. swt:
  16.    switch( c )
  17.       {
  18.  
  19.    case ';':
  20.       if( brac == 0 )
  21.          {
  22.          putc( c , faction );
  23.          return;
  24.          }
  25.       goto lcopy;
  26.  
  27.    case '{':
  28.       brac++;
  29.       goto lcopy;
  30.  
  31.    case '$':
  32.       s = 1;
  33.       tok = -1;
  34.       c = unix_getc(finput);
  35.       if( c == '<' )
  36.          {
  37.          /* type description */
  38.          ungetc( c, finput );
  39.          if( gettok() != TYPENAME ) error( "bad syntax on $<ident> clause" );
  40.          tok = numbval;
  41.          c = unix_getc(finput);
  42.          }
  43.       if( c == '$' )
  44.          {
  45.          fprintf( faction, "yyval");
  46.          if( ntypes )
  47.             {
  48.             /* put out the proper tag... */
  49.             if( tok < 0 ) tok = fdtype( *prdptr[nprod] );
  50.             fprintf( faction, ".%s", typeset[tok] );
  51.             }
  52.          goto loop;
  53.          }
  54.       if( c == '-' )
  55.          {
  56.          s = -s;
  57.          c = unix_getc(finput);
  58.          }
  59.       if( isdigit(c) )
  60.          {
  61.          j=0;
  62.          while( isdigit(c) )
  63.             {
  64.  
  65.             j= j*10+c-'0';
  66.             c = unix_getc(finput);
  67.             }
  68.  
  69.          j = j*s - offset;
  70.          if( j > 0 )
  71.             {
  72.             error( "Illegal use of $%d", j+offset );
  73.             }
  74.  
  75.          fprintf( faction, "yypvt[-%d]", -j );
  76.          if( ntypes )
  77.             {
  78.             /* put out the proper tag */
  79.             if( j+offset <= 0 && tok < 0 ) error( "must specify type of $%d", j+offset );
  80.             if( tok < 0 ) tok = fdtype( prdptr[nprod][j+offset] );
  81.             fprintf( faction, ".%s", typeset[tok] );
  82.             }
  83.          goto swt;
  84.          }
  85.       putc( '$' , faction );
  86.       if( s<0 ) putc('-', faction );
  87.       goto swt;
  88.  
  89.    case '}':
  90.       if( --brac ) goto lcopy;
  91.       putc( c, faction );
  92.       return;
  93.  
  94.  
  95.    case '/':    /* look for comments */
  96.       putc( c , faction );
  97.       c = unix_getc(finput);
  98.       if( c != '*' ) goto swt;
  99.  
  100.       /* it really is a comment */
  101.  
  102.       putc( c , faction );
  103.       c = unix_getc(finput);
  104.       while( c != EOF )
  105.          {
  106.          while( c=='*' )
  107.             {
  108.             putc( c , faction );
  109.             if( (c=unix_getc(finput)) == '/' ) goto lcopy;
  110.             }
  111.          putc( c , faction );
  112.          if( c == '\n' )++lineno;
  113.          c = unix_getc(finput);
  114.          }
  115.       error( "EOF inside comment" );
  116.  
  117.    case '\'':   /* character constant */
  118.       match = '\'';
  119.       goto string;
  120.  
  121.    case '"':    /* character string */
  122.       match = '"';
  123.  
  124. string:
  125.  
  126.       putc( c , faction );
  127.  
  128.       while( c=unix_getc(finput) )
  129.          {
  130.  
  131.          if( c=='\\' )
  132.             {
  133.             putc( c , faction );
  134.             c=unix_getc(finput);
  135.             if( c == '\n' ) ++lineno;
  136.             }
  137.          else if( c==match ) goto lcopy;
  138.          else if( c=='\n' ) error( "newline in string or char. const." );
  139.          putc( c , faction );
  140.          }
  141.       error( "EOF in string or character constant" );
  142.  
  143.    case -1: /* EOF */
  144.       error("action does not terminate" );
  145.  
  146.    case '\n':   
  147.       ++lineno;
  148.       goto lcopy;
  149.  
  150.       }
  151.  
  152. lcopy:
  153.    putc( c , faction );
  154.    goto loop;
  155.    }
  156.