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

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