SlideShare uma empresa Scribd logo
1 de 28
Baixar para ler 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

Mais conteúdo relacionado

Mais procurados

Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignmentKarthi Keyan
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design) Tasif Tanzim
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysisRicha Sharma
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code GeneratorDarshan sai Reddy
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler designSudip Singh
 
Compiler Design
Compiler DesignCompiler Design
Compiler DesignMir Majid
 
Relationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & PatternRelationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & PatternBharat Rathore
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compilerIffat Anjum
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTanzeela_Hussain
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTUREArchie Jamwal
 

Mais procurados (20)

Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code Generator
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Relationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & PatternRelationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & Pattern
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
stack & queue
stack & queuestack & queue
stack & queue
 
Analysis of the source program
Analysis of the source programAnalysis of the source program
Analysis of the source program
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 

Semelhante a 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
 

Semelhante a 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
 

Mais de 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
 

Mais de 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
 

Último

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 

Último (20)

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 

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