NetBSD/usr.bin/lex/sym.c

263 lines
6.1 KiB
C
Raw Normal View History

1993-03-21 12:45:37 +03:00
/* sym - symbol table routines */
/*-
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
1993-05-04 11:44:39 +04:00
* Vern Paxson.
1993-03-21 12:45:37 +03:00
*
* The United States Government has rights in this work pursuant
* to contract no. DE-AC03-76SF00098 between the United States
* Department of Energy and the University of California.
*
1993-05-04 11:44:39 +04:00
* Redistribution and use in source and binary forms are permitted provided
* that: (1) source distributions retain this entire copyright notice and
* comment, and (2) distributions including binaries display the following
* acknowledgement: ``This product includes software developed by the
* University of California, Berkeley and its contributors'' in the
* documentation or other materials provided with the distribution and in
* all advertising materials mentioning features or use of this software.
* Neither the name of the University nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1993-03-21 12:45:37 +03:00
*/
1995-05-05 09:35:08 +04:00
/* $Header: /cvsroot/src/usr.bin/lex/Attic/sym.c,v 1.7 1995/05/05 05:35:42 jtc Exp $ */
1993-03-21 12:45:37 +03:00
#include "flexdef.h"
1993-05-04 11:44:39 +04:00
1993-03-21 12:45:37 +03:00
/* declare functions that have forward references */
int hashfunct PROTO((register char[], int));
struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
struct hash_entry *sctbl[START_COND_HASH_SIZE];
struct hash_entry *ccltab[CCL_HASH_SIZE];
struct hash_entry *findsym();
/* addsym - add symbol and definitions to symbol table
*
* -1 is returned if the symbol already exists, and the change not made.
*/
int addsym( sym, str_def, int_def, table, table_size )
register char sym[];
char *str_def;
int int_def;
hash_table table;
int table_size;
{
1993-12-02 22:17:24 +03:00
int hash_val = hashfunct( sym, table_size );
register struct hash_entry *sym_entry = table[hash_val];
register struct hash_entry *new_entry;
register struct hash_entry *successor;
while ( sym_entry )
{
1994-01-07 02:58:18 +03:00
if ( ! strcmp( sym, sym_entry->name ) )
1993-12-02 22:17:24 +03:00
{ /* entry already exists */
return -1;
}
sym_entry = sym_entry->next;
}
/* create new entry */
new_entry = (struct hash_entry *)
1994-01-07 02:58:18 +03:00
flex_alloc( sizeof( struct hash_entry ) );
1993-12-02 22:17:24 +03:00
if ( new_entry == NULL )
1995-05-05 09:35:08 +04:00
flexfatal( _( "symbol table memory allocation failed" ) );
1993-12-02 22:17:24 +03:00
1995-05-05 09:35:08 +04:00
if ( (successor = table[hash_val]) != 0 )
1993-12-02 22:17:24 +03:00
{
new_entry->next = successor;
successor->prev = new_entry;
}
else
new_entry->next = NULL;
new_entry->prev = NULL;
new_entry->name = sym;
new_entry->str_val = str_def;
new_entry->int_val = int_def;
table[hash_val] = new_entry;
return 0;
1993-03-21 12:45:37 +03:00
}
1993-12-02 22:17:24 +03:00
/* cclinstal - save the text of a character class */
1993-03-21 12:45:37 +03:00
void cclinstal( ccltxt, cclnum )
Char ccltxt[];
int cclnum;
1993-12-02 22:17:24 +03:00
{
/* We don't bother checking the return status because we are not
* called unless the symbol is new.
*/
Char *copy_unsigned_string();
(void) addsym( (char *) copy_unsigned_string( ccltxt ),
(char *) 0, cclnum,
ccltab, CCL_HASH_SIZE );
}
1993-03-21 12:45:37 +03:00
/* ccllookup - lookup the number associated with character class text
*
1993-12-02 22:17:24 +03:00
* Returns 0 if there's no CCL associated with the text.
1993-03-21 12:45:37 +03:00
*/
int ccllookup( ccltxt )
Char ccltxt[];
1993-12-02 22:17:24 +03:00
{
return findsym( (char *) ccltxt, ccltab, CCL_HASH_SIZE )->int_val;
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* findsym - find symbol in symbol table */
1993-03-21 12:45:37 +03:00
struct hash_entry *findsym( sym, table, table_size )
register char sym[];
hash_table table;
int table_size;
{
1993-12-02 22:17:24 +03:00
static struct hash_entry empty_entry =
{
1993-12-06 22:29:01 +03:00
(struct hash_entry *) 0, (struct hash_entry *) 0,
(char *) 0, (char *) 0, 0,
1993-12-02 22:17:24 +03:00
} ;
register struct hash_entry *sym_entry =
table[hashfunct( sym, table_size )];
while ( sym_entry )
{
1994-01-07 02:58:18 +03:00
if ( ! strcmp( sym, sym_entry->name ) )
1993-12-02 22:17:24 +03:00
return sym_entry;
sym_entry = sym_entry->next;
}
return &empty_entry;
1993-03-21 12:45:37 +03:00
}
1995-05-05 09:35:08 +04:00
1993-12-02 22:17:24 +03:00
/* hashfunct - compute the hash value for "str" and hash size "hash_size" */
1993-03-21 12:45:37 +03:00
int hashfunct( str, hash_size )
register char str[];
int hash_size;
1993-12-02 22:17:24 +03:00
{
register int hashval;
register int locstr;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
hashval = 0;
locstr = 0;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
while ( str[locstr] )
{
hashval = (hashval << 1) + (unsigned char) str[locstr++];
hashval %= hash_size;
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
return hashval;
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* ndinstal - install a name definition */
1993-03-21 12:45:37 +03:00
1993-12-06 22:29:01 +03:00
void ndinstal( name, definition )
char name[];
Char definition[];
1993-12-02 22:17:24 +03:00
{
char *copy_string();
Char *copy_unsigned_string();
1993-03-21 12:45:37 +03:00
1993-12-06 22:29:01 +03:00
if ( addsym( copy_string( name ),
(char *) copy_unsigned_string( definition ), 0,
1993-12-02 22:17:24 +03:00
ndtbl, NAME_TABLE_HASH_SIZE ) )
1995-05-05 09:35:08 +04:00
synerr( _( "name defined twice" ) );
1993-12-02 22:17:24 +03:00
}
1993-03-21 12:45:37 +03:00
/* ndlookup - lookup a name definition
*
1993-12-02 22:17:24 +03:00
* Returns a nil pointer if the name definition does not exist.
1993-03-21 12:45:37 +03:00
*/
Char *ndlookup( nd )
char nd[];
1993-12-02 22:17:24 +03:00
{
return (Char *) findsym( nd, ndtbl, NAME_TABLE_HASH_SIZE )->str_val;
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* scextend - increase the maximum number of start conditions */
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
void scextend()
1993-03-21 12:45:37 +03:00
{
current_max_scs += MAX_SCS_INCREMENT;
++num_reallocs;
scset = reallocate_integer_array( scset, current_max_scs );
scbol = reallocate_integer_array( scbol, current_max_scs );
scxclu = reallocate_integer_array( scxclu, current_max_scs );
sceof = reallocate_integer_array( sceof, current_max_scs );
scname = reallocate_char_ptr_array( scname, current_max_scs );
}
1993-12-02 22:17:24 +03:00
/* scinstal - make a start condition
*
* NOTE
* The start condition is "exclusive" if xcluflg is true.
*/
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
void scinstal( str, xcluflg )
char str[];
int xcluflg;
{
char *copy_string();
/* Generate start condition definition, for use in BEGIN et al. */
1995-05-05 09:35:08 +04:00
action_define( str, lastsc );
1993-12-02 22:17:24 +03:00
if ( ++lastsc >= current_max_scs )
scextend();
scname[lastsc] = copy_string( str );
if ( addsym( scname[lastsc], (char *) 0, lastsc,
sctbl, START_COND_HASH_SIZE ) )
1995-05-05 09:35:08 +04:00
format_pinpoint_message(
_( "start condition %s declared twice" ),
1993-12-02 22:17:24 +03:00
str );
scset[lastsc] = mkstate( SYM_EPSILON );
scbol[lastsc] = mkstate( SYM_EPSILON );
scxclu[lastsc] = xcluflg;
sceof[lastsc] = false;
}
1993-03-21 12:45:37 +03:00
/* sclookup - lookup the number associated with a start condition
*
1993-12-02 22:17:24 +03:00
* Returns 0 if no such start condition.
1993-03-21 12:45:37 +03:00
*/
int sclookup( str )
char str[];
1993-12-02 22:17:24 +03:00
{
return findsym( str, sctbl, START_COND_HASH_SIZE )->int_val;
}