home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OS2-YACC.ZIP / PREP4.C < prev    next >
Text File  |  1986-02-06  |  5KB  |  273 lines

  1. /*
  2.   HEADER: CUG     nnn.nn;
  3.   TITLE:     PREP - Preprocessor for YACC
  4.   VERSION:     1.0 for IBM-PC
  5.   DATE:      JAN 28, 1985
  6.   DESCRIPTION:     Preprocessor for YACC Programs
  7.   KEYWORDS:     Preprocessor IBM YACC LEX UNIX
  8.   SYSTEM:     IBM-PC and Compatiables
  9.   FILENAME:     PREP4.C
  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 YACC
  14.   AUTHORS:     Scott Guthery 11100 leafwood lane Austin, TX 78750
  15.   COMPILERS:     DESMET-C
  16.   REFERENCES:     UNIX Systems Manuals
  17. */
  18.  
  19. #define prep4 YES
  20. #include    <stdio.h>
  21. #include    "prep.h"
  22.  
  23. /*
  24.  *
  25.  *    J van Katwijk
  26.  *    Delft University of Technology,
  27.  *    Department of Mathematics,
  28.  *    132 Julianalaan
  29.  *    Delft, The Netherlands
  30.  *
  31.  *    See for a functional description:
  32.  *
  33.  *    SIGPLAN NOTICES
  34.  *    October 1983, 18 (10) pp 12 .. 16
  35.  */
  36. FILE finput, foutput;
  37. char *infile;
  38. char tokname [NAMESIZE];
  39. int tok;
  40. int lineno = 1;
  41.  
  42. /*
  43.  *    process the declaration section of a YACC source
  44.  */
  45.  
  46. prep_decls ()
  47. {
  48.     for (tok = gettok (); tok != MARK && tok != ENDFILE; )
  49.     {
  50.     switch (tok)
  51.     {
  52.         case SEMI:
  53.             fprintf (foutput, ";\n");
  54.             tok = gettok ();
  55.             break;
  56.  
  57.         case START:
  58.             if ( (tok = gettok ()) != IDENT)
  59.                error (FATAL, "bad start construct (YACC)");
  60.  
  61.             fprintf (foutput, "%%start %s\n", tokname);
  62.             tok = gettok ();
  63.             continue;
  64.  
  65.  
  66.         case TYPE:
  67.             if ( (tok = gettok ()) != IDENT)
  68.                error (FATAL, "bad syntax in %%type (YACC)");
  69.  
  70.             fprintf (foutput, "%%typedef %s ", tokname);
  71.  
  72.             while (1)
  73.             { tok = gettok ();
  74.               switch (tok)
  75.               {
  76.                 case IDENT:
  77.                     fprintf (foutput, " %s", tokname);
  78.                     continue;
  79.  
  80.                 case COMMA:
  81.  
  82.                     fprintf (foutput, " ,");
  83.                     continue;
  84.  
  85.                 case SEMI:
  86.                     fprintf (foutput, ";\n");
  87.                     tok = gettok ();
  88.                     break;
  89.  
  90.                 default:
  91.                     break;
  92.                 }
  93.  
  94.              break;
  95.              }
  96.             continue;
  97.  
  98.         case ATTR_DEF:
  99.             proc_def ();
  100.             continue;
  101.  
  102.         case UNION:
  103.             /* copy the union declaration to output */
  104.             cpyunion ();
  105.             tok = gettok ();
  106.             continue;
  107.  
  108.         case LEFT:
  109.         case RIGHT:
  110.         case BINARY:
  111.         case TERM:
  112.             fprintf (foutput, "\n");
  113.             switch (tok)
  114.             {
  115.                 case LEFT:
  116.                     fprintf (foutput, " %%left");
  117.                     break;
  118.  
  119.                 case RIGHT:
  120.                     fprintf (foutput, " %%right");
  121.                     break;
  122.  
  123.                 case BINARY:
  124.                     fprintf (foutput, " %%nonassoc");
  125.                     break;
  126.  
  127.                 case TERM:
  128.                     fprintf (foutput, " %%term");
  129.                     break;
  130.             }
  131.  
  132.             /* get identifiers so defined */
  133.             tok = gettok ();
  134.             if (tok == IDENT)
  135.                { fprintf (foutput, " %s", tokname);
  136.                 tok = gettok ();
  137.                }
  138.  
  139.             while (1)
  140.             { switch (tok)
  141.               {
  142.  
  143.                 case COMMA:
  144.                     fprintf (foutput, " ,");
  145.                     tok = gettok ();
  146.                     break;
  147.  
  148.                 case SEMI:
  149.                     fprintf (foutput, " ;\n");
  150.                     break;
  151.  
  152.                 case IDENT:
  153.                     fprintf (foutput, " %s", tokname);
  154.                     tok = gettok ();
  155.                     continue;
  156.  
  157.                 }
  158.  
  159.                 break;
  160.             }
  161.             continue;
  162.  
  163.         case LCURL:
  164.             cpycode ();
  165.             tok = gettok ();
  166.             continue;
  167.  
  168.         default:
  169.             error (FATAL, "syntax error in def part");
  170.         }
  171.     }
  172.  
  173. /*
  174.  * tok is either MARK or ENDFILE
  175.  */
  176.     if (tok == ENDFILE)
  177.        error (FATAL, "unexpected EOF before %%");
  178.  
  179. }
  180.  
  181. /*
  182.  *    the rules, that is easy
  183.  */
  184. prep_rules ()
  185. {
  186.     fprintf (foutput, "\n%%%%\n");
  187.  
  188.     tok = gettok ();
  189.     while (tok != ENDFILE && tok != MARK)
  190.           { rule ();
  191.         tok = gettok ();
  192.           }
  193. }
  194.  
  195. prep_post ()
  196. {
  197.     register int c;
  198.  
  199.     fprintf (foutput, "\n%%%%\n");
  200.  
  201.     if (tok == MARK)
  202.        while ( (c = unix_getc (finput)) != EOF)
  203.  
  204.            fprintf (foutput, "%c", c);
  205.  
  206.     print_union (); /* the attribute type */
  207.     fprintf (foutput, "\n /* processed by a YACC preprocessor */ \n");
  208. }
  209.  
  210. /*
  211.  *    temp. main program
  212.  *
  213.  *    for yacc preprocessor
  214.  */
  215.  
  216. error (x, y, z)
  217. register int x;
  218. register char *y;
  219. register int z;
  220. {
  221.     printf ("\n*** error line %d :", lineno);
  222.     printf (y, z);
  223.     printf ("       ***\n");
  224.     if (x == FATAL)
  225.        exit (1);
  226. }
  227.  
  228. main (argc, argv)
  229. register char **argv;
  230. register int argc;
  231. {
  232.     if (argc != 3)
  233.        { printf ("usage: prep infile outfile\n");
  234.          exit (1);
  235.        }
  236.  
  237.     infile = argv [1];
  238.     finput = fopen (infile, "r");
  239.     foutput= fopen (argv [2], "w");
  240.     if (finput == 0)
  241.        error (FATAL, "cannot read from %s", infile);
  242.  
  243.     if (foutput == 0)
  244.        error (FATAL, "cannot write to %s", argv[2]);
  245.  
  246.     prep_decls ();
  247.  
  248.     prep_rules ();
  249.  
  250.     prep_post ();
  251.  
  252.     checkdefs ();
  253.     fclose (foutput);
  254. }
  255.  
  256. static char getbuf[30], *getbufptr = getbuf;
  257.  
  258. unix_getc(iop)
  259. FILE iop;
  260. {
  261.     if(getbufptr == getbuf)
  262.         return(getc(iop));
  263.     else
  264.         return(*--getbufptr);
  265. }
  266.  
  267. uungetc(c, iop)
  268. char c;
  269. FILE iop; /* WARNING: iop ignored ... uungetc's are multiplexed!!! */
  270. {
  271.     *getbufptr++ = c;
  272. }
  273.