Chapter 7 Symbol Tables and Error Handler
Chapter 7 Symbol Tables and Error Handler
• Symbol tables:
– Implementation of a simple stack allocation scheme, Implementation of
block-structured languages, Storage allocation in block- structured
languages
A symbol table is simply a table which can be either linear or a hash table.
For example, if a symbol table has to store information about the following
variable declaration:
The insert function takes the symbol and its attributes as arguments and stores the
information in the symbol table.
b. Lookup operation
Lookup operation is used to search a name in the symbol table
to determine:
if the symbol exists in the table.
if it is declared before it is being used.
Check whether the name is used in the scope.
if the symbol is initialized.
if the symbol declared multiple times.
else it will be searched in the parent symbol table until, either the
name is found or global symbol table has been searched for the
name.
Type Checking in Compiler
Type checking in compilers is the process of ensuring that all
expressions in a program are compatible with their respective type
definitions.
It allows the programmer to limit what types may be used in certain
circumstances and assigns types to values.
Much of what we do in the semantic analysis phase is type checking
The main goal of type checking is:
check that the source program should follow the syntactic and
semantic conventions of the source language.
to check whether the source program is maintained correctly or not.
to check the correctness and data type assignments and type-casting
of the data types, whether it is syntactically correct or not before their
execution.
to check the correctness of the program before its execution.
it checks the type rules of the language.
Also determines whether these values are used appropriately or not.
Cont’d…
• Type checking involves comparing the type of an expression with the expected
type, and ensuring that they are compatible.
• For example, adding two values of different types (such as a string and a
number) would result in a type error.
• In some languages, type inference can help to reduce the amount of explicit type
annotations that are required.
• In these cases, the compiler can deduce the type of an expression based on its
context.
• Once the type checking is complete, the compiler can generate code that is
optimized for the specific types that are used in the program.
• This can lead to faster and more efficient code. Type checking is an important
part of the compiler optimization process.
Conversion
Identify the language constructs that have types associated with them.
Division by zero
expression.
– A type name
– detected correctly
• Purpose
– report errors