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