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