Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views17 pages

Symbol Table

Uploaded by

Spandan Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views17 pages

Symbol Table

Uploaded by

Spandan Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Symbol Tables

Symbol Tables
• store the information about identifiers
Each entry in the symbol table contains
• the name of an identifier
• additional information: its kind, its type, if it is constant
Contents of a symbol Table
• For each type name, its type definition.
• For each variable name, its type. If the variable is an array, it also
stores dimension information. It may also store storage class, offset in
activation record etc.
• For each constant name, its type and value.
• For each function and procedure, its formal parameter list and its
output type. Each formal parameter
• must have name, type, type of passing (by-reference or by-value), etc
Symbol Table Operations
Three operations
• Create - a new empty symbol table with a given parent table
• Insert - a new identifier in a symbol table (or error)
• Look up – search an identifier in a symbol table (or error)
- Cannot build symbol tables during lexical analysis as the hierarchy of
scopes encoded in the syntax
- Build the symbol tables While parsing, using the semantic actions
After the AST is constructed
Handling Scope
Information(Block/procedure)
• There is a hierarchy of scopes in the program
• Use a similar hierarchy of symbol tables
• One symbol table for each scope
• Each symbol table contains the symbols declared in that lexical scope
Handling Scope Information
• compiler maintains two types of symbol tables:
i. a global symbol table which can be accessed by all the
procedures
ii. scope symbol tables that are created for each scope in the
program.
• To determine the scope of a name, symbol tables are arranged in
hierarchical structure as shown in the example next.
Scope Information example
Handling Scope Information
• This symbol table data structure hierarchy is stored in the semantic
analyzer and whenever a name needs to be searched in a symbol
table, it is searched using the following algorithm:
• first a symbol will be searched in the current scope, i.e. current
symbol table.
• if a name is found, then search is completed, else it will be searched
in the parent symbol table until,either the name is found or global
symbol table has been searched for the name
DATA STRUCTURE FOR SYMBOL
TABLE
• Array Implementation
• List Implementation
• Hash Table Implementation
• Search Tree
Array Implementation
• Simple implementation
• One entry per symbol
• Scan the array for lookup, compare name at each entry
Disadvantage:
• table has fixed size
• need to know in advance the number of entries
DATA STRUCTURE example
Create list ,hash table and search tree for the following program
Array Implementation
List Implementation

• Dynamic structure
• One cell per entry in the table
• Can grow dynamically during compilation
Disadvantage:
• inefficient for large symbol tables
• need to scan half the list on average
Hash Table Implementation
• Efficient implementation
• It is an array of lists (buckets)
• Uses a hashing function to map the symbol name to the
corresponding bucket:
hashfunc : string →int
• Good hash function = even distribution in the buckets
Hash Table Implementation
Search Tree
• names are created as child of root node that always follow the
property of binary tree
• For inserting any name it always follow binary search tree insert
algorithm
Binary Search tree Implementation

You might also like