|
Packit |
f00812 |
/* sym - symbol table routines */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* Copyright (c) 1990 The Regents of the University of California. */
|
|
Packit |
f00812 |
/* All rights reserved. */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* This code is derived from software contributed to Berkeley by */
|
|
Packit |
f00812 |
/* Vern Paxson. */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* The United States Government has rights in this work pursuant */
|
|
Packit |
f00812 |
/* to contract no. DE-AC03-76SF00098 between the United States */
|
|
Packit |
f00812 |
/* Department of Energy and the University of California. */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* This file is part of flex. */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* Redistribution and use in source and binary forms, with or without */
|
|
Packit |
f00812 |
/* modification, are permitted provided that the following conditions */
|
|
Packit |
f00812 |
/* are met: */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* 1. Redistributions of source code must retain the above copyright */
|
|
Packit |
f00812 |
/* notice, this list of conditions and the following disclaimer. */
|
|
Packit |
f00812 |
/* 2. Redistributions in binary form must reproduce the above copyright */
|
|
Packit |
f00812 |
/* notice, this list of conditions and the following disclaimer in the */
|
|
Packit |
f00812 |
/* documentation and/or other materials provided with the distribution. */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* Neither the name of the University nor the names of its contributors */
|
|
Packit |
f00812 |
/* may be used to endorse or promote products derived from this software */
|
|
Packit |
f00812 |
/* without specific prior written permission. */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
|
|
Packit |
f00812 |
/* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
|
|
Packit |
f00812 |
/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
|
|
Packit |
f00812 |
/* PURPOSE. */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
#include "flexdef.h"
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* Variables for symbol tables:
|
|
Packit |
f00812 |
* sctbl - start-condition symbol table
|
|
Packit |
f00812 |
* ndtbl - name-definition symbol table
|
|
Packit |
f00812 |
* ccltab - character class text symbol table
|
|
Packit |
f00812 |
*/
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
struct hash_entry {
|
|
Packit |
f00812 |
struct hash_entry *prev, *next;
|
|
Packit |
f00812 |
char *name;
|
|
Packit |
f00812 |
char *str_val;
|
|
Packit |
f00812 |
int int_val;
|
|
Packit |
f00812 |
};
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
typedef struct hash_entry **hash_table;
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
#define NAME_TABLE_HASH_SIZE 101
|
|
Packit |
f00812 |
#define START_COND_HASH_SIZE 101
|
|
Packit |
f00812 |
#define CCL_HASH_SIZE 101
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
static struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
|
|
Packit |
f00812 |
static struct hash_entry *sctbl[START_COND_HASH_SIZE];
|
|
Packit |
f00812 |
static struct hash_entry *ccltab[CCL_HASH_SIZE];
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* declare functions that have forward references */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
static int addsym(char[], char *, int, hash_table, int);
|
|
Packit |
f00812 |
static struct hash_entry *findsym (const char *sym, hash_table table,
|
|
Packit |
f00812 |
int table_size);
|
|
Packit |
f00812 |
static int hashfunct(const char *, int);
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* addsym - add symbol and definitions to symbol table
|
|
Packit |
f00812 |
*
|
|
Packit |
f00812 |
* -1 is returned if the symbol already exists, and the change not made.
|
|
Packit |
f00812 |
*/
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
static int addsym (char sym[], char *str_def, int int_def, hash_table table, int table_size)
|
|
Packit |
f00812 |
{
|
|
Packit |
f00812 |
int hash_val = hashfunct (sym, table_size);
|
|
Packit |
f00812 |
struct hash_entry *sym_entry = table[hash_val];
|
|
Packit |
f00812 |
struct hash_entry *new_entry;
|
|
Packit |
f00812 |
struct hash_entry *successor;
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
while (sym_entry) {
|
|
Packit |
f00812 |
if (!strcmp (sym, sym_entry->name)) { /* entry already exists */
|
|
Packit |
f00812 |
return -1;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
sym_entry = sym_entry->next;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* create new entry */
|
|
Packit |
f00812 |
new_entry = malloc(sizeof(struct hash_entry));
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
if (new_entry == NULL)
|
|
Packit |
f00812 |
flexfatal (_("symbol table memory allocation failed"));
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
if ((successor = table[hash_val]) != 0) {
|
|
Packit |
f00812 |
new_entry->next = successor;
|
|
Packit |
f00812 |
successor->prev = new_entry;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
else
|
|
Packit |
f00812 |
new_entry->next = NULL;
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
new_entry->prev = NULL;
|
|
Packit |
f00812 |
new_entry->name = sym;
|
|
Packit |
f00812 |
new_entry->str_val = str_def;
|
|
Packit |
f00812 |
new_entry->int_val = int_def;
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
table[hash_val] = new_entry;
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
return 0;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* cclinstal - save the text of a character class */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
void cclinstal (char ccltxt[], int cclnum)
|
|
Packit |
f00812 |
{
|
|
Packit |
f00812 |
/* We don't bother checking the return status because we are not
|
|
Packit |
f00812 |
* called unless the symbol is new.
|
|
Packit |
f00812 |
*/
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
(void) addsym (xstrdup(ccltxt),
|
|
Packit |
f00812 |
(char *) 0, cclnum, ccltab, CCL_HASH_SIZE);
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* ccllookup - lookup the number associated with character class text
|
|
Packit |
f00812 |
*
|
|
Packit |
f00812 |
* Returns 0 if there's no CCL associated with the text.
|
|
Packit |
f00812 |
*/
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
int ccllookup (char ccltxt[])
|
|
Packit |
f00812 |
{
|
|
Packit |
f00812 |
return findsym (ccltxt, ccltab, CCL_HASH_SIZE)->int_val;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* findsym - find symbol in symbol table */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
static struct hash_entry *findsym (const char *sym, hash_table table, int table_size)
|
|
Packit |
f00812 |
{
|
|
Packit |
f00812 |
static struct hash_entry empty_entry = {
|
|
Packit |
f00812 |
NULL, NULL, NULL, NULL, 0,
|
|
Packit |
f00812 |
};
|
|
Packit |
f00812 |
struct hash_entry *sym_entry =
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
table[hashfunct (sym, table_size)];
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
while (sym_entry) {
|
|
Packit |
f00812 |
if (!strcmp (sym, sym_entry->name))
|
|
Packit |
f00812 |
return sym_entry;
|
|
Packit |
f00812 |
sym_entry = sym_entry->next;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
return &empty_entry;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* hashfunct - compute the hash value for "str" and hash size "hash_size" */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
static int hashfunct (const char *str, int hash_size)
|
|
Packit |
f00812 |
{
|
|
Packit |
f00812 |
int hashval;
|
|
Packit |
f00812 |
int locstr;
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
hashval = 0;
|
|
Packit |
f00812 |
locstr = 0;
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
while (str[locstr]) {
|
|
Packit |
f00812 |
hashval = (hashval << 1) + (unsigned char) str[locstr++];
|
|
Packit |
f00812 |
hashval %= hash_size;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
return hashval;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* ndinstal - install a name definition */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
void ndinstal (const char *name, char definition[])
|
|
Packit |
f00812 |
{
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
if (addsym (xstrdup(name),
|
|
Packit |
f00812 |
xstrdup(definition), 0,
|
|
Packit |
f00812 |
ndtbl, NAME_TABLE_HASH_SIZE))
|
|
Packit |
f00812 |
synerr (_("name defined twice"));
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* ndlookup - lookup a name definition
|
|
Packit |
f00812 |
*
|
|
Packit |
f00812 |
* Returns a nil pointer if the name definition does not exist.
|
|
Packit |
f00812 |
*/
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
char *ndlookup (const char *nd)
|
|
Packit |
f00812 |
{
|
|
Packit |
f00812 |
return findsym (nd, ndtbl, NAME_TABLE_HASH_SIZE)->str_val;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* scextend - increase the maximum number of start conditions */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
void scextend (void)
|
|
Packit |
f00812 |
{
|
|
Packit |
f00812 |
current_max_scs += MAX_SCS_INCREMENT;
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
++num_reallocs;
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
scset = reallocate_integer_array (scset, current_max_scs);
|
|
Packit |
f00812 |
scbol = reallocate_integer_array (scbol, current_max_scs);
|
|
Packit |
f00812 |
scxclu = reallocate_integer_array (scxclu, current_max_scs);
|
|
Packit |
f00812 |
sceof = reallocate_integer_array (sceof, current_max_scs);
|
|
Packit |
f00812 |
scname = reallocate_char_ptr_array (scname, current_max_scs);
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* scinstal - make a start condition
|
|
Packit |
f00812 |
*
|
|
Packit |
f00812 |
* NOTE
|
|
Packit |
f00812 |
* The start condition is "exclusive" if xcluflg is true.
|
|
Packit |
f00812 |
*/
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
void scinstal (const char *str, int xcluflg)
|
|
Packit |
f00812 |
{
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
if (++lastsc >= current_max_scs)
|
|
Packit |
f00812 |
scextend ();
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
scname[lastsc] = xstrdup(str);
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
if (addsym(scname[lastsc], NULL, lastsc,
|
|
Packit |
f00812 |
sctbl, START_COND_HASH_SIZE))
|
|
Packit |
f00812 |
format_pinpoint_message (_
|
|
Packit |
f00812 |
("start condition %s declared twice"),
|
|
Packit |
f00812 |
str);
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
scset[lastsc] = mkstate (SYM_EPSILON);
|
|
Packit |
f00812 |
scbol[lastsc] = mkstate (SYM_EPSILON);
|
|
Packit |
f00812 |
scxclu[lastsc] = xcluflg;
|
|
Packit |
f00812 |
sceof[lastsc] = false;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* sclookup - lookup the number associated with a start condition
|
|
Packit |
f00812 |
*
|
|
Packit |
f00812 |
* Returns 0 if no such start condition.
|
|
Packit |
f00812 |
*/
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
int sclookup (const char *str)
|
|
Packit |
f00812 |
{
|
|
Packit |
f00812 |
return findsym (str, sctbl, START_COND_HASH_SIZE)->int_val;
|
|
Packit |
f00812 |
}
|