0% found this document useful (0 votes)
8 views27 pages

Unit 3 Part 4

The document discusses type checking in programming languages. It covers type synthesis and inference, type conversions, overloading of functions and operators, polymorphic functions, and the unification algorithm for type checking.

Uploaded by

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

Unit 3 Part 4

The document discusses type checking in programming languages. It covers type synthesis and inference, type conversions, overloading of functions and operators, polymorphic functions, and the unification algorithm for type checking.

Uploaded by

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

Unit 3

Translation of Expressions

1. Operations within Expressions


2. Incremental Translation
3. Addressing Array elements
4. Translation of Array References
1. Operations within expressions

● SDD attributes:
○ code - denote three address code
○ addr - address that will hold value of non-terminal
● Top - points to current symbol table
● top.get() - retrieves entry of parameter passed
● gen() - generate 3 address instruction , pass operator in quotes
● new Temp() - Create distinct temp name
2. Incremental Translation
● Code attributes can be long strings, so they are usually generated incrementally
● In the incremental approach, gen not only constructs a three-address instruction, it appends the
instruction to the sequence of instructions generated so far.
● The sequence may either be retained in memory for further processing, or it may be output
incrementally.
3. Addressing Array Elements

12 18 24

0 1 2

Address Element Formula for calculation:


4. Translation of array references
Type Checking

1. Rules of type checking


2. Type conversions
3. Overloading of functions and operators
4. Type Inference and Polymorphic Functions
5. An algorithm for unification
What is type checking?
● assign a type expression to each component of the source program
● The compiler must then determine that these type expressions conform to a collection of logical
rules that is called the type system for the source language.
● It can be used to detect errors in the program.
● A sound type system eliminates the need for dynamic checking for type errors, because it allows us
to determine statically that these errors cannot occur when the target program runs.
● type checking have been used to improve the security of systems that allow software modules to
be imported and executed

Example:
1. Rules for Type Checking (Synthesis/Inference)

Type Synthesis Type Inference


● builds up the type of an expression from the ● Determines the type of a language
types of its subexpressions. construct from the way it is used.
● It requires names to be declared before
● Let null(x) be a function to check if list is
they are used.
empty.
● Eg. Type of E1+E2 is determined from types
of E1 and E2. ● Since null function applied on list, we can
say x is a list
2. Type Conversions/ Type Casting

● Type casting is converting one datatype to some other data type. It is executed using the
cast operator.
● Explicit type conversion in C is when the datatype conversion is user-defined according to
the program's needs. Explicit type conversion can easily change a particular data type to a
different one.
● Implicit type conversion is automatically done by the program.

● Need:

a=5

b = 10.12

print(a+b)
3. Overloading of functions and operators
a+b
4. Type Inference and Polymorphic Functions
● Type inference is useful for a language like ML which is strongly typed, but does not require names
to be declared before they are used. Type inference ensures that names are used consistently.
● The term polymorphic refers to any code fragment that can be executed with arguments of
different types.
● polymorphism is characterized by parameters or type variables
5. Unification Algorithm
1. Unification is the problem of determining whether two expressions s and t can be
made identical by substituting expressions for the variables in s and t.
2. Testing equality of expressions is a special case of unification; if s and t have constants
but no variables, then s and t unify if and only if they are identical.
3. The unification algorithm in this section extends to graphs with cycles, so it can be
used to test structural equivalence of circular types
4. Type variables are represented by leaves and type constructors are represented by
interior nodes.
5. Nodes are grouped into equivalence classes; if two nodes are in the same equivalence
class, then the type expressions they represent must unify.
6. Thus, all interior nodes in the same class must be for the same type constructor, and
their corresponding children must be equivalent.
Finds representative node of equivalence class

Union merges classes with nodes s and t

You might also like