Sei sulla pagina 1di 7

7/10/13

Symbol Table Management

Symbol Table Management


last updated 5/11/07

Screen Mode

Central part of compiler


The symbol table is accessed by most phases of a compiler, beginning with the lexical analysis to optimization. The symbol table carries the collected information about each named object in the program to other phases of the compiler. As soon as a named token is found, depending on the token type, a call to the symbol table routines must be made.

Tokens:
Keywords of the language, special characters/punctuation, user defined identifiers used for different meanings, constants

Identifiers:
Identifiers are user defined variables, memory location labels parameter names names of subroutines (functions or procedures) which are associated with entry points of the routines' code array or structure names field names; file names; enumerated type values constant names; type names Need a global structure to store identifiers and their associated meanings (attributes) for future reference. This structure provides more information that the grammar and syntax directed translation of the language would not normally be able to carry.

Reserved words
These may be stored here too. You have to look up an identifier and reserved words have the same syntax as an identifier.

jcsites.juniata.edu/faculty/rhodes/lt/sytbmgmt.htm

1/7

7/10/13

Symbol Table Management

Scoping rules
The symbol table must be able to handle the scoping rules of the language.

Symbol Table Requirements


fast lookup. The parser of a compiler is linear in speed. The table lookup needs to be as fast as possible. flexible in structure. The entries must contain all necessary information, depending on usage of identifier efficient use of space. This will require runtime allocation (dynamic) of the space. handle characteristics of language (e.g., scoping, implicit declaration) Scoping requires handling entry of a local block and exit. Block exit requires removal or hiding of the entries of the block.

Program components
Declarative statements- define identifiers and their attributes Imperative statements - uses identifiers assuming their attributes Languages and their explicit declarative statements Pascal, Ada C, C++, Java, PL/I FORTRAN COBOL VAR statement statements beginning with a type TYPE, DIMENSION statement DATA DIVISION
2/7

jcsites.juniata.edu/faculty/rhodes/lt/sytbmgmt.htm

7/10/13

Symbol Table Management

Implicit Declarations
Some languages have implicit declarations FORTRAN or PL/I SUM = SUM + I (first encounter assumes declaration with default attributes) APL, BASIC, LISP (most interpreted languages)

Actions on declarations
Declarative statements generally do not translate to (or directly associated with) any executable code. They may allocate space at designated times, however. Symbols can be monitored, especially in languages that require declaration statements SUBSEQUENT FIRST ENCOUNTER CONCLUSION ENCOUNTER Declaration Declaration Declaration Reference (in imperative statement) Reference (in imperative stmt) Declaration None NA Continue--this is the expected case Multiple declarations error unless appropriate within scoping rules Unused warning message Undefined error

String Management
How to efficiently store names of identifiers
jcsites.juniata.edu/faculty/rhodes/lt/sytbmgmt.htm 3/7

7/10/13

Symbol Table Management

1. Restrict length of identifier


FORTRAN 77 and earlier limited variables to 1-6 characters (4 bytes packed encoding) Pascal had 1-10 characters in initial design Advantages: structure consists of simple fixed sized string array; easy lookup Disadvantages: limits programmer to choose effective names; may waste space

2. Separate String Space


Advantages: allows unlimited identifier names; doesn't waste space Disadvantages: extra memory reference for access

3. Dynamic allocation of strings


Advantages: don't need predefined space for strings Disadvantages: complexity Extend to other parts of the symbol table

Name Searching
Functions:
- enter new name into table Declaration of name - return error if already there - add new attributes as found Use - expect name to be found - return position of entry in table - delete entries

Entry of new scope - allow new declarations or redefined declarations Exit of scope

Goals:
jcsites.juniata.edu/faculty/rhodes/lt/sytbmgmt.htm 4/7

7/10/13

Symbol Table Management

1. efficiency of declaration entry, insertion of new name 2. efficiency of retrieval, lookup 3. sorted list of symbol table dump Most important would be #2

Approaches:
n is the number of entries in the symbol table for the analysis below n' is the number of entries in the current [local] block

1. Linear access
Assuming error free source file Time(declaration) = k or O(1) [k is some constant] Time (reference) kn/2 or O(n) To check for errors Time(declaration) ~= kn'/2 Time(reference) ~= kn/2 Time(block entry) = O(1) Time(block exit) = O(n') or O(1) if display used Time(sort) = O(n log n)

2. Binary search access


data structure same as linear table as new name is entered, insert into block to maintain sorted order on lookup use binary search Time(declaration) = O(n') Time(ref) = O(log n') Time(sort) = 0 (already in order) Time(entry) = O(1) Time(exit) = O(1)

jcsites.juniata.edu/faculty/rhodes/lt/sytbmgmt.htm

5/7

7/10/13

Symbol Table Management

3. Tree access
- assumes a more random encounter of names BACPCDBQCDE - entries of symbol table are organized into tree structure - have forest of trees - each tree constitutes a block Average Time(decl) = O(log n) Average Time (ref) = O(log n) Worst case Time(decl) = T(ref) = O(n) Time(sort) = 0 Time(entry) = O(1) Time(exit) = O(n')

4. Hash tables
use a hash function on name to find location in symbol table symbol table is organized like a table of stacks since some identifiers in different block will collide Time(declaration) = O(1) or O(n') Time(reference) = O(b) where b is the chain depth Time(entry) = O(1) Time(exit) = O(n) or O(n') if second link connects block's ids

Single pass vs multipass compiler:


single pass can discard the entries while multipass will need to retain temporarily
jcsites.juniata.edu/faculty/rhodes/lt/sytbmgmt.htm 6/7

7/10/13

Symbol Table Management

inaccessible entries

jcsites.juniata.edu/faculty/rhodes/lt/sytbmgmt.htm

7/7

Potrebbero piacerti anche