home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 290_02 / sym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-12  |  6.5 KB  |  288 lines

  1. /* sym - symbol table routines */
  2.  
  3. /*
  4.  * Copyright (c) 1987, the University of California
  5.  * 
  6.  * The United States Government has rights in this work pursuant to
  7.  * contract no. DE-AC03-76SF00098 between the United States Department of
  8.  * Energy and the University of California.
  9.  * 
  10.  * This program may be redistributed.  Enhancements and derivative works
  11.  * may be created provided the new works, if made available to the general
  12.  * public, are made available for use by anyone.
  13.  */
  14.  
  15. #include "flexdef.h"
  16. #include "sym.h"
  17. #include "parse.h"
  18. #include "misc.h"
  19. #include "nfa.h"
  20.  
  21. struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
  22. struct hash_entry *sctbl[START_COND_HASH_SIZE];
  23. struct hash_entry *ccltab[CCL_HASH_SIZE];
  24.  
  25. struct hash_entry *findsym();
  26.  
  27.  
  28. /* addsym - add symbol and definitions to symbol table
  29.  *
  30.  * synopsis
  31.  *    char sym[], *str_def;
  32.  *    int int_def;
  33.  *    hash_table table;
  34.  *    int table_size;
  35.  *    0 / -1 = addsym( sym, def, int_def, table, table_size );
  36.  *
  37.  * -1 is returned if the symbol already exists, and the change not made.
  38.  */
  39.  
  40. int addsym( sym, str_def, int_def, table, table_size )
  41. register char sym[];
  42. char *str_def;
  43. int int_def;
  44. hash_table table;
  45. int table_size;
  46.  
  47. {
  48.     int hash_val = hashfunct( sym, table_size );
  49.     register struct hash_entry *entry = table[hash_val];
  50.     register struct hash_entry *new_entry;
  51.     register struct hash_entry *successor;
  52.  
  53.     while ( entry )
  54.     {
  55.         if ( ! strcmp( sym, entry->name ) )
  56.         { /* entry already exists */
  57.             return ( -1 );
  58.         }
  59.  
  60.         entry = entry->next;
  61.     }
  62.  
  63.     /* create new entry */
  64.     new_entry = (struct hash_entry *) malloc( sizeof( struct hash_entry ) );
  65.  
  66.     if ( new_entry == NULL )
  67.         flexfatal( "symbol table memory allocation failed" );
  68.  
  69.     if ( (successor = table[hash_val]) )
  70.     {
  71.         new_entry->next = successor;
  72.         successor->prev = new_entry;
  73.     }
  74.     else
  75.         new_entry->next = NULL;
  76.  
  77.     new_entry->prev = NULL;
  78.     new_entry->name = sym;
  79.     new_entry->str_val = str_def;
  80.     new_entry->int_val = int_def;
  81.  
  82.     table[hash_val] = new_entry;
  83.  
  84.     return ( 0 );
  85. }
  86.  
  87.  
  88. /* cclinstal - save the text of a character class
  89.  *
  90.  * synopsis
  91.  *    char ccltxt[];
  92.  *    int cclnum;
  93.  *    cclinstal( ccltxt, cclnum );
  94.  */
  95.  
  96. void cclinstal( ccltxt, cclnum )
  97. char ccltxt[];
  98. int cclnum;
  99.  
  100. {
  101.     /* we don't bother checking the return status because we are not called
  102.      * unless the symbol is new
  103.      */
  104.     char *copy_string();
  105.  
  106.     (void) addsym( copy_string( ccltxt ), (char *) 0, cclnum,
  107.       ccltab, CCL_HASH_SIZE );
  108. }
  109.  
  110.  
  111. /* ccllookup - lookup the number associated with character class text
  112.  *
  113.  * synopsis
  114.  *    char ccltxt[];
  115.  *    int ccllookup, cclval;
  116.  *    cclval/0 = ccllookup( ccltxt );
  117.  */
  118.  
  119. int ccllookup( ccltxt )
  120. char ccltxt[];
  121.  
  122. {
  123.     return ( findsym( ccltxt, ccltab, CCL_HASH_SIZE )->int_val );
  124. }
  125.  
  126.  
  127. /* findsym - find symbol in symbol table
  128.  *
  129.  * synopsis
  130.  *    char sym[];
  131.  *    hash_table table;
  132.  *    int table_size;
  133.  *    struct hash_entry *entry, *findsym();
  134.  *    entry = findsym( sym, table, table_size );
  135.  */
  136.  
  137. struct hash_entry *findsym( sym, table, table_size )
  138. register char sym[];
  139. hash_table table;
  140. int table_size;
  141.  
  142. {
  143.     register struct hash_entry *entry = table[hashfunct( sym, table_size )];
  144.     static struct hash_entry empty_entry =
  145.     {
  146.         (struct hash_entry *) 0, (struct hash_entry *) 0, NULL, NULL, 0,
  147.     };
  148.  
  149.     while ( entry )
  150.     {
  151.         if ( ! strcmp( sym, entry->name ) )
  152.             return ( entry );
  153.         entry = entry->next;
  154.     }
  155.  
  156.     return ( &empty_entry );
  157. }
  158.  
  159.  
  160. /* hashfunct - compute the hash value for "str" and hash size "hash_size"
  161.  *
  162.  * synopsis
  163.  *    char str[];
  164.  *    int hash_size, hash_val;
  165.  *    hash_val = hashfunct( str, hash_size );
  166.  */
  167.  
  168. int hashfunct( str, hash_size )
  169. register char str[];
  170. int hash_size;
  171.  
  172. {
  173.     register int hashval;
  174.     register int locstr;
  175.  
  176.     hashval = 0;
  177.     locstr = 0;
  178.  
  179.     while ( str[locstr] )
  180.         hashval = ((hashval << 1) + str[locstr++]) % hash_size;
  181.  
  182.     return ( hashval );
  183. }
  184.  
  185.  
  186. /* ndinstal - install a name definition
  187.  *
  188.  * synopsis
  189.  *    char nd[], def[];
  190.  *    ndinstal( nd, def );
  191.  */
  192.  
  193. void ndinstal( nd, def )
  194. char nd[], def[];
  195.  
  196. {
  197.     char *copy_string();
  198.  
  199.     if ( addsym( copy_string( nd ), copy_string( def ), 0,
  200.       ndtbl, NAME_TABLE_HASH_SIZE ) )
  201.         synerr( "name defined twice" );
  202. }
  203.  
  204.  
  205. /* ndlookup - lookup a name definition
  206.  *
  207.  * synopsis
  208.  *    char nd[], *def;
  209.  *    char *ndlookup();
  210.  *    def/NULL = ndlookup( nd );
  211.  */
  212.  
  213. char *ndlookup( nd )
  214. char nd[];
  215.  
  216. {
  217.     return ( findsym( nd, ndtbl, NAME_TABLE_HASH_SIZE )->str_val );
  218. }
  219.  
  220.  
  221. /* scinstal - make a start condition
  222.  *
  223.  * synopsis
  224.  *    char str[];
  225.  *    int xcluflg;
  226.  *    scinstal( str, xcluflg );
  227.  *
  228.  * NOTE
  229.  *    the start condition is Exclusive if xcluflg is true
  230.  */
  231.  
  232. void scinstal( str, xcluflg )
  233. char str[];
  234. int xcluflg;
  235.  
  236. {
  237.     char *copy_string();
  238.  
  239.     /* bit of a hack.  We know how the default start-condition is
  240.      * declared, and don't put out a define for it, because it
  241.      * would come out as "#define 0 1"
  242.      */
  243.     /* actually, this is no longer the case.  The default start-condition
  244.      * is now called "INITIAL".  But we keep the following for the sake
  245.      * of future robustness.
  246.      */
  247.  
  248.     if ( strcmp( str, "0" ) )
  249.         printf( "#define %s %d\n", str, lastsc * 2 );
  250.  
  251.     if ( ++lastsc >= current_max_scs )
  252.     {
  253.         current_max_scs += MAX_SCS_INCREMENT;
  254.  
  255.         ++num_reallocs;
  256.  
  257.         scset = reallocate_integer_array( scset, current_max_scs );
  258.         scbol = reallocate_integer_array( scbol, current_max_scs );
  259.         scxclu = reallocate_integer_array( scxclu, current_max_scs );
  260.         actvsc = reallocate_integer_array( actvsc, current_max_scs );
  261.     }
  262.  
  263.     if ( addsym( copy_string( str ), (char *) 0, lastsc,
  264.       sctbl, START_COND_HASH_SIZE ) )
  265.         lerrsf( "start condition %s declared twice", str );
  266.  
  267.     scset[lastsc] = mkstate( SYM_EPSILON );
  268.     scbol[lastsc] = mkstate( SYM_EPSILON );
  269.     scxclu[lastsc] = xcluflg;
  270. }
  271.  
  272.  
  273. /* sclookup - lookup the number associated with a start condition
  274.  *
  275.  * synopsis
  276.  *    char str[], scnum;
  277.  *    int sclookup;
  278.  *    scnum/0 = sclookup( str );
  279.  */
  280.  
  281. int sclookup( str )
  282. char str[];
  283.  
  284. {
  285.     return ( findsym( str, sctbl, START_COND_HASH_SIZE )->int_val );
  286. }
  287.  
  288.