home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archimedes / arwart.c < prev   
C/C++ Source or Header  |  2020-01-01  |  16KB  |  904 lines

  1. /* -> c.ckwart
  2.  */ 
  3. char *wartv = "Wart Version 1A(003) 27 May 85";
  4.  
  5. /* W A R T */
  6.  
  7. /*
  8.  pre-process a lex-like file into a C program.
  9.  
  10.  Author:Jeff Damens, Columbia University Center for Computing Activites, 11/84.
  11.  Copyright (C) 1985, Trustees of Columbia University in the City of New York.
  12.  Permission is granted to any individual or institution to use, copy, or
  13.  redistribute this software so long as it is not sold for profit, provided this
  14.  copyright notice is retained. 
  15.  
  16.  * input format is:
  17.  *  lines to be copied | %state <state names...>
  18.  *  %%
  19.  * <state> | <state,state,...> CHAR  { actions }
  20.  * ...
  21.  *  %%
  22.  */
  23.  
  24. #include "ckcdeb.h"         /* Includes */
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #ifdef ANSI
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #endif
  31.  
  32. #define C_L 014             /* Formfeed */
  33.  
  34. #define SEP 1                           /* Token types */
  35. #define LBRACK 2
  36. #define RBRACK 3
  37. #define WORD 4
  38. #define COMMA 5
  39.  
  40. /* Storage sizes */
  41.  
  42. #define MAXSTATES 50            /* max number of states */
  43. #define MAXWORD 50          /* max # of chars/word */
  44. #define SBYTES ((MAXSTATES+7)/8)    /* # of bytes for state bitmask */
  45.  
  46. /* Name of wart function in generated program */
  47.  
  48. #ifndef FNAME
  49. #define FNAME "wart"
  50. #endif
  51.  
  52. /* Structure for state information */
  53.  
  54. struct trans { CHAR states[SBYTES]; /* included states */
  55.                int anyst;       /* true if this good from any state */
  56.                CHAR inchr;      /* input character */
  57.            int actno;       /* associated action */
  58.            struct trans *nxt; };    /* next transition */
  59.  
  60. typedef struct trans *Trans;
  61.  
  62. #ifndef ANSI
  63. char *malloc();             /* Returns pointer (not int) */
  64. #endif
  65.  
  66. #ifdef ANSI
  67. extern void statelist( FILE *, Trans );
  68. extern void initial( FILE *, FILE * );
  69. extern void prolog( FILE * );
  70. extern void rdword( FILE *, char * );
  71. extern void rdstates( FILE *, FILE * );
  72. extern void enter( char *, int );
  73. extern void fatal( char * );
  74. extern void epilogue( FILE * );
  75. extern int gettoken( FILE * );
  76. extern void copyact( FILE *, FILE *, int );
  77. extern void clrhash( void );
  78. extern void copyrest( FILE *, FILE * );
  79. extern void rdcmnt( FILE * );
  80. extern int lkup( char * );
  81. extern void warray( FILE *, char *, int[], int );
  82. #endif
  83.  
  84.  
  85. /* Variables and tables */
  86.  
  87. int lines,nstates,nacts;
  88.  
  89. char tokval[MAXWORD];
  90.  
  91. int tbl[MAXSTATES*128];
  92.  
  93.  
  94.  
  95. char *txt1 = "\n#define BEGIN state =\n\nint state = 0;\n\n";
  96.  
  97. char *fname = FNAME;        /* function name goes here */
  98.  
  99. /* rest of program... */
  100.  
  101. char *txt2 = "()\n\
  102. {\n\
  103.   int c,actno;\n\
  104.   while (1) {\n\
  105.     c = input();\n\
  106.     if ((actno = tbl(c + state*128)) != -1)\n\
  107.       switch(actno) {\n";
  108.  
  109. /* this program's output goes here, followed by final text... */
  110.  
  111. #ifdef ANSI
  112. char *txt3 = "\n    }\n  }\n}\n\n";
  113. #else
  114. char *txt3 = "\n    }\n  }\n\}\n\n";
  115. #endif
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. /*
  137.  * turn on the bit associated with the given state
  138.  *
  139.  */
  140. #ifdef ANSI
  141. void
  142. #endif
  143. setstate(state,t)
  144. int state;
  145. Trans t;
  146. {
  147.   int idx,msk;
  148.   idx = state/8;            /* byte associated with state */
  149.   msk = 0x80 >> (state % 8);        /* bit mask for state */
  150.   t->states[idx] |= msk;
  151. }
  152.  
  153. /*
  154.  * see if the state is involved in the transition
  155.  *
  156.  */
  157.  
  158. #ifdef ANSI
  159. int
  160. #endif
  161. teststate(state,t)
  162. int state;
  163. Trans t;
  164. {
  165.   int idx,msk;
  166.   idx = state/8;
  167.   msk = 0x80 >> (state % 8);
  168.   return(t->states[idx] & msk);
  169. }
  170.  
  171.  
  172. /*
  173.  * read input from here...
  174.  *
  175.  */
  176.  
  177. Trans
  178. rdinput(infp,outfp)
  179. FILE *infp,*outfp;
  180. {
  181. #ifdef ANSI
  182.   Trans x,rdrules( FILE *, FILE * );
  183. #else
  184.   Trans x,rdrules();
  185. #endif
  186.   lines = 1;                /* line counter */
  187.   nstates = 0;              /* no states */
  188.   nacts = 0;                /* no actions yet */
  189.   fprintf(outfp,"\n%c* WARNING -- This C source program generated by ",'/');
  190.   fprintf(outfp,"Wart preprocessor. */\n");
  191.   fprintf(outfp,"%c* Do not edit this file; edit the Wart-format ",'/');
  192.   fprintf(outfp,"source file instead, */\n");
  193.   fprintf(outfp,"%c* and then run it through Wart to produce a new ",'/');
  194.   fprintf(outfp,"C source file.     */\n\n");
  195.   fprintf(outfp,"%c* Wart Version Info: */\n",'/');
  196.   fprintf(outfp,"char *wartv = \"%s\";\n\n",wartv);
  197.  
  198.   initial(infp,outfp);          /* read state names, initial defs */
  199.   prolog(outfp);            /* write out our initial code */
  200.   x = rdrules(infp,outfp);      /* read rules */
  201.   epilogue(outfp);          /* write out epilogue code */
  202.   return(x);
  203. }
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218. /*
  219.  * initial - read initial definitions and state names.  Returns
  220.  * on EOF or %%.
  221.  *
  222.  */
  223.  
  224. #ifdef ANSI
  225. void
  226. #endif
  227. initial(infp,outfp)
  228. FILE *infp,*outfp;
  229. {
  230.   int c;
  231.   char wordbuf[MAXWORD];
  232.   while ((c = getc(infp)) != EOF) {
  233.     if (c == '%') {
  234.             rdword(infp,wordbuf);
  235.             if (strcmp(wordbuf,"states") == 0)
  236.                 rdstates(infp,outfp);
  237.             else if (strcmp(wordbuf,"%") == 0) return;
  238.             else fprintf(outfp,"%%%s",wordbuf);
  239.               }
  240.     else putc(c,outfp);
  241.     if (c == '\n') lines++;
  242.      }
  243. }
  244.  
  245. /*
  246.  * boolean function to tell if the given character can be part of
  247.  * a word.
  248.  *
  249.  */
  250. #ifdef ANSI
  251. int
  252. #endif
  253. isin(s,c) char *s; int c; {
  254.    for (; *s != '\0'; s++)
  255.       if (*s == c) return(1);
  256.    return(0);
  257. }
  258.  
  259. #ifdef ANSI
  260. int
  261. #endif
  262. isword(c)
  263. int c;
  264. {
  265.   static char special[] = ".%_-$@"; /* these are allowable */
  266.   return(isalnum(c) || isin(special,c));
  267. }
  268.  
  269. /*
  270.  * read the next word into the given buffer.
  271.  *
  272.  */
  273. #ifdef ANSI
  274. void
  275. #endif
  276. rdword(fp,buf)
  277. FILE *fp;
  278. char *buf;
  279. {
  280.   int len = 0,c;
  281.   while (isword(c = getc(fp)) && ++len < MAXWORD) *buf++ = c;
  282.   *buf++ = '\0';            /* tie off word */
  283.   ungetc(c,fp);             /* put break char back */
  284. }
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303. /*
  304.  * read state names, up to a newline.
  305.  *
  306.  */
  307.  
  308. #ifdef ANSI
  309. void
  310. #endif
  311. rdstates(fp,ofp)
  312. FILE *fp,*ofp;
  313. {
  314.   int c;
  315.   char wordbuf[MAXWORD];
  316.   while ((c = getc(fp)) != EOF && c != '\n')
  317.   {
  318.     if (isspace(c) || c == C_L) continue;   /* skip whitespace */
  319.     ungetc(c,fp);           /* put char back */
  320.     rdword(fp,wordbuf);     /* read the whole word */
  321.     enter(wordbuf,++nstates);   /* put into symbol tbl */
  322.     fprintf(ofp,"#define %s %d\n",wordbuf,nstates);
  323.   }
  324.   lines++;
  325. }
  326.         
  327. /*
  328.  * allocate a new, empty transition node
  329.  *
  330.  */
  331.  
  332. Trans
  333. newtrans()
  334. {
  335.   Trans new;
  336.   int i;
  337.   new = (Trans) malloc(sizeof (struct trans));
  338.   for (i=0; i<SBYTES; i++) new->states[i] = 0;
  339.   new->anyst = 0;
  340.   new->nxt = NULL;
  341.   return(new);
  342. }
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354. /*
  355.  * read all the rules.
  356.  *
  357.  */
  358.  
  359. Trans
  360. rdrules(fp,out)
  361. FILE *fp,*out;
  362. {
  363.   Trans head,cur,prev;
  364. #ifdef ANSI
  365.   int curtok;
  366. #else
  367.   int curtok,i;
  368. #endif
  369.   head = cur = NULL;
  370.   while ((curtok = gettoken(fp)) != SEP) 
  371.  
  372.     switch(curtok) {
  373.         case LBRACK: if (cur == NULL) cur = newtrans();
  374.                      else fatal("duplicate state list");
  375.                  statelist(fp,cur);/* set states */
  376.                  continue;  /* prepare to read char */
  377.  
  378.         case WORD:   if (strlen(tokval) != 1)
  379.                     fatal("multiple chars in state");
  380.                  if (cur == NULL) {
  381.                 cur = newtrans();
  382.                 cur->anyst = 1;
  383.                 }
  384.                  cur->actno = ++nacts;
  385.                  cur->inchr = tokval[0];
  386.                  if (head == NULL) head = cur;
  387.                  else prev->nxt = cur;
  388.                  prev = cur;
  389.                  cur = NULL;
  390.                  copyact(fp,out,nacts);
  391.                  break; 
  392.          default: fatal("bad input format");
  393.          }
  394.     
  395.    return(head);
  396. }
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406. /*
  407.  * read a list of (comma-separated) states, set them in the
  408.  * given transition.
  409.  *
  410.  */
  411. #ifdef ANSI
  412. void
  413. #endif
  414. statelist(fp,t)
  415. FILE *fp;
  416. Trans t;
  417. {
  418.   int curtok,sval;
  419.   curtok = COMMA;
  420.   while (curtok != RBRACK) {
  421.     if (curtok != COMMA) fatal("missing comma");
  422.     if ((curtok = gettoken(fp)) != WORD) fatal("missing state name");
  423.         if ((sval = lkup(tokval)) == -1) {
  424.         fprintf(stderr,"state %s undefined\n",tokval);
  425.         fatal("undefined state");
  426.        }
  427.         setstate(sval,t);
  428.     curtok = gettoken(fp);
  429.    }
  430. }
  431.  
  432. /*
  433.  * copy an action from the input to the output file
  434.  *
  435.  */
  436. #ifdef ANSI
  437. void
  438. #endif
  439. copyact(inp,outp,actno)
  440. FILE *inp,*outp;
  441. int actno;
  442. {
  443.   int c,bcnt;
  444.   fprintf(outp,"case %d:\n",actno);
  445.   while (((c = getc(inp)) != '\n') && (isspace(c) || c == C_L));
  446.   if (c == '{') {
  447.      bcnt = 1;
  448.      putc(c,outp);
  449.      while (bcnt > 0 && (c = getc(inp)) != EOF) {
  450.     if (c == '{') bcnt++;
  451.     else if (c == '}') bcnt--;
  452.     else if (c == '\n') lines++;
  453.     putc(c,outp);
  454.       }
  455.      if (bcnt > 0) fatal("action doesn't end");
  456.     }
  457.    else {
  458.       while (c != '\n' && c != EOF) {
  459.         putc(c,outp);
  460.         c = getc(inp);
  461.         }
  462.       lines++;
  463.     }
  464.    fprintf(outp,"\nbreak;\n");
  465. }
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484. /*
  485.  * find the action associated with a given character and state.
  486.  * returns -1 if one can't be found.
  487.  *
  488.  */
  489. #ifdef ANSI
  490. int
  491. #endif
  492. faction(hd,state,chr)
  493. Trans hd;
  494. int state,chr;
  495. {
  496.   while (hd != NULL) {
  497.     if (hd->anyst || teststate(state,hd))
  498.       if (hd->inchr == '.' || hd->inchr == chr) return(hd->actno);
  499.     hd = hd->nxt;
  500.     }
  501.   return(-1);
  502. }
  503.  
  504.  
  505. /*
  506.  * empty the table...
  507.  *
  508.  */
  509. #ifdef ANSI
  510. void
  511. #endif
  512. emptytbl()
  513. {
  514.   int i;
  515.   for (i=0; i<nstates*128; i++) tbl[i] = -1;
  516. }
  517.  
  518. /*
  519.  * add the specified action to the output for the given state and chr.
  520.  *
  521.  */
  522.  
  523. #ifdef ANSI
  524. void
  525. #endif
  526. addaction(act,state,chr)
  527. int act,state,chr;
  528. {
  529.  tbl[state*128 + chr] = act;
  530. }
  531.  
  532. #ifdef ANSI
  533. void
  534. #endif
  535. writetbl(fp)
  536. FILE *fp;
  537. #ifdef ARTHUR
  538. { static int jdx[MAXSTATES*128], j, n, l, u;
  539. #else
  540. { int jdx[MAXSTATES*128], j, n, l, u;
  541. #endif
  542.   l = 0;  u = 1;  n = 128*(nstates+1);  j = 0;
  543.   while (u < n) {
  544.     while ((u < n) && (tbl[u] == tbl[l])) ++u;
  545.     jdx[j++] = l;
  546.     l = u;
  547.   }
  548.   jdx[j] = u;
  549.   warray(fp,"jdx",jdx,j);
  550.   fprintf(fp,"\nstatic int jdxupb = %d;\n\n",j);
  551.   for (l = 0; l < j; ++l) jdx[l] = tbl[jdx[l]];
  552.   warray(fp,"jtbl",jdx,j-1);
  553. /*  warray(fp,"tbl",tbl,128*(nstates+1)); */
  554. }
  555.  
  556.  
  557.  
  558.  
  559. /*
  560.  * write an array to the output file, given its name and size.
  561.  *
  562.  */
  563. #ifdef ANSI
  564. void
  565. #endif
  566. warray(fp,nam,cont,siz)
  567. FILE *fp;
  568. char *nam;
  569. int cont[],siz;
  570. {
  571.   int i;
  572.   fprintf(fp,"static short %s[] = {\n",nam);
  573.   for (i = 0; i < siz-1; i++) {
  574.     fprintf(fp,"%d, ",cont[i]);
  575.     if ((i % 20) == 0) putc('\n',fp);
  576.     }
  577.   fprintf(fp,"%d};\n",cont[siz]);
  578. }
  579.  
  580. #ifdef ANSI
  581. int
  582. #endif
  583. main(argc,argv)
  584. int argc;
  585. char *argv[];
  586. {
  587.   Trans head;
  588.   int state,c;
  589.   FILE *infile,*outfile;
  590.  
  591.   if (argc > 1) {
  592.     if ((infile = fopen(argv[1],"r")) == NULL) {
  593.         fprintf(stderr,"Can't open %s\n",argv[1]);
  594.     fatal("unreadable input file"); } }
  595.   else infile = stdin;
  596.  
  597.   if (argc > 2) {
  598.     if ((outfile = fopen(argv[2],"w")) == NULL) {
  599.         fprintf(stderr,"Can't write to %s\n",argv[2]);
  600.     fatal("bad output file"); } }
  601.   else outfile = stdout;
  602.  
  603.   clrhash();                /* empty hash table */
  604.   head = rdinput(infile,outfile);   /* read input file */
  605.   emptytbl();               /* empty our tables */
  606.   for (state = 0; state <= nstates; state++)
  607.     for (c = 1; c < 128; c++)
  608.      addaction(faction(head,state,c),state,c);  /* find actions, add to tbl */
  609.   writetbl(outfile);
  610.   copyrest(infile,outfile);
  611.   printf("%d states, %d actions\n",nstates,nacts);
  612. #ifdef undef
  613.   for (state = 1; state <= nstates; state ++)
  614.     for (c = 1; c < 128; c++)
  615.        if (tbl[state*128 + c] != -1) printf("state %d, chr %d, act %d\n",
  616.         state,c,tbl[state*128 + c]);
  617. #endif
  618.   exit(GOOD_EXIT);
  619. #ifdef ANSI
  620.   return 0;
  621. #endif
  622. }
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640. /*
  641.  * fatal error handler
  642.  *
  643.  */
  644.  
  645. #ifdef ANSI
  646. void
  647. #endif
  648. fatal(msg)
  649. char *msg;
  650. {
  651.   fprintf(stderr,"error in line %d: %s\n",lines,msg);
  652.   exit(BAD_EXIT);
  653. }
  654.  
  655. #ifdef ANSI
  656. void
  657. #endif
  658. prolog(outfp)
  659. FILE *outfp;
  660. {
  661.   int c;
  662.   while ((c = *txt1++) != '\0')  putc(c,outfp);
  663.   while ((c = *fname++) != '\0') putc(c,outfp);
  664.   while ((c = *txt2++) != '\0')  putc(c,outfp);
  665. }
  666.  
  667. #ifdef ANSI
  668. void
  669. #endif
  670. epilogue(outfp)
  671. FILE *outfp;
  672. {
  673.   int c;
  674.   while ((c = *txt3++) != '\0') putc(c,outfp);
  675. }
  676.  
  677. #ifdef ANSI
  678. void
  679. #endif
  680. copyrest(in,out)
  681. FILE *in,*out;
  682. {
  683.   int c;
  684.   while ((c = getc(in)) != EOF) putc(c,out);
  685. }
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700. /*
  701.  * gettoken - returns token type of next token, sets tokval
  702.  * to the string value of the token if appropriate.
  703.  *
  704.  */
  705. #ifdef ANSI
  706. int
  707. #endif
  708. gettoken(fp)
  709. FILE *fp;
  710. {
  711.   int c;
  712.   while (1) {               /* loop if reading comments... */
  713.     do {
  714.       c = getc(fp);
  715.       if (c == '\n') lines++;
  716.        } while ((isspace(c) || c == C_L)); /* skip whitespace */
  717.     switch(c) {
  718.       case EOF: return(SEP);
  719.       case '%': if ((c = getc(fp)) == '%') return(SEP);
  720.             tokval[0] = '%';
  721.             tokval[1] = c;
  722.             rdword(fp,tokval+2);
  723.             return(WORD);
  724.       case '<': return(LBRACK);
  725.       case '>': return(RBRACK);
  726.       case ',': return(COMMA);
  727.       case '/': if ((c = getc(fp)) == '*') {
  728.                   rdcmnt(fp);   /* skip over the comment */
  729.               continue; }   /* and keep looping */
  730.             else {
  731. #ifdef ANSI
  732. /* Surely this is a bug! */
  733.             ungetc(c,fp);  /* put this back into input */
  734. #else
  735.             ungetc(c);  /* put this back into input */
  736. #endif
  737.             c = '/'; }  /* put character back, fall thru */
  738.  
  739.       default: if (isword(c)) {
  740.               ungetc(c,fp);
  741.               rdword(fp,tokval);
  742.               return(WORD);
  743.                 }
  744.            else fatal("Invalid character in input");
  745.          }
  746.   }
  747. }
  748.  
  749. /*
  750.  * skip over a comment
  751.  *
  752.  */
  753.  
  754. #ifdef ANSI
  755. void
  756. #endif
  757. rdcmnt(fp)
  758. FILE *fp;
  759. {
  760.   int c,star,prcnt;
  761.   prcnt = star = 0;         /* no star seen yet */
  762.   while (!((c = getc(fp)) == '/' && star)) {
  763.     if (c == EOF || (prcnt && c == '%')) fatal("Unterminated comment");
  764.     prcnt = (c == '%');
  765.     star = (c == '*');
  766.     if (c == '\n') lines++; }
  767. }
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782. /*
  783.  * symbol table management for wart
  784.  *
  785.  * entry points:
  786.  *   clrhash - empty hash table.
  787.  *   enter - enter a name into the symbol table
  788.  *   lkup - find a name's value in the symbol table.
  789.  *
  790.  */
  791.  
  792. #define HASHSIZE 101            /* # of entries in hash table */
  793.  
  794. struct sym { char *name;        /* symbol name */
  795.          int val;           /* value */
  796.          struct sym *hnxt; }    /* next on collision chain */
  797.     *htab[HASHSIZE];            /* the hash table */
  798.  
  799.  
  800. /*
  801.  * empty the hash table before using it...
  802.  *
  803.  */
  804. #ifdef ANSI
  805. void
  806. #endif
  807. clrhash()
  808. {
  809.   int i;
  810.   for (i=0; i<HASHSIZE; i++) htab[i] = NULL;
  811. }
  812.  
  813. /*
  814.  * compute the value of the hash for a symbol
  815.  *
  816.  */
  817. #ifdef ANSI
  818. int
  819. #endif
  820. hash(name)
  821. char *name;
  822. {
  823.   int sum;
  824.   for (sum = 0; *name != '\0'; name++) sum += (sum + *name);
  825.   sum %= HASHSIZE;          /* take sum mod hashsize */
  826.   if (sum < 0) sum += HASHSIZE;     /* disallow negative hash value */
  827.   return(sum);
  828. }
  829.  
  830. /*
  831.  * make a private copy of a string...
  832.  *
  833.  */
  834. char *
  835. copy(s)
  836. char *s;
  837. {
  838.   char *new;
  839.   new = (char *) malloc(strlen(s) + 1);
  840.   strcpy(new,s);
  841.   return(new);
  842. }
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860. /*
  861.  * enter state name into the hash table
  862.  *
  863.  */
  864. #ifdef ANSI
  865. void
  866. #endif
  867. enter(name,svalue)
  868. char *name;
  869. int svalue;
  870. {
  871.   int h;
  872.   struct sym *cur;
  873.   if (lkup(name) != -1) {
  874. #ifdef ANSI
  875.     fprintf(stderr,"state %s appears twice...\n",name);
  876. #else
  877.     fprintf(stderr,"state %s appears twice...\n");
  878. #endif
  879.     exit(BAD_EXIT); }
  880.   h = hash(name);
  881.   cur = (struct sym *)malloc(sizeof (struct sym));
  882.   cur->name = copy(name);
  883.   cur->val = svalue;
  884.   cur->hnxt = htab[h];
  885.   htab[h] = cur;
  886. }
  887.  
  888. /*
  889.  * find name in the symbol table, return its value.  Returns -1
  890.  * if not found.
  891.  *
  892.  */
  893. #ifdef ANSI
  894. int
  895. #endif
  896. lkup(name)
  897. char *name;
  898. {
  899.   struct sym *cur;
  900.   for (cur = htab[hash(name)]; cur != NULL; cur = cur->hnxt)
  901.     if (strcmp(cur->name,name) == 0) return(cur->val);
  902.   return(-1);
  903. }
  904.