22-05-2013, 12:56 PM
The Symbol Table
The Symbol.ppt (Size: 139.5 KB / Downloads: 185)
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
efficient storage and access important
in practice (but we won’t worry about efficiency - we’ll just use a liked list)
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
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)
Scope
The scope of any particular variable may be global,
or within a specific code file, or in a file after its
declaration, or within specific code blocks.
In C, blocks are files, function declarations and compound statements (between "{" and "}"). Also, structures and unions can be considered to be blocks.
Scope and the Symbol Table
lookup must must find the most recently inserted declaration; i.e. search for a declaration of the identifier valid in the current scope.
insert must not overwrite previous
declarations, but make them inaccessible.
delete should hide the most recent the most recent declaration and uncover the previous one.
Syntax trees and scope
Many compilers simply build a syntax tree on the
first pass (while carrying out lexical and syntax
analysis).
They then make a second pass, constructing the
symbol table, checking data types, etc.
The Symbol.ppt (Size: 139.5 KB / Downloads: 185)
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
efficient storage and access important
in practice (but we won’t worry about efficiency - we’ll just use a liked list)
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
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)
Scope
The scope of any particular variable may be global,
or within a specific code file, or in a file after its
declaration, or within specific code blocks.
In C, blocks are files, function declarations and compound statements (between "{" and "}"). Also, structures and unions can be considered to be blocks.
Scope and the Symbol Table
lookup must must find the most recently inserted declaration; i.e. search for a declaration of the identifier valid in the current scope.
insert must not overwrite previous
declarations, but make them inaccessible.
delete should hide the most recent the most recent declaration and uncover the previous one.
Syntax trees and scope
Many compilers simply build a syntax tree on the
first pass (while carrying out lexical and syntax
analysis).
They then make a second pass, constructing the
symbol table, checking data types, etc.