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.             j= j*10+c-'0';
  65.             c = unix_getc(finput);
  66.             }
  67.  
  68.          j = j*s - offset;
  69.          if( j > 0 )
  70.             {
  71.             error( "Illegal use of $%d", j+offset );
  72.             }
  73.  
  74.          fprintf( faction, "yypvt[-%d]", -j );
  75.          if( ntypes )
  76.             {
  77.             /* put out the proper tag */
  78.             if( j+offset <= 0 && tok < 0 ) error( "must specify type of $%d", j+offset );
  79.             if( tok < 0 ) tok = fdtype( prdptr[nprod][j+offset] );
  80.             fprintf( faction, ".%s", typeset[tok] );
  81.             }
  82.          goto swt;
  83.          }
  84.       putc( '$' , faction );
  85.       if( s<0 ) putc('-', faction );
  86.       goto swt;
  87.  
  88.    case '}':
  89.       if( --brac ) goto lcopy;
  90.       putc( c, faction );
  91.       return;
  92.  
  93.  
  94.    case '/':    /* look for comments */
  95.       putc( c , faction );
  96.       c = unix_getc(finput);
  97.       if( c != '*' ) goto swt;
  98.  
  99.       /* it really is a comment */
  100.  
  101.       putc( c , faction );
  102.       c = unix_getc(finput);
  103.       while( c != EOF )
  104.          {
  105.          while( c=='*' )
  106.             {
  107.             putc( c , faction );
  108.             if( (c=unix_getc(finput)) == '/' ) goto lcopy;
  109.             }
  110.          putc( c , faction );
  111.          if( c == '\n' )++lineno;
  112.          c = unix_getc(finput);
  113.          }
  114.       error( "EOF inside comment" );
  115.  
  116.    case '\'':   /* character constant */
  117.       match = '\'';
  118.       goto string;
  119.  
  120.    case '"':    /* character string */
  121.       match = '"';
  122.  
  123. string:
  124.  
  125.       putc( c , faction );
  126.       while( c=unix_getc(finput) )
  127.          {
  128.  
  129.          if( c=='\\' )
  130.             {
  131.             putc( c , faction );
  132.             c=unix_getc(finput);
  133.             if( c == '\n' ) ++lineno;
  134.             }
  135.          else if( c==match ) goto lcopy;
  136.          else if( c=='\n' ) error( "newline in string or char. const." );
  137.          putc( c , faction );
  138.          }
  139.       error( "EOF in string or character constant" );
  140.  
  141.    case -1: /* EOF */
  142.       error("action does not terminate" );
  143.  
  144.    case '\n':   
  145.       ++lineno;
  146.       goto lcopy;
  147.  
  148.       }
  149.  
  150. lcopy:
  151.    putc( c , faction );
  152.    goto loop;
  153.    }
  154.