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

PDF
Symbol table in compiler Design
PPTX
Symbol Table
PPT
Lecture 1 - Lexical Analysis.ppt
PPTX
Type checking compiler construction Chapter #6
PPTX
3.5 equivalence of pushdown automata and cfl
PDF
Token, Pattern and Lexeme
PPTX
Theory of automata and formal language
PPT
Intermediate code generation (Compiler Design)
Symbol table in compiler Design
Symbol Table
Lecture 1 - Lexical Analysis.ppt
Type checking compiler construction Chapter #6
3.5 equivalence of pushdown automata and cfl
Token, Pattern and Lexeme
Theory of automata and formal language
Intermediate code generation (Compiler Design)

What's hot (20)

PDF
Design and analysis of algorithms
PPTX
Two pass Assembler
PDF
Bottom up parser
PPTX
Asymptotic Notation
PPTX
Turing machine-TOC
PPTX
Syntax Analysis in Compiler Design
PDF
Algorithms Lecture 1: Introduction to Algorithms
PPTX
Stressen's matrix multiplication
PPT
1.Role lexical Analyzer
PPT
Top down parsing
PDF
Pumping lemma for cfl
PPTX
Compiler design syntax analysis
DOC
Time and space complexity
PPTX
Context free grammar
PDF
Syntax Directed Definition and its applications
PPTX
Single pass assembler
PDF
Lecture 01 introduction to compiler
PPT
Lecture 3,4
PDF
Syntax analysis
PPTX
1.7. eqivalence of nfa and dfa
Design and analysis of algorithms
Two pass Assembler
Bottom up parser
Asymptotic Notation
Turing machine-TOC
Syntax Analysis in Compiler Design
Algorithms Lecture 1: Introduction to Algorithms
Stressen's matrix multiplication
1.Role lexical Analyzer
Top down parsing
Pumping lemma for cfl
Compiler design syntax analysis
Time and space complexity
Context free grammar
Syntax Directed Definition and its applications
Single pass assembler
Lecture 01 introduction to compiler
Lecture 3,4
Syntax analysis
1.7. eqivalence of nfa and dfa
Ad

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

PDF
Compiler Design
PPTX
module 2 introduction to syntax analysis
PPT
Symbol Table, Error Handler & Code Generation
PDF
C compiler(final)
PPT
Lecture 04 syntax analysis
PPTX
Parsers -
DOCX
Cs6660 compiler design may june 2016 Answer Key
PPT
SYNTAX ANALYSIS, PARSING, BACKTRACKING IN COMPILER DESIGN
PPT
SS & CD Module 3
PPT
Module 2
PDF
Static name resolution
PPTX
Ch3_Syntax Analysis.pptx
PDF
04 Syntax Analysis.pdf
PPT
Integrated Fundamental and Technical Analysis of Select Public Sector Oil Com...
DOC
Compiler Design QA
PDF
Lecture8 syntax analysis_4
PPTX
COMPILER DESIGN LECTURES -UNIT-2 ST.pptx
PPTX
Error detection recovery
PPTX
Chapter 6 - Intermediate Languages.pptxjfjgj
Compiler Design
module 2 introduction to syntax analysis
Symbol Table, Error Handler & Code Generation
C compiler(final)
Lecture 04 syntax analysis
Parsers -
Cs6660 compiler design may june 2016 Answer Key
SYNTAX ANALYSIS, PARSING, BACKTRACKING IN COMPILER DESIGN
SS & CD Module 3
Module 2
Static name resolution
Ch3_Syntax Analysis.pptx
04 Syntax Analysis.pdf
Integrated Fundamental and Technical Analysis of Select Public Sector Oil Com...
Compiler Design QA
Lecture8 syntax analysis_4
COMPILER DESIGN LECTURES -UNIT-2 ST.pptx
Error detection recovery
Chapter 6 - Intermediate Languages.pptxjfjgj
Ad

Recently uploaded (20)

PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Business Ethics Teaching Materials for college
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Classroom Observation Tools for Teachers
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Cell Types and Its function , kingdom of life
PPTX
master seminar digital applications in india
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Pre independence Education in Inndia.pdf
Business Ethics Teaching Materials for college
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Week 4 Term 3 Study Techniques revisited.pptx
O7-L3 Supply Chain Operations - ICLT Program
RMMM.pdf make it easy to upload and study
VCE English Exam - Section C Student Revision Booklet
TR - Agricultural Crops Production NC III.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Classroom Observation Tools for Teachers
human mycosis Human fungal infections are called human mycosis..pptx
Cell Types and Its function , kingdom of life
master seminar digital applications in india
FourierSeries-QuestionsWithAnswers(Part-A).pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

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)