Unit 3 Part 4
Unit 3 Part 4
Translation of 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
Example:
1. Rules for Type Checking (Synthesis/Inference)
● 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