Vous êtes sur la page 1sur 14

IMPLEMENTATION

What is the Symbol Table symbol table is a data structure used by a language translator such as a compiler or interpreter

Constructing the Symbol Table


There are three main operations to be carried out on the symbol table:

determining whether a string has already been stored


inserting an entry for a string deleting a string when it goes out of scope

Table
name : attributes : Collect attributes when a name is declared. Provide information when a name is used. Two issues:

1. interface: how to use symbol tables


2. implementation: how to implement it.

This requires three functions: lookup(s): returns the index of the entry for string s, or 0 if there is no entry

insert(s,t): add a new entry for string s (of token t), and return its index delete(s): deletes s from the table (or, typically, hides it)

The Symbol Table


used during all phases of compilation maintains information about many source language constructs Incrementally constructed and expanded during the analysis phases used directly in the code generation phases

01 PROGRAM Main 02 GLOBAL a,b 03 PROCEDURE P (PARAMETER x) 04 LOCAL a 05 BEGIN {P} 06 a 07 b 08 x 09 END {P} 10 BEGIN{Main} 11 Call P(a) 12 END {Main}

Info provided by Symbol Table


Given an Identifier which name is it? What information is to be associated with a name? How do we access this information? How do we associate this information with a name?

Symbol Table
Each piece of info associated with a name is called an

attribute. Attributes are language dependent.


Actual Characters of the name

Type
Storage allocation info (number of bytes). Line number where declared Lines where referenced.

Scope.

Symbol Table
A name can represent
Variable Type Constant Parameter Record Record Field Procedure Array Label file

Symbol Table
Different Classes of Symbols have different Attributes
Variable, Type, Constant, parameter, record field.
Type is one of attributes.

Procedure or function.
Number of parameters, parameters themselves, result type.

Array
# of Dimensions, Array bounds.

File
record size, record type

Other attributes
A scope of a variable can be represented by
A number (Scope is just one of attributes) A different symbol table is constructed for different scope.

Object Oriented Languages Have classes like


Method names, class names, object names.
Scoping is VERY important. (Inheritance).

Functional Languages Lisp


Binding Issues

Symbol Table Data structures


Symbol table as list Symbol table as hash table Symbol table as search tree

Thank you

Vous aimerez peut-être aussi