SlideShare a Scribd company logo
1 of 28
Download to read offline
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 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.
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 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.
Reference
• A.V. Aho, M.S. Lam, R. Sethi, J. D. Ullman,
Compilers Principles, Techniques and Tools,
Pearson Edition, 2013.
P. Kuppusamy - Lexical Analyzer

More Related Content

What's hot

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 designRajkumar R
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lexAnusuya123
 
Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9koolkampus
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and LexemeA. S. M. Shafi
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler designSudip Singh
 
Software Cost Estimation Techniques
Software Cost Estimation TechniquesSoftware Cost Estimation Techniques
Software Cost Estimation TechniquesSanthi thi
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction toolsAkhil Kaushik
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMvikas dhakane
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 

What's hot (20)

Code generation
Code generationCode generation
Code generation
 
Code optimization
Code optimizationCode optimization
Code optimization
 
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
 
Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
 
COMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time EnvironmentsCOMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time Environments
 
Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and Lexeme
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Software Cost Estimation Techniques
Software Cost Estimation TechniquesSoftware Cost Estimation Techniques
Software Cost Estimation Techniques
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHM
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
Cocomo model
Cocomo modelCocomo model
Cocomo model
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Symbol Table
Symbol TableSymbol Table
Symbol Table
 

Similar to Symbol table in compiler Design

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 ANALYTICSHaritikaChhatwal1
 
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.pptAnshika865276
 
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.pptAshok280385
 
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.pptAlliVinay1
 
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...HendraPurnama31
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesAndrew Ferlitsch
 
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.pptxkalai75
 
Ts project Hash based inventory system
Ts project Hash based inventory systemTs project Hash based inventory system
Ts project Hash based inventory systemDADITIRUMALATARUN
 
Data Analytics with R and SQL Server
Data Analytics with R and SQL ServerData Analytics with R and SQL Server
Data Analytics with R and SQL ServerStéphane Fréchette
 

Similar to Symbol table in compiler Design (20)

Symbol Table.pptx
Symbol Table.pptxSymbol Table.pptx
Symbol Table.pptx
 
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
 
Indexing
IndexingIndexing
Indexing
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
R programming by ganesh kavhar
R programming by ganesh kavharR programming by ganesh kavhar
R programming by ganesh kavhar
 
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
 
Declarations
DeclarationsDeclarations
Declarations
 
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
 
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
 
2017 biological databasespart2
2017 biological databasespart22017 biological databasespart2
2017 biological databasespart2
 
Data structure
Data structureData structure
Data structure
 
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
 
SAP ABAP data dictionary
SAP ABAP data dictionarySAP ABAP data dictionary
SAP ABAP data dictionary
 
9780324782011_PPT_ch09.ppt
9780324782011_PPT_ch09.ppt9780324782011_PPT_ch09.ppt
9780324782011_PPT_ch09.ppt
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
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
 
R Datatypes
R DatatypesR Datatypes
R Datatypes
 
R Datatypes
R DatatypesR Datatypes
R Datatypes
 
Ts project Hash based inventory system
Ts project Hash based inventory systemTs project Hash based inventory system
Ts project Hash based inventory system
 
Data Analytics with R and SQL Server
Data Analytics with R and SQL ServerData Analytics with R and SQL Server
Data Analytics with R and SQL Server
 

More from Kuppusamy P

Recurrent neural networks rnn
Recurrent neural networks   rnnRecurrent neural networks   rnn
Recurrent neural networks rnnKuppusamy P
 
Image segmentation
Image segmentationImage segmentation
Image segmentationKuppusamy P
 
Image enhancement
Image enhancementImage enhancement
Image enhancementKuppusamy P
 
Feature detection and matching
Feature detection and matchingFeature detection and matching
Feature detection and matchingKuppusamy P
 
Image processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filtersImage processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filtersKuppusamy P
 
Flowchart design for algorithms
Flowchart design for algorithmsFlowchart design for algorithms
Flowchart design for algorithmsKuppusamy P
 
Algorithm basics
Algorithm basicsAlgorithm basics
Algorithm basicsKuppusamy P
 
Problem solving using Programming
Problem solving using ProgrammingProblem solving using Programming
Problem solving using ProgrammingKuppusamy 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
 
Java methods or Subroutines or Functions
Java methods or Subroutines or FunctionsJava methods or Subroutines or Functions
Java methods or Subroutines or FunctionsKuppusamy P
 
Java iterative statements
Java iterative statementsJava iterative statements
Java iterative statementsKuppusamy P
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statementsKuppusamy P
 
Java introduction
Java introductionJava introduction
Java introductionKuppusamy P
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine LearningKuppusamy 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 LearningKuppusamy P
 
Machine Learning Performance metrics for classification
Machine Learning Performance metrics for classificationMachine Learning Performance metrics for classification
Machine Learning Performance metrics for classificationKuppusamy P
 

More from Kuppusamy P (20)

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

Recently uploaded

latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answersdalebeck957
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 

Recently uploaded (20)

latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 

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
  • 9.
  • 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.
  • 13.
  • 14.
  • 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.
  • 16.
  • 17.
  • 18.
  • 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.
  • 27.
  • 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