0% found this document useful (0 votes)
25 views39 pages

2024_CD-Ch04 & 05_SDD_Type Checking

Chapter Four of Compiler Design discusses Syntax Directed Translation (SDT) and its components, including Syntax Directed Definitions (SDD), types of attributes, and the evaluation order for SDD. SDD generalizes context-free grammars by associating attributes and semantic rules with grammar symbols, enabling the construction of annotated parse trees that carry semantic information. The chapter also covers semantic errors, attribute evaluation, and the construction of syntax trees for effective semantic analysis in compilers.

Uploaded by

munyemola
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)
25 views39 pages

2024_CD-Ch04 & 05_SDD_Type Checking

Chapter Four of Compiler Design discusses Syntax Directed Translation (SDT) and its components, including Syntax Directed Definitions (SDD), types of attributes, and the evaluation order for SDD. SDD generalizes context-free grammars by associating attributes and semantic rules with grammar symbols, enabling the construction of annotated parse trees that carry semantic information. The chapter also covers semantic errors, attribute evaluation, and the construction of syntax trees for effective semantic analysis in compilers.

Uploaded by

munyemola
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/ 39

Compiler Design

Chapter Four: Syntax Directed Translation

4.1. Introduction: Syntax-directed definitions (SDD)


Outline 4.2. Types of Attributes
4.3. Evaluation order for SDD
4.4. Construction of Syntax tree for SDD

By: Tseganesh M.(MSc.)


4.1. Introduction: Syntax Directed Definition (SDD)
 Syntax Directed Definition (SDD): is a generalization of a CFG in which;
 each grammar symbols is associated with a set of attributes to associate
information and
 each productions rules is associated with a set of semantic rules
 In syntax analysis phase, we have learnt how a parser constructs parse trees, but the it
is not used for a compiler as it is,
 Because, it does not carry any information about of how to evaluate the tree.
 i.e., the productions of CFG makes the rules of the language, but do not accommodate
how to interpret them. For example: E → E + T
 This CFG production has no any semantic rule associated with it, and it cannot
help in making any sense of the production.

1/1/2025 WCU-CS Compiled by TM. 2


SDD cont’d….
 Semantic information can be represented by associating CFG used in SA with
semantic rules
 Then the result is a syntax-directed translation/Definition,

 i.e., SDD = Context free Grammar(production rule) + Semantic Rules

 So, a parse tree that showing the values of attributes at each node is called Annotated
parse tree.
 The process of computing the attributes values at the nodes is called annotating (or
decorating) of the parse tree.
 Obviously, the order of these computations depends on the dependency graph induced
by the semantic rules.
 What is Semantics?

1/1/2025 WCU-CS Compiled by TM. 3


SDD cont’d….
 What is Semantics?
 Semantics of a language provide meaning to its constructs, like tokens and syntax

structure.
 Semantics help to interpret symbols, their types and relations with each other

 Semantic analysis judges whether the syntax structure constructed in the source

program derives any meaning or not.


For example: int a = “value”;
 This is not an error issue in LA and SA phase, as it is lexically and structurally

correct,
 But it should generate a semantic error as the type of the assignment differs.

 List Some of the semantics errors that the semantic analyzer will generate

1/1/2025 WCU-CS Compiled by TM. 4


SDD cont’d….
 Semantic Errors
 Some of the semantics errors that the semantic analyzer is expected to recognize:
 Type mismatch
 Undeclared variable
 Reserved identifier misuse.
 Multiple declaration of variable in a scope.
 Accessing an out of scope variable.
 Attributes for expressions can be a:
 type of value: int, float, double, char, string,...
 type of construct: variable, constant, operations, ...
 Values of these attributes are evaluated by the semantic rules associated with the
production rules, which is set by grammar of the language
1/1/2025 WCU-CS Compiled by TM. 5
SDD cont’d….
 Evaluation of these semantic rules:
 may generate intermediate codes
 may put information into the symbol table
 may perform type checking
 may issue error messages
 may perform some other activities
 The following tasks should be performed in semantic analysis:
 The source program will be checked for semantic errors
 Scope resolution
 Type checking: type information will be collected for the code generation
 Array-bound checking

1/1/2025 WCU-CS Compiled by TM. 6


SDD cont’d….
 In SDD, every non-terminals can get zero or more attributes depend on the type of attribute.
 Example: newval := oldval + 12 here, the type of the identifier newval must match with type
of the expression (oldval+12)
 In Semantic rule, attributes (such as data type, string, number, memory location, etc.) can be
represented by VAL.
 A SDD specifies the values of attributes by associating semantic rules with the grammar productions.
 Example:
 Production Semantic Rule/actions
EE+T E.val=E.val + T.val
ET E.val=T.val
TE*T T.val=E.val * T.val
TF T.val=F.val
FNUM F.val=num.lexval (this attribute return by LA)
 We may also alternatively insert the semantic actions inside the grammar.
 Example: E  E+T {print ‘+’}
1/1/2025 7
4.2. Types of Attribute
 Attribute Grammar
 Attribute grammar is a medium to provide semantics or some additional information (attributes) to
CFG.
 Attribute grammar (viewed as a parse-tree) can pass values or information among the nodes of a tree.
 Example: E → E + T { E.value = E.value + T.value }
 The right part of the CFG contains the semantic rules that specify how the grammar should be
interpreted.
 Here, the values of E and T are added together and the result is copied to E.
 Since SDD is a generalization of a CFG, in which each grammar symbol is associated with a set of
attributes.
 Based on the way the attributes get their values, this the set of attributes for a grammar
symbol is partitioned into two categories/subsets called:
i. synthesized attributes and
1/1/2025 ii. inherited attributes. 8
Types of Attribute cont’d….
i. Synthesized attribute, meaning that its value at a parse tree node is obtained
from attributes of child nodes in the parse tree,
 or from other attributes of the same node in the parse tree

 Example: S → ABC

 If S is taking values from its child nodes (A,B,C),

 then it is said to be a synthesized attribute, as the values of ABC are synthesized to S.

 If an SDD uses only synthesized attributes, it is called as S-attributed Definitions.


 These attributes are evaluated using S-attributed SDTs that have their semantic actions written
after the production (right hand side).

 As this diagram shows, attributes in S-attributed


SDDs are evaluated in bottom-up parsing,
 And the values of the parent nodes depend upon the
values of the child nodes.
1/1/2025 9
Types of Attribute cont’d….
ii. Inherited attribute, meaning that its value is obtained
 from the parent node in the parse tree,
 or from sibling nodes in the parse tree
 useful when there is mismatch between grammar for parsing, and “abstract syntax” for translation
 Example S → ABC
 A can get values from S, B and C.
 B can take values from S, A, and C.
 Likewise, C can take values from S, A, and B
 A SDD that uses both synthesized and inherited attributes with restriction of not taking values from
right siblings is called L-attributed Definitions.
 In L-attributed SDTs, a non-terminal can get values from its parent, child, and sibling nodes.
 See this production: S → ABC S can take values from A, B, and C (synthesized).
 A can take values from S only.

 B can take values from S and A.

 C can get values from S, A, and B.


1/1/2025 10
Types of Attribute cont’d….
 Reduction: When a terminal is
 Expansion: When a non-terminal reduced to its corresponding non-
is expanded to terminals as per a terminal according to grammar rules.
grammatical rule

 Syntax trees are parsed top-down and left to right.


 Whenever reduction occurs, we apply its corresponding semantic rules (actions).
 Semantic analysis uses Syntax Directed Translations to perform the above tasks.
 Semantic analyzer receives AST (Abstract Syntax Tree) from its previous stage (syntax analysis).
 Semantic analyzer attaches attribute information with AST, which are called Attributed AST.
 Attributes are two tuple value, <attribute name, attribute value>
 For example: int value = 5;<type, “integer”><presentvalue, “5”>
 For every production, we attach a semantic rule.
11
Types of Attribute cont’d….
 Example: synthesized attributed SDD for simple calculator’s Arithmetic Expression; “9+3*5\n”;
Production Semantic Rule
L  E \n L.val = E.val L.val=24
E  E1 + T E.val = E1.val + T.val
E T E.val = T.val
T  T1 * F T.val = T1.val * F.val E.val=24 \n
T F T.val = F.val
F (E) F.val = E.val +
E.val=9 T.val=15
F  digit F.val = digit.lexval
T.val=3 F.val=5
T.val=9 *
 Symbols E, T, and F are associated
with a synthesized attribute val.
F.val=9 This is an annotated parse tree
 digit is a token which has a synthesized
attribute lexval (it is assumed that it is
evaluated by the lexical analyzer) digit.lexval=9

12
4.3. Evaluation order of SDD
 Dependencies of Attributes
 In semantic rule: b := f(c1, c2, ..., ck), we say b depends on c1, c2, ..., ck
 The semantic rule for b must be evaluated after the semantic rules for c1, c2, ..., ck
 This dependencies of attributes can be represented by a directed graph called dependency graph
 A dependency graph is used to determine the order of evaluation of attributes with the help of
edges.
 Evaluation order of SDD’s
 It indicate the flow of information among the instances of attributes in a given parse tree.
 An edge from one attribute instance to another
 means that the value of the first is needed to compute the second.
 Example: if dependency graph has an edge from M to N then M must be evaluated before the
attribute of N

1/1/2025 13
Evaluation order cont’d….
 The only allowable orders of evaluation are those sequence of nodes N1,N2,…,Nk such that if
there is an edge from Ni to Nj then i<j
 Such an ordering is called a topological sort of a graph, which produces a possible sequential
order of evaluation
 Evaluation order for SDD includes how the SDD is evaluated with the help of attributes,
dependency graphs, semantic rules, and S and L attributed definitions.
 In dependency graphs an edge from the first node to the second node attribute gives the information
that evaluation of first node attribute is required for the evaluation of the second node attribute.
 Edges represent the semantic rules of the corresponding production.
 Values of synthesized attributes may be calculated in any order
 For a S-attributed SDD, any bottom-up order be sufficient
 Whereas for inherited attributes, order of evaluation is important
 it is possible to write rules for which no order of evaluation is possible
1/1/2025  In a L-attributed SDD, dependencies always go left-to-right, so no circularity is14 possible.
4.4. Construction of Syntax Tree/ Annotated Parse Tree for SDD
 A parse tree that showing the value of attributes at each node is called an annotated parse tree.
 Syntax tree/annotated parse tree is a condensed form of parse tree useful for representing
language constructs.
 Terminals can have synthesized attributes, but not inherited attributes.
 Example1: Consider the following CFG and compute Synthesized Attributes of it
 S→ EN E→ E+T E→E-T E→ T T→ T*F T→T/F T→F F→ (E) F→digit N→;
 The SDD of this CFG can be written by using semantic actions for each production as follows;

Production Semantic Rule/actions


S  EN S.val = E.val  For the Non-terminals E, T, and F the values can be
E  E1 + T E.val = E1.val + T.val obtained using the attribute; val.
E  E1 - T E.val = E1.val - T.val
E T E.val = T.val  A token digit has a synthesized attribute lexval (it is

T  T1 * F T.val = T1.val * F.val assumed that it is evaluated by the lexical analyzer)


T→T/F T.val = T1.val / F.val
 In S→EN, symbol S is the start symbol.
T F T.val = F.val
F (E) F.val = E.val
 This rule is to print the final answer of expressed.
F  digit F.val = digit.lexval
N→; can be ignored by lexical Analyzer as ; is terminating symbol
Construction of parse tree for SDD cont’d….
 By using the above CFG to Compute S attributed definition the following steps are basic
 Firstly: Write the SDD using the appropriate semantic actions for corresponding production
rule of the given Grammar.
 secondly: The annotated parse tree is generated and attribute values are computed, in
which the Computation is done in bottom up manner.
 Finally: The value obtained at the node is supposed to be final output.

 For instance: Consider the string 5*6+7; and Construct parse tree, and annotated parse tree using
the above CFG.
This is the corresponding
The corresponding
+ annotated parse tree for the
parse tree for the ; string 5*6+7
string 5*6+7
* 7

5 6

1/1/2025 16
Construction of parse tree for SDD cont’d….
 Exercise: Consider the following grammar:
 L→En E→E1+T E→T T→T*F T→F F→ (E) F→digit that is used for Simple calculator,
 using the given grammar for the given string 3*5+4n

a. Write the appropriate semantic actions for production rule of the given Grammar,
b. Construct Parse tree,
c. Construct annotated parse tree and compute attribute values in bottom up manner.
 The value obtained at the node is supposed to be final output.
Solutions
a. Production Semantic actions b. The corresponding parse tree for the string 3*5+4n
L  En L.val = E.val
E  E1 + T E.val = E1.val + T.val +
E T E.val = T.val n
T  T1 * F T.val = T1.val * F.val 4
*
T F T.val = F.val
F (E) F.val = E.val
F  digit F.val = digit.lexval 3 5
1/1/2025 17
Construction of parse tree for SDD cont’d….
c. The corresponding Annotated parse tree for the string 3*5+4n

 The advantages of annotated parse tree or


SDDs are more readable and useful for
specifications,
 But, its disadvantage is it is not very
efficient.
1/1/2025 18
Compiler Design
Next
Chapter Five: Type Checking
5.1. Rules of Type Checking
Outline 5.1.1. Specifications of a type checker
5.1.2. Equivalence of type expressions
5.2. Type conversion
By: Tseganesh M.(MSc.)
5.1. Rules of Type Checking
 What is Type checking?
 Type checking is the process of verifying and enforcing constraints/rules of types in values.
 Type checking ensures the compatibility and correctness of data types within a program.
 A compiler must check if the source program follows the syntactic and semantic
conventions of the source language.
 Type checking allows the programmer to limit what types may be used in certain
circumstances and assigns what types to values.
 Then the type-checker determines whether these values are used appropriately or not.
 It checks the type of objects and reports a type error in the case of a violation, and incorrect
types are corrected.
 Whatever the compiler we use, while the compiler is compiling the program, it has to check
and follow the type rules of the language.
 Every language has its own set of type rules for the language.
20
Rules of Type Checking cont’d…
 Type checking verifies if the operands used in expressions and statements are compatible
with each other according to the language's specific rules.
 For instance, in a strongly-typed language like Java, adding an integer to a string would
result in a type error since these types are incompatible.
 So, Type checking helps to catch such errors even before the execution of the program,
saving time, effort, and avoiding potential run-time errors.

 Types of Type Checking


 There are two kinds/methods employed in type checking, each with their own advantages
and disadvantages.
i. Static Type Checking.
ii. Dynamic Type Checking.

21
Rules of Type Checking cont’d…
i. Static Type Checking:
 Here, type checking is done based on the program's source code at compile time.
 It checks the type variables, i.e., type of the variable is known at the compile time.
 It examines the program text during the translation of the program.
 also used to determine the amount of memory needed to store the variable
 Static checks include:
a. Type-checks: A compiler should report an error if an operator is applied to an
incompatible operand.
 Example see this line of code: int a, c[10], d;

d=c+a; //here an error occurs, when an array variable is added with ‘function variable
b. flow of control checks: Statements that cause the flow of control to leave a construct must
have someplace to transfer the flow of control.
 Example: an error occurs when enclosing statement like ‘break’ does not exist in switch statements

1/1/2025 WCU-CS Compiled by TM. 22


Rules of Type Checking cont’d…
c. Uniqueness checks: There are situations in which an object must be defined only once.
Example: int i, j, i; //error
add( int a, int a){} //error
d. Name-related checks: Sometimes the same name may appear more than once.
 The Advantages of Static Type Checking:
 enables early detection of type errors, and Runtime Error Protection.

 providing programmers with quick feedback to rectify mistakes.

 It catches syntactic errors like spurious words or extra punctuation.

 Detects incorrect argument types.

 It catches the wrong number of arguments.

 It is efficient and widely used,

 The Disadvantages of Static Type Checking


 It can sometimes lead to false-positive errors, where certain correct programs are
flagged as erroneous
1/1/2025 WCU-CS Compiled by TM. 23
Rules of Type Checking cont’d…
ii. Dynamic Type Checking:
 Here, type checking is done at run time, when the target program runs
 The types are associated with values, not with variables.
 This method is more flexible and adaptable to different program scenarios,
 but it incurs a performance overhead as the checking occurs at runtime.
 A static type system always restricts what can be conveniently expressed.
 Programming with a static type system often requires more design and implementation effort.
 Languages like Pascal and C have static type checking.
 Dynamic type checking is commonly used in dynamically typed languages like Python, where
objects can change their types at runtime
 The main purpose of type-checking is to check the correctness, data type assignments and
type-casting of the data types in a program, before their execution.
1/1/2025 WCU-CS Compiled by TM. 24
Rules of Type Checking cont’d…
 The design of the type-checker depends on:
 Syntactic Structure of language constructs.

 The Expressions of languages.

 The rules for assigning types to constructs (semantic rules).

 The Position of the Type checker in the Compiler

 Description of this Type checking position in Compiler


 The token streams from the lexical analyzer are passed to the PARSER.
 The PARSER will generate a syntax tree.
 When a source code is converted into a syntax tree, the type-checker plays a Crucial Role.
 So, by seeing the syntax tree, you can tell whether each data type is handling the correct
variable or not.
1/1/2025 25
Rules of Type Checking cont’d…
 The Type-Checker will check and if any modifications are present, then it will modify.
 It produces a syntax tree, and after that, INTERMEDIATE CODE Generation is done
 Type checking is carried out in semantic phase, and its information is added to semantic rules
 The type information produced by the type checker may be needed when the code is generated.
 It is especially important for overloaded and polymorphic functions
 Overloading function: is one that has different operations depending on its context.
 A polymorphic function: can be executed with several types of arguments.

 Type Expressions
 Types have structure, which we shall represent using type expressions
 Type expression: is the idea of associating each language construct with an expression that
describing its type.
 The type of a language construct (like tokens) will be denoted by a type expression
1/1/2025 26
Rules of Type Checking cont’d…
 A type expressions are defined inductively from a basic type or from constants using type
constructor (which are formed by applying an operator)
 A type expressions can be:
1. A basic type: which is a primitive data type such as integer, real, char, Boolean, ... are a type expression
 A special basic type, type-error will signal a type error during type checking
 void: denote “ the absence of a value” allows statements to be checked
2. A type name: a name can be used to denote a type expression
3. Type expression may contain variables whose values are type expressions
4. A type constructor applied to other type expression is a type expression.
 A type constructor include
a. Arrays. If I is int index set and T is a type expression, then array (I, T) is a type expression;
denoting the type of an array with elements in T and indices in the range 0 ... n - 1.
 Ex: An array type int[2] can be read as array(2, integer).
1/1/2025 WCU-CS Compiled by TM. 27
Rules of Type Checking cont’d…
 A type constructor include
b. Products. If T1 and T2 are type expressions, the Cartesian product T1 x T2 is a type
expression, x, is left associative. Ex: int x int
c. Pointers. If T is a type expression then pointer(T) is a type expression denoting the type
pointer to an object of type T.
 Ex: pointer (int)

d. Functions. a functions in a programming language has a form of mapping from a domain


type D to a range type R.
 So, the type of such function is denoted by type expression D→R, where, D is TE of the
parameters and R is the TE of the returned value
 Ex: int→int represents the type of a function which takes an int value as parameter,
and its return type is also int.
e. Record is a data structure with named fields.
 Record types can be implemented by applying the constructor record to a symbol table
containing entries for the fields.

1/1/2025 28
5.1.1. Specifications of a simple type checker
 In this section, we specify a type checker for a simple language, in which the type of each
identifier must be declared before the identifier is used.
 The type checker is a translation scheme that synthesizes the type of each expression from the
types of its sub expressions.
 The type declaration with list of names can be done using simplified grammar that declares
just one name at a time.
 The type checker can handle arrays, pointers, statements, and functions.
 Example: see the following grammar
DT id; D | ε  This grammar deals with basic and array type.

 The nonterminal D generates a sequence of declarations.


TB C | record ‘{‘ D ‘}’
 The nonterminal T generates basic, array, or record types.
B char | int | array{num} of T | ↑T
Cε | [num] C
 The nonterminal B generates one of the basic types int, char and float.
 The nonterminal C generates string of zero or integers, each integer surrounded by brackets
1/1/2025 WCU-CS Compiled by TM. 29
Specifications of type checker cont’d…
 The following examples gives the collection of type-checking rules for a given grammar,
which specified in a syntax directed manner.
a. Type checking rule of expressions
 In the grammar rules, the attribute type for E gives the type expression assigned to the
expressions generated by E
1. E literal {E.type := char}
E num {E.type := integerr}
 Here, constants represented by tokens; literal and num have basic type char and integer
2. E id {E.type := lookup(id.entry) }
 lookup(e) is used to fetch the type saved in the symbol table entry pointed to by e.
3. E E1 mod E2 {E.type := if E1.type = integer and E2.type=integer then integer
else type error }
 The expression formed by applying the mod operator to two subexpressions of type integer
has type integer; otherwise, its type is type error
1/1/2025 WCU-CS Compiled by TM. 30
Specifications of type checker cont’d…
4. E E1 [ E2] {E.type := if E1.type = integer and E2.type= array(1,2) then t
else type error }
 In array reference E1 [ E2], the index expression E2must have type integer.
 The result is the element type t obtained from the type array(s,t) of E1.
5. E E1 ↑ {E.type := if E1.type = pointer(t) then t
else type error }
 In postfix operator ↑, yields the object pointed to by its operand.
 The type E ↑ is the type t of the object pointed to by the pointer E.
b. Type checking rule of Statements
 Statements do not have values so the basic type void can be assigned to them
 If an error is detected within a statement, then type error is assigned
1. Assignment Statements:
S→id := E {S.type := if id.type =E.type then void
else type error }
1/1/2025 WCU-CS Compiled by TM. 31
Specifications of type checker cont’d…
2. Conditional Statements:
S→if E then S1 {S.type := if E.type = boolean then S1.type
else type error }
3. While Statements:
S→while E do S1 {S.type := if E.type = boolean then S1.type
else type error }
4. Sequence of Statements:
S→S1;S2 {S.type := if S1.type = void and S1.type = void then void
else type error }
c. Type checking rule of Functions
 The rule for checking the type of a function application is:
E→E1 (E2) {E.type := if E2.type = s and E1.type = St then t
else type_error }

1/1/2025 WCU-CS Compiled by TM. 32


5.1.2. Equivalence of types expression
 Many type-checking rules have the form; "If two type expressions are equal then return a
certain type else return type error."
 Ambiguities arise when names are given to two type expressions, and the names are used in
subsequent expressions.
 Type equivalence that implemented by a compiler can be explained using the concepts of
structural and name equivalence.
a. Structural Equivalence of Type Expressions:-
 If a type expressions built from basic types and constructors, then the equivalence
between two type expressions is structural equivalence.
 Two type expressions are said to be structurally equivalent if and only if one of the
following conditions hold:
 They are built from the same basic type or they are identical,
 They are formed by applying the same constructor to structurally equivalent type
 One is a type name that denotes the other.
1/1/2025 WCU-CS Compiled by TM. 33
Types Equivalence cont’d…..
 For example, the type expression integer is equivalent only to integer because they are the
same basic type.
 Similarly, pointer (integer) is equivalent only to pointer (integer) because the two are
formed by applying the same constructor pointer to equivalent types.
b. Name equivalence
 In some programming languages, we give a name to a type expression, and we use that
name as a type expression afterwards.
 Two type expressions are name equivalent if and only if they are identical.
 In structural equivalence, names are replaced by type expressions that they define,
 So two type expressions are structurally equivalent if they represent two structurally
equivalent type expressions when all names have been substituted.
 Example1: ptr and pointer (integer) are not name equivalent but they are structurally
equivalent.
1/1/2025 WCU-CS Compiled by TM. 34
Types Equivalence cont’d…..
 Example2, in the following Pascal program fragment
type link = ↑ cell;
var next : link;
last : link;
P : f cell;
q, r : t cell;
 The identifier link is declared to be a name for the type cell
 The question arises, do the variables next, last, p, q, r all have same types? Surprisingly, the
answer depends on the implementation;
 How do we treat type names?
 To model this situation, we allow type expressions for a type name(then use structural
equivalence), or
 Treat a type name as a basic type.
 For example; if cell is the name of a type expression, then pointer(cell) is a type expression.
1/1/2025 WCU-CS Compiled by TM. 35
5.2. Type conversion
 In type conversion, a data type is automatically converted into another data type by a
compiler at the compiler time.
 Type conversion can only be applied to compatible data types.
Example: int x=30; float y; y=x; // y==30.000000.
 Consider expressions:
x + i; Where x is of type real and i of type integer
 Of course, the machine cannot execute this operation as it involves different types of values
 However, most languages accept such expressions to be used;
 The compiler will be in charge of converting one of the operand into the type of the other.
 The type checker can be used to insert these conversion operations into the intermediate
representation of the source program
 For example, an operator inttoreal may be inserted whenever an operand need to

implicitly converted

1/1/2025 WCU-CS Compiled by TM. 36


Type conversion Cont’d……
 Coercion
 If the Conversion from one type to another type is done automatically by the compiler, the
conversation is known as implicit.
 Implicit-type conversions are also called Coercion and coercion is limited in many
languages.
 Example: An integer may be converted to a real but real is not converted to an integer.
 But, if the programmer writes something to do the Conversion, then the conversion is said to
be Explicit.
 Tasks:
 has to allow “Indexing is only on an array”
 has to check the range of data types used
 INTEGER (int) has a range of -32,768 to +32767
 FLOAT has a range of 1.2E-38 to 3.4E+38.
Question: What is the difference between Type Casting and Type Conversion? 37
1/1/2025 WCU-CS Compiled by TM.
THANK YOU VERY MUCH!!!!

Compiled by TM. 38
Next Class
Chapter Six
Intermidiate Code Generation
Outline
6.1. Intermediate languages
6.2. Types of three address statements
6.3. Syntax directed translation into three address code
6.4. Implementation of three address statements
6.5. Translation scheme to generate three address code
6.6. Addressing array elements
39
1/1/2025 WCU-CS Compiled by TM.

You might also like