SlideShare a Scribd company logo
Symbol Table
Management
04/11/19 swati chauhan (KIET)
Symbol Table
 Symbol table is data structure created and maintained by
compilers to store information about the occurrence of various
entities such as variable names, function names, objects,
classes, interfaces, etc.
 Symbol table is used by both the analysis and the synthesis parts
of a compiler.
04/11/19 swati chauhan (KIET)
Symbol Table
 When identifiers are found, they will be entered into a symbol table,
which will hold all relevant information about identifiers and other
symbols, variables, constants, procedures statements e.t.c,
 This information about the name:-
 Type
 Its Form , Its Location
It will be used later by the semantic analyzer and the code generator.
Lexical
Analyzer
Semantic
Analyzer
Code
Generator
Symbol
Table
Syntax
Analyzer
04/11/19
swati chauhan (KIET)
 Efficient to add new entries to the S.T
 Dynamic in nature
Issues in Symbol Table:
 Format of entries
 Method of Access
 Place where they are stored
04/11/19 swati chauhan (KIET)
Contents of Symbol Table
Name Information
Capabilities of S.T :
1)Checking (Determine whether the given information is in
the table)
2)Adding New Information
3)Access the information of Name
4)Deletion
04/11/19 swati chauhan (KIET)
Symbol Table Entries
 We will store the following information about
Name and each entry in the symbol table is
associated with attributes that support the
compiler in different phases:
 The name (as a string).
 Size and Dimension
 The data type.
 Its scope (global, local, or parameter).
 Its offset from the base pointer (for local variables and
parameters only).
04/11/19 swati chauhan (KIET)
Implementation
 Use linear Array of records ,one record per name.
 Entries of S.T are not uniform so to make it uniform some
information is kept outside the table and pointer to this information
stored in S.T.
 Record (consist known no. of consecutive words of memory,, so
names stored in record)
It is appropriate if upper bound on the length of identifier is
given…..
04/11/19 swati chauhan (KIET)
Data Structure for S.T
 Required to make n-entries and m-inquiries
1) Lists:
Name 1 Info 1 Name 2 Info 2 ……...... ……….. ……….. ………..
It is easy to implement
Addition is easy
Retrieve Information
ADVANTAGES: Minimum space is required
Addition in table is easy
DISADVANTAGE: Higher Access time
Available
2)Binary Search Tree:
 Efficient approach to organize S.T with two field :::
 Left and Right
 Algorithm for searching name in B.S.T
P= initially a pointer to root
1) If Name = Name (P) then Return /* success */
2) Else if Name < Name (P) then
P:= left(P) /* visit left child */
3) Else Name (P) < Name then
P:= Right (P) /* visit right child */
Addition
Firstly search, if doesn’t exist then create new node at proper
position.
swati chauhan (KIET)
3)Hash Table
 Consists K words [0….k-1]
 Pointers into the storage table (linked list)
 Searching Name in S.T
 Apply hash function to name
h(Name) -> {0…..k-1 (integer) }
Addition new Name
Create a record at available space in storage table and link that record
to h(Name)th list.
Hashing > BST > Linear List
04/11/19 swati chauhan (KIET)
Representing Scope information in S.T
 Scope: The region of a program where a binding
is active
 The same name in a different scope can have a different binding
 Rules for governing scope :
 1) If name declared within a block B then valid only within B
 2) If B1()
 {…………..<-
 B2()
 {…….}
 }
04/11/19 swati chauhan (KIET)
 Require complicated S.T organization
 So use Multiple symbol tables, one for each block
 Each Table has : Name and Information
 If New block entered
Then Push an empty table into the stack for storing names
and information.
Ex:- Program main
Var x,y : integer ;
Procedure P:
Var x,a : boolean ;
Procedure Q:
Var x,y,z : real;
Begin …… end
Begin ……end
Begin ……end
04/11/19 swati chauhan (KIET)
Symbol Table organization that compiles
with static scope information rules
 Next technique to represent scope information in S.T:
1) Write nesting depth of each procedure block
2) Use pair (Procedure name, nesting depth) to access the
information from the table
04/11/19 swati chauhan (KIET)
Error Detection & Recovery
 programmers make mistakes
 Error
Compile Time Run Time
Lexical Phase
Error
Syntactic Phase
Error
Semantic Error
swati chauhan (KIET)
• Overflow {Indicates that the magnitude
of a computational result is too large to represent.}
Underflow {ndicates that the magnitude
of a computational result is too close to zero to
represent.}
• invalid subscript {}
• An integer division by zero
Sources of Error
 Algorithmic Error
 Coding Error
 A program may exceed a compiler or machine limit
Ex:- Array declaration with too many dimensions to fit into S.T
 Error in the phases of compiler ( during translating program into
object code)
Some Transcription Errors
 The insertion of an extra character
 Deletion of required character
 Replacement of correct character by an incorrect character
04/11/19 swati chauhan (KIET)
1) Lexical Phase Error
 If after some processing lexical analyzer discover that
no prefix of remaining input fits to any token class then invoke
error recovery routine.
Simplest way to recover it
 skip the erroneous character until L.A finds next token
 Disadvantage:
set the problems for next phases of compiler
Ex: comment , number, string e.t.c.
/* there is a program
3.1 4 {lexical error}
“cse department
04/11/19 swati chauhan (KIET)
Error Recovery
 Panic Mode Recovery:
1) The parser discovers an error.
2) If any unwanted character occurs then delete that
character to recover error.
3) Rejects input symbols until a “synchronizing”
token usually a statement delimiter as:
semicolon ; , end } is encountered.
3) Parser delete stack entry until finds an entry with which it
can continue parsing.
04/11/19 swati chauhan (KIET)
2) Syntactic Errors
 Examples of Syntactic Errors
1) Missing right Parenthesis:
max(A, 2* (3+B) { Deletion error }
2) Extra Comma: for(i=0;,1<100;i++) { insertion error }
3) Colon in place of semicolon :
I = 1: {Replacement Error}
4) Misspelled keyword :
Void mian () {Transposition Error}
5) Extra Blank:
/* comment * / {Insertion Error}
04/11/19 swati chauhan (KIET)
Minimum Distance correction of syntactic
error
 Theoretical way of defining errors and their location
 It is called “Minimum Hamming distance” Method.
 Let a program P has errors = k
 Find shortest sequence of error transformations that will map to
valid program
 Ex: IFA =B THEN
SUM =SUM + A;
ELSE
SUM =SUM - A;
“Minimum Hamming distance” = 1 (Transformation may be the
insertion or deletion of a single character)
IF A =B THEN
SUM =SUM + A;
ELSE
SUM =SUM - A;
Recovery from syntactic Error
I) Panic Mode Recovery:
The parser discovers an error. It then discards input
symbols till a designated set of synchronizing token is
found.
● Synchronizing tokens selected are such that their role in
the program is unambiguous, like Delimiters ; } etc.
● Advantage: Simple and never goes into an infinite loop.
04/11/19 swati chauhan (KIET)
Panic Mode Recovery in LL(1) Parser
 Grammar => E -> TE’
 E’
-> +TE’
/ ɛ
 T -> F T’
 T’
-> * F T’
/ ɛ
 F -> (E)/id
id + * ( ) $
E E -> TE’
E -> TE’
E’
E’
-> +TE’
E’
-> ɛ E’
-> ɛ
T T -> F T’
T -> F T’
T’
T’
-> ɛ T’
-> * F T’
T’
-> ɛ T’
-> ɛ
F F -> id F -> (E)
LL(1) Parsing Table for a given Grammar
Algorithm of panic mode recovery in
LL(1)Parsing
1) Parser looking for entry in parsing table
2) if M[A, a] = ‘Blank’
then input symbol a skipped
else if M[A, a]= “Synch”
then pop off the nonterminal from the top of the
stack
else top[token] ≠ Input symbol
then pop off the token from the stack
Processing:
Fill the synch entries under the follow of
nonterminals
id + * ( ) $
E E -> TE’
E -> TE’
synch synch
E’
E’
-> +TE’
E’
-> ɛ E’
-> ɛ
T T -> F T’
synch T -> F T’
synch synch
T’
T’
-> ɛ T’
-> * F T’
T’
-> ɛ T’
-> ɛ
F F -> id synch synch F -> (E) synch synch
Fill “synch” under the follow of nonterminals ………..
Then perform the operation for the
Input string (w) = * id *+ id $
According to Algorithm
04/11/19 swati chauhan (KIET)
II) Phrase –level Recovery
● Local Correction by parser on remaining input, by some
string which allows parser to continue.
● Replacing comma by semicolon, inserting extra semicolon
etc.
● Perform local correction on the input to repair the error
● Drawbacks: Improper replacement might lead to infinite
loops.
Hard to find where is actual error.
● Advantage: It can correct any input string.
swati chauhan (KIET)
III) Global Correction
 Compiler perform some changes to process
the input string.
 It uses simple way , where choose minimal sequence of
changes to obtain least cost correction.
 Input:: Incorrect I/P string = X
Grammar= G
 Then algorithm will find the parse tree for related
string = Y
 Transform X toY by performing some insertion, deletion
and changes in to the token stream.
04/11/19 swati chauhan (KIET)
Disadvantages
 Too costly to implement in terms of space and
time.
 Basically includes theoretical interest.
04/11/19 swati chauhan (KIET)
IV) Error Production
 A method of predict common errors that might be
encountered.
● Augmenting the grammar for the language at hand, with
productions as : A-> Error.
● Such a parser will detect expected errors when an error
production is used.
● Ex:- Automatic Error recovery in YACC
Use error production with semantic actions
A : Error ɛ {semantic action to recover error}.
● Advantage: Error diagnostics is very fast.
3) Recovery from Semantic error
 Sources of Error
i) Undeclared names and type incompatibilities.
ii) Recovery
a) Type Checking, where compiler report the nature
and location of error.
b)Declare the undeclared names and stored into the
symbol table
Stack and Heap Allocation
04/11/19 swati chauhan (KIET)
Program Address Space
 Any program you run has, associated with it, some memory which is
divided into:
 Code Segment
 Data Segment (Holds Global Data)
 Stack (where the local variables and other temporary information
is stored)
 Heap
Code
Segment
Data Segment
Stack
HeapThe Heap grows
downwards
The Stack
grows
upwards
Local Variables:Stack Allocation
 When we have a declaration of the form “int a;”:
 a variable with identifier “a” and some memory allocated to it is created in the
stack. The attributes of “a” are:
 Name: a
 Data type: int
 Scope: visible only inside the function it is defined, disappears once we
exit the function
 Address: address of the memory location reserved for it. Note: Memory
is allocated in the stack for a even before it is initialized.
 Size: typically 2 bytes
 Value: Will be set once the variable is initialized
 Since the memory allocated for the variable is set in the beginning itself, we
cannot use the stack in cases where the amount of memory required is not
known in advance. This motivates the need for HEAP
Pointers
 We know what a pointer is. Let us say we have declared a pointer “int
*p;” The attributes of “a” are:
 Name: p
 Data type: Integer address
 Scope: Local or Global
 Address: Address in the data segment or stack segment
 Size: 32 bits in a 32-bit architecture
 We saw how a fixed memory allocation is done in the stack, now we
want to allocate dynamically. Consider the declaration:
 “int *p;”. So the compiler knows that we have a pointer p that may
store the starting address of a variable of type int.
 To point “p” to a dynamic variable we need to use a declaration of
the type “ p = new int;”
Pointers : Heap Allocation
 Dynamic variables are never initialized by the compiler, so it
is a good practice to initialize it.
 In more compact notation:
int *p;
p = new int;
*p = 0;
int *p = new
int(0);
Static Data Storage Allocation
 Compiler allocates space for all
variables (local and global) of
all procedures at compile
timeNo stack/heap allocation;
no overheads
 Ex: Fortran IV and Fortran 77
 Variable access is fast since
addresses are known at compile
time
 No recursion
04/11/19 swati chauhan (KIET)
Main program
variables
Procedure P1
variables
Procedure P2
variables
Procedure P4
variables
Main memory
Dynamic Data Storage Allocation
 Compiler allocates space only for golbal variables at
compile time
 Space for variables of procedures will be allocated at
run-time Stack/heap allocation
 Ex: C, C++, Java, Fortran 8/9
 Variable access is slow (compared to static allocation)
since addresses are accessed through the stack/heap
pointer
 Recursion can be implemented
Variable Storage Offset
Computation
 The compiler should compute the offsets at which
variables and constants will be stored in the activation
record (AR)
 These offsets will be with respect to the pointer pointing
to the beginning of the AR
 Variables are usually stored in the AR in the declaration
order
 Offsets can be easily computed while performing
semantic analysis of declarations
Static Scope and Dynamic
Scope
 Static Scope A global identifier refers to the identifier
with that name that is declared in the closest enclosing
scope of the program text
 Uses the static(unchanging) relationship between blocks in
the program text
 Dynamic Scope A global identifier refers to the identifier
associated with the most recent activation record
 Uses the actual sequence of calls that are executed in the
dynamic(changing) execution of the program
 Both are identical as far as local variables are concerned
Thank You
04/11/19 swati chauhan (KIET)

More Related Content

What's hot

Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
shashidharPapishetty
 
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
 
Paging and segmentation
Paging and segmentationPaging and segmentation
Paging and segmentation
Piyush Rochwani
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
MAHASREEM
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
Karthi Keyan
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
Akhil Kaushik
 
Symbol Table
Symbol TableSymbol Table
Symbol Table
Akhil Kaushik
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
Tanzeela_Hussain
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
Mahbubur Rahman
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
Mir Majid
 
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
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
Vipul Naik
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
Mukesh Tekwani
 
Lex
LexLex
Linked list
Linked listLinked list
Linked list
akshat360
 
Multiversion Concurrency Control Techniques
Multiversion Concurrency Control TechniquesMultiversion Concurrency Control Techniques
Multiversion Concurrency Control Techniques
Raj vardhan
 
NLP_KASHK:Minimum Edit Distance
NLP_KASHK:Minimum Edit DistanceNLP_KASHK:Minimum Edit Distance
NLP_KASHK:Minimum Edit Distance
Hemantha Kulathilake
 
12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS
koolkampus
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
Akshaya Arunan
 
COMPILER DESIGN
COMPILER DESIGNCOMPILER DESIGN
COMPILER DESIGN
Vetukurivenkatashiva
 

What's hot (20)

Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Paging and segmentation
Paging and segmentationPaging and segmentation
Paging and segmentation
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Symbol Table
Symbol TableSymbol Table
Symbol Table
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Lex
LexLex
Lex
 
Linked list
Linked listLinked list
Linked list
 
Multiversion Concurrency Control Techniques
Multiversion Concurrency Control TechniquesMultiversion Concurrency Control Techniques
Multiversion Concurrency Control Techniques
 
NLP_KASHK:Minimum Edit Distance
NLP_KASHK:Minimum Edit DistanceNLP_KASHK:Minimum Edit Distance
NLP_KASHK:Minimum Edit Distance
 
12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
COMPILER DESIGN
COMPILER DESIGNCOMPILER DESIGN
COMPILER DESIGN
 

Similar to Symbol table management and error handling in compiler design

Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Bhavin Darji
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
appasami
 
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
chinthala Vijaya Kumar
 
C compiler(final)
C compiler(final)C compiler(final)
C compiler(final)
Farzan Dehbashi
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
JAYAPRIYAR7
 
Data Structures (BE)
Data Structures (BE)Data Structures (BE)
Data Structures (BE)
PRABHAHARAN429
 
System programmin practical file
System programmin practical fileSystem programmin practical file
System programmin practical file
Ankit Dixit
 
Cs2251 daa
Cs2251 daaCs2251 daa
C language
C languageC language
C language
spatidar0
 
Linear Data Structures_SSD.pdf
Linear Data Structures_SSD.pdfLinear Data Structures_SSD.pdf
Linear Data Structures_SSD.pdf
ssuser37b0e0
 
Computer Organization and 8085 microprocessor notes
Computer Organization and 8085 microprocessor notesComputer Organization and 8085 microprocessor notes
Computer Organization and 8085 microprocessor notes
Lakshmi Sarvani Videla
 
C language
C languageC language
C language
VAIRA MUTHU
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
Saifur Rahman
 
Lab 1 Essay
Lab 1 EssayLab 1 Essay
Lab 1 Essay
Melissa Moore
 
C1320prespost
C1320prespostC1320prespost
C1320prespost
FALLEE31188
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
Akhil Kaushik
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Clanguage
ClanguageClanguage
Clanguage
Vidyacenter
 
Symbolic Computation and Automated Reasoning in Differential Geometry
Symbolic Computation and Automated Reasoning in Differential GeometrySymbolic Computation and Automated Reasoning in Differential Geometry
Symbolic Computation and Automated Reasoning in Differential Geometry
M Reza Rahmati
 
Computer science ms
Computer science msComputer science ms
Computer science ms
B Bhuvanesh
 

Similar to Symbol table management and error handling in compiler design (20)

Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
 
C compiler(final)
C compiler(final)C compiler(final)
C compiler(final)
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
Data Structures (BE)
Data Structures (BE)Data Structures (BE)
Data Structures (BE)
 
System programmin practical file
System programmin practical fileSystem programmin practical file
System programmin practical file
 
Cs2251 daa
Cs2251 daaCs2251 daa
Cs2251 daa
 
C language
C languageC language
C language
 
Linear Data Structures_SSD.pdf
Linear Data Structures_SSD.pdfLinear Data Structures_SSD.pdf
Linear Data Structures_SSD.pdf
 
Computer Organization and 8085 microprocessor notes
Computer Organization and 8085 microprocessor notesComputer Organization and 8085 microprocessor notes
Computer Organization and 8085 microprocessor notes
 
C language
C languageC language
C language
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
Lab 1 Essay
Lab 1 EssayLab 1 Essay
Lab 1 Essay
 
C1320prespost
C1320prespostC1320prespost
C1320prespost
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Theory1&amp;2
 
Clanguage
ClanguageClanguage
Clanguage
 
Symbolic Computation and Automated Reasoning in Differential Geometry
Symbolic Computation and Automated Reasoning in Differential GeometrySymbolic Computation and Automated Reasoning in Differential Geometry
Symbolic Computation and Automated Reasoning in Differential Geometry
 
Computer science ms
Computer science msComputer science ms
Computer science ms
 

Recently uploaded

Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 

Recently uploaded (20)

Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 

Symbol table management and error handling in compiler design

  • 2. Symbol Table  Symbol table is data structure created and maintained by compilers to store information about the occurrence of various entities such as variable names, function names, objects, classes, interfaces, etc.  Symbol table is used by both the analysis and the synthesis parts of a compiler. 04/11/19 swati chauhan (KIET)
  • 3. Symbol Table  When identifiers are found, they will be entered into a symbol table, which will hold all relevant information about identifiers and other symbols, variables, constants, procedures statements e.t.c,  This information about the name:-  Type  Its Form , Its Location It will be used later by the semantic analyzer and the code generator. Lexical Analyzer Semantic Analyzer Code Generator Symbol Table Syntax Analyzer 04/11/19 swati chauhan (KIET)
  • 4.  Efficient to add new entries to the S.T  Dynamic in nature Issues in Symbol Table:  Format of entries  Method of Access  Place where they are stored 04/11/19 swati chauhan (KIET)
  • 5. Contents of Symbol Table Name Information Capabilities of S.T : 1)Checking (Determine whether the given information is in the table) 2)Adding New Information 3)Access the information of Name 4)Deletion 04/11/19 swati chauhan (KIET)
  • 6. Symbol Table Entries  We will store the following information about Name and each entry in the symbol table is associated with attributes that support the compiler in different phases:  The name (as a string).  Size and Dimension  The data type.  Its scope (global, local, or parameter).  Its offset from the base pointer (for local variables and parameters only). 04/11/19 swati chauhan (KIET)
  • 7. Implementation  Use linear Array of records ,one record per name.  Entries of S.T are not uniform so to make it uniform some information is kept outside the table and pointer to this information stored in S.T.  Record (consist known no. of consecutive words of memory,, so names stored in record) It is appropriate if upper bound on the length of identifier is given….. 04/11/19 swati chauhan (KIET)
  • 8. Data Structure for S.T  Required to make n-entries and m-inquiries 1) Lists: Name 1 Info 1 Name 2 Info 2 ……...... ……….. ……….. ……….. It is easy to implement Addition is easy Retrieve Information ADVANTAGES: Minimum space is required Addition in table is easy DISADVANTAGE: Higher Access time Available
  • 9. 2)Binary Search Tree:  Efficient approach to organize S.T with two field :::  Left and Right  Algorithm for searching name in B.S.T P= initially a pointer to root 1) If Name = Name (P) then Return /* success */ 2) Else if Name < Name (P) then P:= left(P) /* visit left child */ 3) Else Name (P) < Name then P:= Right (P) /* visit right child */ Addition Firstly search, if doesn’t exist then create new node at proper position. swati chauhan (KIET)
  • 10. 3)Hash Table  Consists K words [0….k-1]  Pointers into the storage table (linked list)  Searching Name in S.T  Apply hash function to name h(Name) -> {0…..k-1 (integer) } Addition new Name Create a record at available space in storage table and link that record to h(Name)th list. Hashing > BST > Linear List 04/11/19 swati chauhan (KIET)
  • 11. Representing Scope information in S.T  Scope: The region of a program where a binding is active  The same name in a different scope can have a different binding  Rules for governing scope :  1) If name declared within a block B then valid only within B  2) If B1()  {…………..<-  B2()  {…….}  } 04/11/19 swati chauhan (KIET)
  • 12.  Require complicated S.T organization  So use Multiple symbol tables, one for each block  Each Table has : Name and Information  If New block entered Then Push an empty table into the stack for storing names and information. Ex:- Program main Var x,y : integer ; Procedure P: Var x,a : boolean ; Procedure Q: Var x,y,z : real; Begin …… end Begin ……end Begin ……end 04/11/19 swati chauhan (KIET)
  • 13. Symbol Table organization that compiles with static scope information rules  Next technique to represent scope information in S.T: 1) Write nesting depth of each procedure block 2) Use pair (Procedure name, nesting depth) to access the information from the table 04/11/19 swati chauhan (KIET)
  • 14. Error Detection & Recovery  programmers make mistakes  Error Compile Time Run Time Lexical Phase Error Syntactic Phase Error Semantic Error swati chauhan (KIET) • Overflow {Indicates that the magnitude of a computational result is too large to represent.} Underflow {ndicates that the magnitude of a computational result is too close to zero to represent.} • invalid subscript {} • An integer division by zero
  • 15. Sources of Error  Algorithmic Error  Coding Error  A program may exceed a compiler or machine limit Ex:- Array declaration with too many dimensions to fit into S.T  Error in the phases of compiler ( during translating program into object code) Some Transcription Errors  The insertion of an extra character  Deletion of required character  Replacement of correct character by an incorrect character 04/11/19 swati chauhan (KIET)
  • 16. 1) Lexical Phase Error  If after some processing lexical analyzer discover that no prefix of remaining input fits to any token class then invoke error recovery routine. Simplest way to recover it  skip the erroneous character until L.A finds next token  Disadvantage: set the problems for next phases of compiler Ex: comment , number, string e.t.c. /* there is a program 3.1 4 {lexical error} “cse department 04/11/19 swati chauhan (KIET)
  • 17. Error Recovery  Panic Mode Recovery: 1) The parser discovers an error. 2) If any unwanted character occurs then delete that character to recover error. 3) Rejects input symbols until a “synchronizing” token usually a statement delimiter as: semicolon ; , end } is encountered. 3) Parser delete stack entry until finds an entry with which it can continue parsing. 04/11/19 swati chauhan (KIET)
  • 18. 2) Syntactic Errors  Examples of Syntactic Errors 1) Missing right Parenthesis: max(A, 2* (3+B) { Deletion error } 2) Extra Comma: for(i=0;,1<100;i++) { insertion error } 3) Colon in place of semicolon : I = 1: {Replacement Error} 4) Misspelled keyword : Void mian () {Transposition Error} 5) Extra Blank: /* comment * / {Insertion Error} 04/11/19 swati chauhan (KIET)
  • 19. Minimum Distance correction of syntactic error  Theoretical way of defining errors and their location  It is called “Minimum Hamming distance” Method.  Let a program P has errors = k  Find shortest sequence of error transformations that will map to valid program  Ex: IFA =B THEN SUM =SUM + A; ELSE SUM =SUM - A; “Minimum Hamming distance” = 1 (Transformation may be the insertion or deletion of a single character) IF A =B THEN SUM =SUM + A; ELSE SUM =SUM - A;
  • 20. Recovery from syntactic Error I) Panic Mode Recovery: The parser discovers an error. It then discards input symbols till a designated set of synchronizing token is found. ● Synchronizing tokens selected are such that their role in the program is unambiguous, like Delimiters ; } etc. ● Advantage: Simple and never goes into an infinite loop. 04/11/19 swati chauhan (KIET)
  • 21. Panic Mode Recovery in LL(1) Parser  Grammar => E -> TE’  E’ -> +TE’ / ɛ  T -> F T’  T’ -> * F T’ / ɛ  F -> (E)/id id + * ( ) $ E E -> TE’ E -> TE’ E’ E’ -> +TE’ E’ -> ɛ E’ -> ɛ T T -> F T’ T -> F T’ T’ T’ -> ɛ T’ -> * F T’ T’ -> ɛ T’ -> ɛ F F -> id F -> (E) LL(1) Parsing Table for a given Grammar
  • 22. Algorithm of panic mode recovery in LL(1)Parsing 1) Parser looking for entry in parsing table 2) if M[A, a] = ‘Blank’ then input symbol a skipped else if M[A, a]= “Synch” then pop off the nonterminal from the top of the stack else top[token] ≠ Input symbol then pop off the token from the stack
  • 23. Processing: Fill the synch entries under the follow of nonterminals id + * ( ) $ E E -> TE’ E -> TE’ synch synch E’ E’ -> +TE’ E’ -> ɛ E’ -> ɛ T T -> F T’ synch T -> F T’ synch synch T’ T’ -> ɛ T’ -> * F T’ T’ -> ɛ T’ -> ɛ F F -> id synch synch F -> (E) synch synch Fill “synch” under the follow of nonterminals ……….. Then perform the operation for the Input string (w) = * id *+ id $ According to Algorithm
  • 25. II) Phrase –level Recovery ● Local Correction by parser on remaining input, by some string which allows parser to continue. ● Replacing comma by semicolon, inserting extra semicolon etc. ● Perform local correction on the input to repair the error ● Drawbacks: Improper replacement might lead to infinite loops. Hard to find where is actual error. ● Advantage: It can correct any input string. swati chauhan (KIET)
  • 26. III) Global Correction  Compiler perform some changes to process the input string.  It uses simple way , where choose minimal sequence of changes to obtain least cost correction.  Input:: Incorrect I/P string = X Grammar= G  Then algorithm will find the parse tree for related string = Y  Transform X toY by performing some insertion, deletion and changes in to the token stream. 04/11/19 swati chauhan (KIET)
  • 27. Disadvantages  Too costly to implement in terms of space and time.  Basically includes theoretical interest. 04/11/19 swati chauhan (KIET)
  • 28. IV) Error Production  A method of predict common errors that might be encountered. ● Augmenting the grammar for the language at hand, with productions as : A-> Error. ● Such a parser will detect expected errors when an error production is used. ● Ex:- Automatic Error recovery in YACC Use error production with semantic actions A : Error ɛ {semantic action to recover error}. ● Advantage: Error diagnostics is very fast.
  • 29. 3) Recovery from Semantic error  Sources of Error i) Undeclared names and type incompatibilities. ii) Recovery a) Type Checking, where compiler report the nature and location of error. b)Declare the undeclared names and stored into the symbol table
  • 30. Stack and Heap Allocation 04/11/19 swati chauhan (KIET)
  • 31. Program Address Space  Any program you run has, associated with it, some memory which is divided into:  Code Segment  Data Segment (Holds Global Data)  Stack (where the local variables and other temporary information is stored)  Heap Code Segment Data Segment Stack HeapThe Heap grows downwards The Stack grows upwards
  • 32. Local Variables:Stack Allocation  When we have a declaration of the form “int a;”:  a variable with identifier “a” and some memory allocated to it is created in the stack. The attributes of “a” are:  Name: a  Data type: int  Scope: visible only inside the function it is defined, disappears once we exit the function  Address: address of the memory location reserved for it. Note: Memory is allocated in the stack for a even before it is initialized.  Size: typically 2 bytes  Value: Will be set once the variable is initialized  Since the memory allocated for the variable is set in the beginning itself, we cannot use the stack in cases where the amount of memory required is not known in advance. This motivates the need for HEAP
  • 33. Pointers  We know what a pointer is. Let us say we have declared a pointer “int *p;” The attributes of “a” are:  Name: p  Data type: Integer address  Scope: Local or Global  Address: Address in the data segment or stack segment  Size: 32 bits in a 32-bit architecture  We saw how a fixed memory allocation is done in the stack, now we want to allocate dynamically. Consider the declaration:  “int *p;”. So the compiler knows that we have a pointer p that may store the starting address of a variable of type int.  To point “p” to a dynamic variable we need to use a declaration of the type “ p = new int;”
  • 34. Pointers : Heap Allocation  Dynamic variables are never initialized by the compiler, so it is a good practice to initialize it.  In more compact notation: int *p; p = new int; *p = 0; int *p = new int(0);
  • 35. Static Data Storage Allocation  Compiler allocates space for all variables (local and global) of all procedures at compile timeNo stack/heap allocation; no overheads  Ex: Fortran IV and Fortran 77  Variable access is fast since addresses are known at compile time  No recursion 04/11/19 swati chauhan (KIET) Main program variables Procedure P1 variables Procedure P2 variables Procedure P4 variables Main memory
  • 36. Dynamic Data Storage Allocation  Compiler allocates space only for golbal variables at compile time  Space for variables of procedures will be allocated at run-time Stack/heap allocation  Ex: C, C++, Java, Fortran 8/9  Variable access is slow (compared to static allocation) since addresses are accessed through the stack/heap pointer  Recursion can be implemented
  • 37. Variable Storage Offset Computation  The compiler should compute the offsets at which variables and constants will be stored in the activation record (AR)  These offsets will be with respect to the pointer pointing to the beginning of the AR  Variables are usually stored in the AR in the declaration order  Offsets can be easily computed while performing semantic analysis of declarations
  • 38. Static Scope and Dynamic Scope  Static Scope A global identifier refers to the identifier with that name that is declared in the closest enclosing scope of the program text  Uses the static(unchanging) relationship between blocks in the program text  Dynamic Scope A global identifier refers to the identifier associated with the most recent activation record  Uses the actual sequence of calls that are executed in the dynamic(changing) execution of the program  Both are identical as far as local variables are concerned
  • 39. Thank You 04/11/19 swati chauhan (KIET)