0% found this document useful (0 votes)
38 views24 pages

CD Unit3

The document discusses compiler design and focuses on symbol tables and error handling. It describes how symbol tables are used to store semantic information about program elements collected during compilation. Various data structures for implementing symbol tables like lists, linked lists, hash tables and binary search trees are covered. The different types of errors that can occur during compilation like lexical, syntactic and semantic errors are explained. Methods for error detection and recovery in compilers like panic mode recovery are also summarized.

Uploaded by

Mayank Hora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views24 pages

CD Unit3

The document discusses compiler design and focuses on symbol tables and error handling. It describes how symbol tables are used to store semantic information about program elements collected during compilation. Various data structures for implementing symbol tables like lists, linked lists, hash tables and binary search trees are covered. The different types of errors that can occur during compilation like lexical, syntactic and semantic errors are explained. Methods for error detection and recovery in compilers like panic mode recovery are also summarized.

Uploaded by

Mayank Hora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Compiler Design

VI SEMESTER
ETCS-302

Department of Computer Science and Engineering, BVCOE, New Delhi


1
UNIT-III
Computer graphics & multimedia Content

 Symbol Table
Error Handling
Error Recovery

2 Department of Computer Science and Engineering, BVCOE, New Delh


Symbol Table
 Symbol Table is an important data structure created and
maintained by the compiler in order to keep track of
semantics of variable i.e. it stores information about scope
and binding information about names, information about
instances of various entities such as variable and function
names, classes, objects, etc.
 It is built in lexical and syntax analysis phases.
 The information is collected by the analysis phases of
compiler and is used by synthesis phases of compiler to
generate code.
 It is used by compiler to achieve compile time efficiency.

Department of Computer Science and Engineering, BVCOE, New Delhi


3
Symbol Table
It is used by various phases of compiler as follows :-
Lexical Analysis: Creates new table entries in the table, example like entries about
token.
Syntax Analysis: Adds information regarding attribute type, scope, dimension, line
of reference, use, 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 and
what type of run-time 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.
 

Department of Computer Science and Engineering, BVCOE, New Delhi


4
Items stored in Symbol table:

Variable names and constants


Procedure and function names
Literal constants and strings
Compiler generated temporaries

Department of Computer Science and Engineering, BVCOE, New Delhi


5
Information used by compiler from
Symbol table:
Data type and name
Declaring procedures
Offset in storage
If structure or record then, pointer to structure table.
For parameters, whether parameter passing by value or
by reference
Number and type of arguments passed to function
Base Address

Department of Computer Science and Engineering, BVCOE, New Delhi


6
Operations of Symbol table
The basic operations defined on a symbol table
include:

Department of Computer Science and Engineering, BVCOE, New Delhi


7
Implementation of Symbol table –
Following are commonly used data structure for implementing
symbol table :-
List –
In this method, an array is used to store names and associated
information.
A pointer “available” is maintained at end of all stored records and new
names are added in the order as they arrive
To search for a name we start from beginning of list till available pointer
and if not found we get an error “use of undeclared name”
While inserting a new name we must ensure that it is not already present
otherwise error occurs i.e. “Multiple defined name”
Insertion is fast O(1), but lookup is slow for large tables – O(n) on
average
Advantage is that it takes minimum amount of space.
Department of Computer Science and Engineering, BVCOE, New Delhi
8
Continue..
Linked List –
This implementation is using linked list. A link field is added to each record.
Searching of names is done in order pointed by link of link field.
A pointer “First” is maintained to point to first record of symbol table.
Insertion is fast O(1), but lookup is slow for large tables – O(n) on average
Hash Table –
In hashing scheme two tables are maintained – a hash table and symbol table
and is the most commonly used method to implement symbol tables..
A hash table is an array with index range: 0 to tablesize – 1.These entries are
pointer pointing to names of symbol table.
To search for a name we use hash function that will result in any integer
between 0 to tablesize – 1.
Insertion and lookup can be made very fast – O(1).
Advantage is quick search is possible and disadvantage is that hashing is
complicated to implement.
Department of Computer Science and Engineering, BVCOE, New Delhi
9
Continue…
Binary Search Tree –
Another approach to implement symbol table is to use
binary search tree i.e. we add two link fields i.e. left and
right child.
All names are created as child of root node that always
follow the property of binary search tree.
Insertion and lookup are O(log  n) on average.
2

 

Department of Computer Science and Engineering, BVCOE, New Delhi


10
Error Handling in Compiler Design
The tasks of the Error Handling process are to detect each error, report it to
the user, and then make some recover strategy and implement them to handle
error. During this whole process processing time of program should not be
slow. An Error is the blank entries in the symbol table.
 Types or Sources of Error – There are two types of error: run-time and
compile-time error:
A run-time error is an error which takes place during the execution of a
program, and usually happens because of adverse system parameters or
invalid input data. The lack of sufficient memory to run an application or a
memory conflict with another program and logical error are example of this.
Logic errors, occur when executed code does not produce the expected result.
Logic errors are best handled by meticulous program debugging.
Compile-time errors rises at compile time, before execution of the program.
Syntax error or missing file reference that prevents the program from
successfully compiling is the example of this.
Department of Computer Science and Engineering, BVCOE, New Delhi
11
Classification of Compile-time
error

 Lexical : This includes misspellings of identifiers,


keywords or operators
Syntactical : missing semicolon or unbalanced
parenthesis
Semantical : incompatible value assignment or type
mismatches between operator and operand
Logical : code not reachable, infinite loop.

Department of Computer Science and Engineering, BVCOE, New Delhi


12
Error detection and Recovery in Compiler
In this phase of compilation, all possible errors made
by the user are detected and reported to the user in
form of error messages. This process of locating errors
and reporting it to user is called Error Handling
process.
Functions of Error handler
Detection
Reporting
Recovery

Department of Computer Science and Engineering, BVCOE, New Delhi


13
Classification of Errors

Department of Computer Science and Engineering, BVCOE, New Delhi


14
Lexical phase errors
These errors are detected during the lexical analysis phase. Typical
lexical errors are
Exceeding length of identifier or numeric constants.
Appearance of illegal characters
Unmatched string
Example 1 : printf("hello");<illegal $ > < for() >
This is a lexical error since an illegal character $ appears at the
end of statement.
 Example 2 : This is a comment */
 int a, $2, @Q , sum ;
This is an lexical error since end of comment is present but
beginning is not present.
Department of Computer Science and Engineering, BVCOE, New Delhi
15
Error recovery:
Panic Mode Recovery
In this method, successive characters from the input
are removed one at a time until a designated set of
synchronizing tokens is found. Synchronizing tokens
are delimiters such as; or }
Advantage is that it is easy to implement and
guarantees not to go to infinite loop
Disadvantage is that a considerable amount of input is
skipped without checking it for additional errors
It work in 2 phase : lexical phase and syntax phase

Department of Computer Science and Engineering, BVCOE, New Delhi


16
Syntactic phase errors
These errors are detected during syntax analysis phase.
Typical syntax errors are
Errors in structure
Missing operator
Misspelled keywords ofr() smu
Unbalanced parenthesis

Department of Computer Science and Engineering, BVCOE, New Delhi


17
Example
swicth(ch)
{
.......
.......
}

/*..............* / 
The keyword switch is incorrectly written as swicth.
Hence, “Unidentified keyword/identifier” error
occurs.
Department of Computer Science and Engineering, BVCOE, New Delhi
18
Error recovery:
Panic Mode Recovery
In this method, successive characters from input are
removed one at a time until a designated set of
synchronizing tokens is found. Synchronizing tokens are
deli-meters such as ; or }
Advantage is that its easy to implement and guarantees
not to go to infinite loop
Disadvantage is that a considerable amount of input is
skipped without checking it for additional errors

Department of Computer Science and Engineering, BVCOE, New Delhi


19
Department of Computer Science
20 and Engineering, BVCOE, New Delhi
Continue…
Statement/phrase Mode recovery
In this method, when a parser encounters an error, it
performs necessary correction on remaining input so that
the rest of input statement allow the parser to parse
ahead.
The correction can be deletion of extra semicolons,
replacing comma by semicolon or inserting missing
semicolon.
While performing correction, atmost care should be
taken for not going in infinite loop.
Disadvantage is that it  Parser designers have to be
careful here because one wrong correction may lead to
Department of Computer Science and Engineering, BVCOE, New Delhi
21 an infinite loop..
Continue…
Error production
If user has knowledge of common errors that can be encountered then, these
errors can be incorporated by augmenting the grammar with error productions
that generate erroneous constructs.
If this is used then, during parsing appropriate error messages can be
generated and parsing can be continued.
Disadvantage is that its difficult to maintain.  Example: write 5x instead of 5*x
 E->e+e |e*e|error

Global Correction
The parser examines the whole program and tries to find out the closest match
for it which is error free.
The closest match program has less number of insertions, deletions and
changes of tokens to recover from erroneous input.
Due to high time and space complexity, this method is not implemented
practically.
Department of Computer Science and Engineering, BVCOE, New Delhi
22
Semantic errors
These errors are detected during semantic analysis phase.
Typical semantic errors are
Incompatible type of operands a*b
Undeclared variables
Not matching of actual arguments with formal one
Example : int a[10], b;
 .......
 .......
 a = b;
It generates a semantic error because of an incompatible type
of a and b.
Department of Computer Science and Engineering, BVCOE, New Delhi
23
Error recovery
If error “Undeclared Identifier” is encountered then,
to recover from this a symbol table entry for
corresponding identifier is made.
If data types of two operands are incompatible then,
automatic type conversion is done by the compiler.

Department of Computer Science and Engineering, BVCOE, New Delhi


24

You might also like