home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / lex / sym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-12  |  8.1 KB  |  328 lines

  1. /* sym - symbol table routines */
  2.  
  3. /*-
  4.  * Copyright (c) 1990 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson of Lawrence Berkeley Laboratory.
  9.  * 
  10.  * The United States Government has rights in this work pursuant
  11.  * to contract no. DE-AC03-76SF00098 between the United States
  12.  * Department of Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms, with or without
  15.  * modification, are permitted provided that the following conditions
  16.  * are met:
  17.  * 1. Redistributions of source code must retain the above copyright
  18.  *    notice, this list of conditions and the following disclaimer.
  19.  * 2. Redistributions in binary form must reproduce the above copyright
  20.  *    notice, this list of conditions and the following disclaimer in the
  21.  *    documentation and/or other materials provided with the distribution.
  22.  * 3. All advertising materials mentioning features or use of this software
  23.  *    must display the following acknowledgement:
  24.  *    This product includes software developed by the University of
  25.  *    California, Berkeley and its contributors.
  26.  * 4. Neither the name of the University nor the names of its contributors
  27.  *    may be used to endorse or promote products derived from this software
  28.  *    without specific prior written permission.
  29.  *
  30.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  */
  42.  
  43. #ifndef lint
  44. static char sccsid[] = "@(#)sym.c    5.2 (Berkeley) 6/18/90";
  45. #endif /* not lint */
  46.  
  47. #include "flexdef.h"
  48.  
  49. /* declare functions that have forward references */
  50.  
  51. int hashfunct PROTO((register char[], int));
  52.  
  53.  
  54. struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
  55. struct hash_entry *sctbl[START_COND_HASH_SIZE];
  56. struct hash_entry *ccltab[CCL_HASH_SIZE];
  57.  
  58. struct hash_entry *findsym();
  59.  
  60.  
  61. /* addsym - add symbol and definitions to symbol table
  62.  *
  63.  * synopsis
  64.  *    char sym[], *str_def;
  65.  *    int int_def;
  66.  *    hash_table table;
  67.  *    int table_size;
  68.  *    0 / -1 = addsym( sym, def, int_def, table, table_size );
  69.  *
  70.  * -1 is returned if the symbol already exists, and the change not made.
  71.  */
  72.  
  73. int addsym( sym, str_def, int_def, table, table_size )
  74. register char sym[];
  75. char *str_def;
  76. int int_def;
  77. hash_table table;
  78. int table_size;
  79.  
  80.     {
  81.     int hash_val = hashfunct( sym, table_size );
  82.     register struct hash_entry *sym_entry = table[hash_val];
  83.     register struct hash_entry *new_entry;
  84.     register struct hash_entry *successor;
  85.  
  86.     while ( sym_entry )
  87.     {
  88.     if ( ! strcmp( sym, sym_entry->name ) )
  89.         { /* entry already exists */
  90.         return ( -1 );
  91.         }
  92.     
  93.     sym_entry = sym_entry->next;
  94.     }
  95.  
  96.     /* create new entry */
  97.     new_entry = (struct hash_entry *) malloc( sizeof( struct hash_entry ) );
  98.  
  99.     if ( new_entry == NULL )
  100.     flexfatal( "symbol table memory allocation failed" );
  101.  
  102.     if ( (successor = table[hash_val]) )
  103.     {
  104.     new_entry->next = successor;
  105.     successor->prev = new_entry;
  106.     }
  107.     else
  108.     new_entry->next = NULL;
  109.  
  110.     new_entry->prev = NULL;
  111.     new_entry->name = sym;
  112.     new_entry->str_val = str_def;
  113.     new_entry->int_val = int_def;
  114.  
  115.     table[hash_val] = new_entry;
  116.  
  117.     return ( 0 );
  118.     }
  119.  
  120.  
  121. /* cclinstal - save the text of a character class
  122.  *
  123.  * synopsis
  124.  *    Char ccltxt[];
  125.  *    int cclnum;
  126.  *    cclinstal( ccltxt, cclnum );
  127.  */
  128.  
  129. void cclinstal( ccltxt, cclnum )
  130. Char ccltxt[];
  131. int cclnum;
  132.  
  133.     {
  134.     /* we don't bother checking the return status because we are not called
  135.      * unless the symbol is new
  136.      */
  137.     Char *copy_unsigned_string();
  138.  
  139.     (void) addsym( (char *) copy_unsigned_string( ccltxt ), (char *) 0, cclnum,
  140.            ccltab, CCL_HASH_SIZE );
  141.     }
  142.  
  143.  
  144. /* ccllookup - lookup the number associated with character class text
  145.  *
  146.  * synopsis
  147.  *    Char ccltxt[];
  148.  *    int ccllookup, cclval;
  149.  *    cclval/0 = ccllookup( ccltxt );
  150.  */
  151.  
  152. int ccllookup( ccltxt )
  153. Char ccltxt[];
  154.  
  155.     {
  156.     return ( findsym( (char *) ccltxt, ccltab, CCL_HASH_SIZE )->int_val );
  157.     }
  158.  
  159.  
  160. /* findsym - find symbol in symbol table
  161.  *
  162.  * synopsis
  163.  *    char sym[];
  164.  *    hash_table table;
  165.  *    int table_size;
  166.  *    struct hash_entry *sym_entry, *findsym();
  167.  *    sym_entry = findsym( sym, table, table_size );
  168.  */
  169.  
  170. struct hash_entry *findsym( sym, table, table_size )
  171. register char sym[];
  172. hash_table table;
  173. int table_size;
  174.  
  175.     {
  176.     register struct hash_entry *sym_entry = table[hashfunct( sym, table_size )];
  177.     static struct hash_entry empty_entry =
  178.     {
  179.     (struct hash_entry *) 0, (struct hash_entry *) 0, NULL, NULL, 0,
  180.     } ;
  181.  
  182.     while ( sym_entry )
  183.     {
  184.     if ( ! strcmp( sym, sym_entry->name ) )
  185.         return ( sym_entry );
  186.     sym_entry = sym_entry->next;
  187.     }
  188.  
  189.     return ( &empty_entry );
  190.     }
  191.  
  192.     
  193. /* hashfunct - compute the hash value for "str" and hash size "hash_size"
  194.  *
  195.  * synopsis
  196.  *    char str[];
  197.  *    int hash_size, hash_val;
  198.  *    hash_val = hashfunct( str, hash_size );
  199.  */
  200.  
  201. int hashfunct( str, hash_size )
  202. register char str[];
  203. int hash_size;
  204.  
  205.     {
  206.     register int hashval;
  207.     register int locstr;
  208.  
  209.     hashval = 0;
  210.     locstr = 0;
  211.  
  212.     while ( str[locstr] )
  213.     hashval = ((hashval << 1) + str[locstr++]) % hash_size;
  214.  
  215.     return ( hashval );
  216.     }
  217.  
  218.  
  219. /* ndinstal - install a name definition
  220.  *
  221.  * synopsis
  222.  *    char nd[];
  223.  *    Char def[];
  224.  *    ndinstal( nd, def );
  225.  */
  226.  
  227. void ndinstal( nd, def )
  228. char nd[];
  229. Char def[];
  230.  
  231.     {
  232.     char *copy_string();
  233.     Char *copy_unsigned_string();
  234.  
  235.     if ( addsym( copy_string( nd ), (char *) copy_unsigned_string( def ), 0,
  236.          ndtbl, NAME_TABLE_HASH_SIZE ) )
  237.     synerr( "name defined twice" );
  238.     }
  239.  
  240.  
  241. /* ndlookup - lookup a name definition
  242.  *
  243.  * synopsis
  244.  *    char nd[], *def;
  245.  *    char *ndlookup();
  246.  *    def/NULL = ndlookup( nd );
  247.  */
  248.  
  249. Char *ndlookup( nd )
  250. char nd[];
  251.  
  252.     {
  253.     return ( (Char *) findsym( nd, ndtbl, NAME_TABLE_HASH_SIZE )->str_val );
  254.     }
  255.  
  256.  
  257. /* scinstal - make a start condition
  258.  *
  259.  * synopsis
  260.  *    char str[];
  261.  *    int xcluflg;
  262.  *    scinstal( str, xcluflg );
  263.  *
  264.  * NOTE
  265.  *    the start condition is Exclusive if xcluflg is true
  266.  */
  267.  
  268. void scinstal( str, xcluflg )
  269. char str[];
  270. int xcluflg;
  271.  
  272.     {
  273.     char *copy_string();
  274.  
  275.     /* bit of a hack.  We know how the default start-condition is
  276.      * declared, and don't put out a define for it, because it
  277.      * would come out as "#define 0 1"
  278.      */
  279.     /* actually, this is no longer the case.  The default start-condition
  280.      * is now called "INITIAL".  But we keep the following for the sake
  281.      * of future robustness.
  282.      */
  283.  
  284.     if ( strcmp( str, "0" ) )
  285.     printf( "#define %s %d\n", str, lastsc );
  286.  
  287.     if ( ++lastsc >= current_max_scs )
  288.     {
  289.     current_max_scs += MAX_SCS_INCREMENT;
  290.  
  291.     ++num_reallocs;
  292.  
  293.     scset = reallocate_integer_array( scset, current_max_scs );
  294.     scbol = reallocate_integer_array( scbol, current_max_scs );
  295.     scxclu = reallocate_integer_array( scxclu, current_max_scs );
  296.     sceof = reallocate_integer_array( sceof, current_max_scs );
  297.     scname = reallocate_char_ptr_array( scname, current_max_scs );
  298.     actvsc = reallocate_integer_array( actvsc, current_max_scs );
  299.     }
  300.  
  301.     scname[lastsc] = copy_string( str );
  302.  
  303.     if ( addsym( scname[lastsc], (char *) 0, lastsc,
  304.          sctbl, START_COND_HASH_SIZE ) )
  305.     format_pinpoint_message( "start condition %s declared twice", str );
  306.  
  307.     scset[lastsc] = mkstate( SYM_EPSILON );
  308.     scbol[lastsc] = mkstate( SYM_EPSILON );
  309.     scxclu[lastsc] = xcluflg;
  310.     sceof[lastsc] = false;
  311.     }
  312.  
  313.  
  314. /* sclookup - lookup the number associated with a start condition
  315.  *
  316.  * synopsis
  317.  *    char str[], scnum;
  318.  *    int sclookup;
  319.  *    scnum/0 = sclookup( str );
  320.  */
  321.  
  322. int sclookup( str )
  323. char str[];
  324.  
  325.     {
  326.     return ( findsym( str, sctbl, START_COND_HASH_SIZE )->int_val );
  327.     }
  328.