[SLP-Homepage]    [Source Modules]    [Manual]    [Run]    [Examples]
 

Symbol Table

This module defines a subclass sym_c. The objects of this class correspond to the "atoms" in the program, i.e. the symbols used for predicates and constants. The class itself contains a method find which is a classical symbol table (dictionary): It is called with the name of the symbol as a parameter and either returns the existing sym_c-object with this name or constructs a new object (if there was no symbol with this name before). Thus, the class method find replaces the constructor. One cannot directly construct sym_c-objects (in order to ensure that there is always only one object with a given name). It is currently also not possible to destroy objects of this class (they would have to be deleted in the symbol table).

The symbol table is implemented as a hash table with an "external" linked list of symbols for every hash value.


Class Methods:

init:
static void init(void);
This class method initializes the symbol table to empty. It must be called once before any other method of this class. It should not be called later again since this would be a memory leak. This method also creates the special atom "$answer".
find:
static atom_c* find(str_t name_ptr, str_t name_end);
This method returns an object with the given name. If an object with this name already exists, this existing object is returned. Otherwise a new object is constructed. In this way it is guaranteed that there is always only a single object for one name. The input name is represented in a slightly no-standard way in order to fit better to the output of the lexical scanner: The name extends from name_ptr to the character before name_end (i.e. the name is name_end-name_ptr characters long). If a new object is constructed, the symbol name will be copied. So the string pointed to be name_ptr can be overwritten later (and it usually will, since this is only some part of the input buffer). Note finally that find automatically removes single quotes ("'", see ch_c::is_quote) around the symbol name. However, the quotes determine the result of the object method quoted (see below).
answer:
static atom_c* answer(void);
This function returns the special atom "$answer" used internally for query evaluation.


Object Methods:

name:
str_t name(void);
This method returns the name of a symbol (as a null-terminated string).
quoted:
bool_t quoted(void);
This method returns BOOL_TRUE if the symbol name was quoted when it was first used. As explained in the manual, the symbols "'abc'" and "abc" are treated as identical (and name will return "abc" in both cases). However, for the user it might be important that the object is printed on output as it was used in his or her program. Therefore, we remember whether the symbol was quoted on first use.
id:
int id(void);
This method returns a unique number for each atom. It is used in hash functions.
is_special:
bool_t is_special(void);
This method returns BOOL_TRUE if the symbol name starts with a dollar sign (special atom, used for commands).
print:
void print(void);
This method prints the name of the symbol. If quoted() is true, single quotes are printed around the symbol name.


Implementation:


Stefan Brass (brass@acm.org), April 4, 2002.    [HTML 3.2 Checked]