SlideShare a Scribd company logo
Symbol Table
Symbol Table
• When names are found, they will be entered into a symbol
table, which will hold all relevant information about
identifiers, function names, objects, classes, interfaces, etc.
• This information will be used later by the semantic analyzer
and the code generator.
Lexical
Analyzer
Semantic
Analyzer
Code
Generator
Symbol
Table
Syntax
Analyzer
Usage of Symbol table by Various Phases of Compiler
• Lexical Analysis: Creates new entries in the table about token.
• Syntax Analysis: Adds information regarding attribute type, scope,
dimension, line of reference, etc in the table.
• Semantic Analysis: Uses available information in the table to check for
semantics i.e. to verify that expressions and assignments are semantically
correct(type checking) and update it accordingly.
• Intermediate Code generation: Refers symbol table for knowing how much
memory and what type is allocated and table helps in adding temporary
variable information.
• Code Optimization: Uses information present in symbol table for machine
dependent optimization.
• Target Code generation: Generates code by using address information of
identifier present in the table.
Symbol table
• Symbol table: A data structure used by a compiler to keep track of
semantics of names.
– Determine whether the variable is defined already or not.
– Determine the scope.
• The effective context where a name is valid.
– Where it is stored: storage address.
– Type checking for semantic correctness determination.
• Operations:
– Find / Lookup /Search: Access the information associated with given
name.
– Insert: add a name into the table.
– Delete: remove a name when its scope is closed.
5
Symbol Table
• Compiler uses symbol table to keep track of scope (block) and binding information
about names
• symbol table is changed (updated) every time
– if a new name is discovered
– if new information about an existing name is discovered
• Symbol table must have mechanism to:
– add new entries
– find existing information efficiently
• Two common mechanism:
– linear lists, simple to implement, poor performance
– hash tables, greater programming, good performance
• Compiler should be able to grow symbol table dynamically
• If size is fixed, it must be large enough for the largest program
Symbol table Information
Symbol table stores:
• For each type name, its type definition (eg. for the C type
declaration typedef int* mytype, it maps the name mytype to a data
structure that represents the type int*).
• 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
• Variable Name:
– Must be present to let other phases know which is a particular variable
– Major issue is variability of length of name
– Two popular approaches
• To set a fixed maximum length for variable name
• To keep only a descriptor in the variable name field and keep the
name in general string area referenced by this descriptor
• First approach gives quick table access while the other supports efficient
storage of variable names
• First approach is inefficient in short named variables while second has slow
table access due to referencing
Symbol table in compiler Design
Symbol Table Organization for Block Structured
Languages
Symbol Table Organization for Non
Block Structured Languages
Four Structures of Non Block Structured Languages
• Unordered list: (linked list/array)
– for a very small set of variables;
– coding is easy, but performance is bad for large number of variables.
• Ordered linear list:
– use binary search on arrays;
– insertion and deletion are expensive;
– coding is relatively easy.
• Binary search tree:
– O(log n) time per operation (search, insert or delete) for n variables;
– coding is relatively difficult.
• Hash table:
– most commonly used;
– very efficient provided the memory space is adequately larger than the
number of variables;
– performance maybe bad if unlucky or the table is saturated;
– coding is not too difficult.
Symbol table in compiler Design
Symbol table in compiler Design
Hash Table Efficiency
• For a given hash table capacity,
– If there are too many buckets, then many buckets will
not be used, leading to space inefficiency.
– If there are too few buckets, then there will be many
clashes, causing the searches to degenerate into
predominately sequential searches, leading to time
inefficiency.
Symbol table in compiler Design
Symbol table in compiler Design
Symbol table in compiler Design
Symbol tables in block-structured languages
ď‚· Symbol tables in block-structured languages:
– 1. many small symbol tables
– 2. one global symbol table
1. many small tables (Stack based Implementation)
– one symbol table per scope.
– use a stack of tables.
– The symbol table for the current scope is on top of the stack.
– The symbol tables for other enclosing scopes are placed under the current
one.
– Push a new table when a new scope is entered.
– Pop a symbol table when a scope is closed.
Example
Multiple symbol tables in one stack
H:int
A:int
L:int
x:real
y:real
:
symbol table
stack
{
int H,A,L;
{
real x,y;
:
:
}
{
char A,C,M;
print(M);
H + A ..... ;
X + L ...... ;
}
}
symbol table
Example
Multiple symbol tables in one stack
H:int
A:int
L:int
symbol table
stack
{
int H,A,L;
{
real x,y;
:
:
}
{
char A,C,M;
print(M);
H + A ..... ;
X + L ...... ;
}
} Second scope is
completed. So Pop
(remove) the symbol
table of x,y
Example
H:int
A:int
L:int
A:char
C:char
M:char
:
symbol table
stack
{
int H,A,L;
{
real x,y;
:
:
}
{
char A,C,M;
print(M);
H + A ..... ;
X + L ...... ;
}
}
This symbol table in inserted
after popping a symbol table
consisting of x,y
symbol table
many small tables
• To search for a name, we check the symbol tables on the stack from top to
bottom.
• We may need to search multiple tables.
E.g. A global name is defined in the bottom-most symbol table.
• Space may be wasted if a fixed-sized hash table is used to
implement symbol tables.
- hash table too big – waste the memory space
- hash table too small -- collisions
2. one global table
One Symbol Table with chaining
• All names are in the single global table.
• What about the same name is declared several times?
• Each name is given a scope number.
• <name, scope number> should be unique in the table.
• Easy to search a name.
• New names are placed at the front of lists.
• To close a scope, we need to remove all entries defined in that
scope.
Example
Hash based Chaining
{
int H,A,L;
{
float x,y,H;
:
:
}
{
char A,C,M;
}
}
}
Scope
number
1
2
3
Scope
number
1
2
3
One Symbol Table with chaining
• Binary search tree with chaining.
• Use a doubly linked list to chain all entries with the same name.
Symbol table in compiler Design
Reference
• A.V. Aho, M.S. Lam, R. Sethi, J. D. Ullman,
Compilers Principles, Techniques and Tools,
Pearson Edition, 2013.
P. Kuppusamy - Lexical Analyzer
Ad

More Related Content

What's hot (20)

Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
Anul Chaudhary
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
Akhil Kaushik
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
Siva Sathya
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design
LogsAk
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
Radhakrishnan Chinnusamy
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
Dattatray Gandhmal
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
United International University
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler design
Swati Chauhan
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
Shine Raj
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and Lexeme
A. S. M. Shafi
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
Dattatray Gandhmal
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
Rajkumar R
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
Jyothishmathi Institute of Technology and Science Karimnagar
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
Kuppusamy P
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
Kalaimathi Vijayakumar
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
Dr. C.V. Suresh Babu
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
Akhil Kaushik
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
Huawei Technologies
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
Anul Chaudhary
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
Akhil Kaushik
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
Siva Sathya
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design
LogsAk
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler design
Swati Chauhan
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
Shine Raj
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and Lexeme
A. S. M. Shafi
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
Rajkumar R
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
Kuppusamy P
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
Dr. C.V. Suresh Babu
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
Akhil Kaushik
 

Similar to Symbol table in compiler Design (20)

Symbol Table
Symbol TableSymbol Table
Symbol Table
Akhil Kaushik
 
Symbol Table.pptx
Symbol Table.pptxSymbol Table.pptx
Symbol Table.pptx
LaibaFaisal3
 
Symbol-Table concept in compiler design pdf for reference
Symbol-Table concept in compiler design pdf for referenceSymbol-Table concept in compiler design pdf for reference
Symbol-Table concept in compiler design pdf for reference
shalini s
 
BCS304 Module 5 slides DSA notes 3rd sem
BCS304 Module 5 slides DSA notes 3rd semBCS304 Module 5 slides DSA notes 3rd sem
BCS304 Module 5 slides DSA notes 3rd sem
ticonah393
 
Introduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICSIntroduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICS
HaritikaChhatwal1
 
Indexing
IndexingIndexing
Indexing
Davood Barfeh
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
Haitham El-Ghareeb
 
R programming by ganesh kavhar
R programming by ganesh kavharR programming by ganesh kavhar
R programming by ganesh kavhar
Savitribai Phule Pune University
 
Advanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.pptAdvanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.ppt
Anshika865276
 
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python (3).pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python (3).pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python (3).pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python (3).pptx
smartashammari
 
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Ogunsina1
 
Declarations
DeclarationsDeclarations
Declarations
sangeetha r
 
Symbol table Management Compiler Design.pptx
Symbol table Management Compiler Design.pptxSymbol table Management Compiler Design.pptx
Symbol table Management Compiler Design.pptx
Call me xyz
 
Symbol table Management Compiler Design.pptx
Symbol table Management Compiler Design.pptxSymbol table Management Compiler Design.pptx
Symbol table Management Compiler Design.pptx
Call me xyz
 
R-programming with example representation.ppt
R-programming with example representation.pptR-programming with example representation.ppt
R-programming with example representation.ppt
geethar79
 
R Programming for Statistical Applications
R Programming for Statistical ApplicationsR Programming for Statistical Applications
R Programming for Statistical Applications
drputtanr
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt
Ashok280385
 
ds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.pptds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.ppt
AlliVinay1
 
Introduction to R programming Language.pptx
Introduction to R programming Language.pptxIntroduction to R programming Language.pptx
Introduction to R programming Language.pptx
kemetex
 
2017 biological databasespart2
2017 biological databasespart22017 biological databasespart2
2017 biological databasespart2
Prof. Wim Van Criekinge
 
Symbol Table.pptx
Symbol Table.pptxSymbol Table.pptx
Symbol Table.pptx
LaibaFaisal3
 
Symbol-Table concept in compiler design pdf for reference
Symbol-Table concept in compiler design pdf for referenceSymbol-Table concept in compiler design pdf for reference
Symbol-Table concept in compiler design pdf for reference
shalini s
 
BCS304 Module 5 slides DSA notes 3rd sem
BCS304 Module 5 slides DSA notes 3rd semBCS304 Module 5 slides DSA notes 3rd sem
BCS304 Module 5 slides DSA notes 3rd sem
ticonah393
 
Introduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICSIntroduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICS
HaritikaChhatwal1
 
Advanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.pptAdvanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.ppt
Anshika865276
 
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python (3).pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python (3).pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python (3).pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python (3).pptx
smartashammari
 
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Ogunsina1
 
Declarations
DeclarationsDeclarations
Declarations
sangeetha r
 
Symbol table Management Compiler Design.pptx
Symbol table Management Compiler Design.pptxSymbol table Management Compiler Design.pptx
Symbol table Management Compiler Design.pptx
Call me xyz
 
Symbol table Management Compiler Design.pptx
Symbol table Management Compiler Design.pptxSymbol table Management Compiler Design.pptx
Symbol table Management Compiler Design.pptx
Call me xyz
 
R-programming with example representation.ppt
R-programming with example representation.pptR-programming with example representation.ppt
R-programming with example representation.ppt
geethar79
 
R Programming for Statistical Applications
R Programming for Statistical ApplicationsR Programming for Statistical Applications
R Programming for Statistical Applications
drputtanr
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt
Ashok280385
 
ds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.pptds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.ppt
AlliVinay1
 
Introduction to R programming Language.pptx
Introduction to R programming Language.pptxIntroduction to R programming Language.pptx
Introduction to R programming Language.pptx
kemetex
 
Ad

More from Kuppusamy P (20)

Recurrent neural networks rnn
Recurrent neural networks   rnnRecurrent neural networks   rnn
Recurrent neural networks rnn
Kuppusamy P
 
Deep learning
Deep learningDeep learning
Deep learning
Kuppusamy P
 
Image segmentation
Image segmentationImage segmentation
Image segmentation
Kuppusamy P
 
Image enhancement
Image enhancementImage enhancement
Image enhancement
Kuppusamy P
 
Feature detection and matching
Feature detection and matchingFeature detection and matching
Feature detection and matching
Kuppusamy P
 
Image processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filtersImage processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filters
Kuppusamy P
 
Flowchart design for algorithms
Flowchart design for algorithmsFlowchart design for algorithms
Flowchart design for algorithms
Kuppusamy P
 
Algorithm basics
Algorithm basicsAlgorithm basics
Algorithm basics
Kuppusamy P
 
Problem solving using Programming
Problem solving using ProgrammingProblem solving using Programming
Problem solving using Programming
Kuppusamy P
 
Parts of Computer, Hardware and Software
Parts of Computer, Hardware and Software Parts of Computer, Hardware and Software
Parts of Computer, Hardware and Software
Kuppusamy P
 
Strings in java
Strings in javaStrings in java
Strings in java
Kuppusamy P
 
Java methods or Subroutines or Functions
Java methods or Subroutines or FunctionsJava methods or Subroutines or Functions
Java methods or Subroutines or Functions
Kuppusamy P
 
Java arrays
Java arraysJava arrays
Java arrays
Kuppusamy P
 
Java iterative statements
Java iterative statementsJava iterative statements
Java iterative statements
Kuppusamy P
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
Kuppusamy P
 
Java data types
Java data typesJava data types
Java data types
Kuppusamy P
 
Java introduction
Java introductionJava introduction
Java introduction
Kuppusamy P
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine Learning
Kuppusamy P
 
Anomaly detection (Unsupervised Learning) in Machine Learning
Anomaly detection (Unsupervised Learning) in Machine LearningAnomaly detection (Unsupervised Learning) in Machine Learning
Anomaly detection (Unsupervised Learning) in Machine Learning
Kuppusamy P
 
Machine Learning Performance metrics for classification
Machine Learning Performance metrics for classificationMachine Learning Performance metrics for classification
Machine Learning Performance metrics for classification
Kuppusamy P
 
Recurrent neural networks rnn
Recurrent neural networks   rnnRecurrent neural networks   rnn
Recurrent neural networks rnn
Kuppusamy P
 
Deep learning
Deep learningDeep learning
Deep learning
Kuppusamy P
 
Image segmentation
Image segmentationImage segmentation
Image segmentation
Kuppusamy P
 
Image enhancement
Image enhancementImage enhancement
Image enhancement
Kuppusamy P
 
Feature detection and matching
Feature detection and matchingFeature detection and matching
Feature detection and matching
Kuppusamy P
 
Image processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filtersImage processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filters
Kuppusamy P
 
Flowchart design for algorithms
Flowchart design for algorithmsFlowchart design for algorithms
Flowchart design for algorithms
Kuppusamy P
 
Algorithm basics
Algorithm basicsAlgorithm basics
Algorithm basics
Kuppusamy P
 
Problem solving using Programming
Problem solving using ProgrammingProblem solving using Programming
Problem solving using Programming
Kuppusamy P
 
Parts of Computer, Hardware and Software
Parts of Computer, Hardware and Software Parts of Computer, Hardware and Software
Parts of Computer, Hardware and Software
Kuppusamy P
 
Strings in java
Strings in javaStrings in java
Strings in java
Kuppusamy P
 
Java methods or Subroutines or Functions
Java methods or Subroutines or FunctionsJava methods or Subroutines or Functions
Java methods or Subroutines or Functions
Kuppusamy P
 
Java arrays
Java arraysJava arrays
Java arrays
Kuppusamy P
 
Java iterative statements
Java iterative statementsJava iterative statements
Java iterative statements
Kuppusamy P
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
Kuppusamy P
 
Java data types
Java data typesJava data types
Java data types
Kuppusamy P
 
Java introduction
Java introductionJava introduction
Java introduction
Kuppusamy P
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine Learning
Kuppusamy P
 
Anomaly detection (Unsupervised Learning) in Machine Learning
Anomaly detection (Unsupervised Learning) in Machine LearningAnomaly detection (Unsupervised Learning) in Machine Learning
Anomaly detection (Unsupervised Learning) in Machine Learning
Kuppusamy P
 
Machine Learning Performance metrics for classification
Machine Learning Performance metrics for classificationMachine Learning Performance metrics for classification
Machine Learning Performance metrics for classification
Kuppusamy P
 
Ad

Recently uploaded (20)

Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 

Symbol table in compiler Design

  • 2. Symbol Table • When names are found, they will be entered into a symbol table, which will hold all relevant information about identifiers, function names, objects, classes, interfaces, etc. • This information will be used later by the semantic analyzer and the code generator. Lexical Analyzer Semantic Analyzer Code Generator Symbol Table Syntax Analyzer
  • 3. Usage of Symbol table by Various Phases of Compiler • Lexical Analysis: Creates new entries in the table about token. • Syntax Analysis: Adds information regarding attribute type, scope, dimension, line of reference, etc in the table. • Semantic Analysis: Uses available information in the table to check for semantics i.e. to verify that expressions and assignments are semantically correct(type checking) and update it accordingly. • Intermediate Code generation: Refers symbol table for knowing how much memory and what type is allocated and table helps in adding temporary variable information. • Code Optimization: Uses information present in symbol table for machine dependent optimization. • Target Code generation: Generates code by using address information of identifier present in the table.
  • 4. Symbol table • Symbol table: A data structure used by a compiler to keep track of semantics of names. – Determine whether the variable is defined already or not. – Determine the scope. • The effective context where a name is valid. – Where it is stored: storage address. – Type checking for semantic correctness determination. • Operations: – Find / Lookup /Search: Access the information associated with given name. – Insert: add a name into the table. – Delete: remove a name when its scope is closed.
  • 5. 5 Symbol Table • Compiler uses symbol table to keep track of scope (block) and binding information about names • symbol table is changed (updated) every time – if a new name is discovered – if new information about an existing name is discovered • Symbol table must have mechanism to: – add new entries – find existing information efficiently • Two common mechanism: – linear lists, simple to implement, poor performance – hash tables, greater programming, good performance • Compiler should be able to grow symbol table dynamically • If size is fixed, it must be large enough for the largest program
  • 6. Symbol table Information Symbol table stores: • For each type name, its type definition (eg. for the C type declaration typedef int* mytype, it maps the name mytype to a data structure that represents the type int*). • 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.
  • 8. • Variable Name: – Must be present to let other phases know which is a particular variable – Major issue is variability of length of name – Two popular approaches • To set a fixed maximum length for variable name • To keep only a descriptor in the variable name field and keep the name in general string area referenced by this descriptor • First approach gives quick table access while the other supports efficient storage of variable names • First approach is inefficient in short named variables while second has slow table access due to referencing
  • 10. Symbol Table Organization for Block Structured Languages
  • 11. Symbol Table Organization for Non Block Structured Languages
  • 12. Four Structures of Non Block Structured Languages • Unordered list: (linked list/array) – for a very small set of variables; – coding is easy, but performance is bad for large number of variables. • Ordered linear list: – use binary search on arrays; – insertion and deletion are expensive; – coding is relatively easy. • Binary search tree: – O(log n) time per operation (search, insert or delete) for n variables; – coding is relatively difficult. • Hash table: – most commonly used; – very efficient provided the memory space is adequately larger than the number of variables; – performance maybe bad if unlucky or the table is saturated; – coding is not too difficult.
  • 15. Hash Table Efficiency • For a given hash table capacity, – If there are too many buckets, then many buckets will not be used, leading to space inefficiency. – If there are too few buckets, then there will be many clashes, causing the searches to degenerate into predominately sequential searches, leading to time inefficiency.
  • 19. Symbol tables in block-structured languages ď‚· Symbol tables in block-structured languages: – 1. many small symbol tables – 2. one global symbol table 1. many small tables (Stack based Implementation) – one symbol table per scope. – use a stack of tables. – The symbol table for the current scope is on top of the stack. – The symbol tables for other enclosing scopes are placed under the current one. – Push a new table when a new scope is entered. – Pop a symbol table when a scope is closed.
  • 20. Example Multiple symbol tables in one stack H:int A:int L:int x:real y:real : symbol table stack { int H,A,L; { real x,y; : : } { char A,C,M; print(M); H + A ..... ; X + L ...... ; } } symbol table
  • 21. Example Multiple symbol tables in one stack H:int A:int L:int symbol table stack { int H,A,L; { real x,y; : : } { char A,C,M; print(M); H + A ..... ; X + L ...... ; } } Second scope is completed. So Pop (remove) the symbol table of x,y
  • 22. Example H:int A:int L:int A:char C:char M:char : symbol table stack { int H,A,L; { real x,y; : : } { char A,C,M; print(M); H + A ..... ; X + L ...... ; } } This symbol table in inserted after popping a symbol table consisting of x,y symbol table
  • 23. many small tables • To search for a name, we check the symbol tables on the stack from top to bottom. • We may need to search multiple tables. E.g. A global name is defined in the bottom-most symbol table. • Space may be wasted if a fixed-sized hash table is used to implement symbol tables. - hash table too big – waste the memory space - hash table too small -- collisions
  • 24. 2. one global table One Symbol Table with chaining • All names are in the single global table. • What about the same name is declared several times? • Each name is given a scope number. • <name, scope number> should be unique in the table. • Easy to search a name. • New names are placed at the front of lists. • To close a scope, we need to remove all entries defined in that scope.
  • 25. Example Hash based Chaining { int H,A,L; { float x,y,H; : : } { char A,C,M; } } } Scope number 1 2 3 Scope number 1 2 3
  • 26. One Symbol Table with chaining • Binary search tree with chaining. • Use a doubly linked list to chain all entries with the same name.
  • 28. Reference • A.V. Aho, M.S. Lam, R. Sethi, J. D. Ullman, Compilers Principles, Techniques and Tools, Pearson Edition, 2013. P. Kuppusamy - Lexical Analyzer