2024_CD-Ch04 & 05_SDD_Type Checking
2024_CD-Ch04 & 05_SDD_Type Checking
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?
structure.
Semantics help to interpret symbols, their types and relations with each other
Semantic analysis judges whether the syntax structure constructed in the source
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
Example: S → ABC
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;
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
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
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)
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
DT id; D | ε This grammar deals with basic and array type.
implicitly converted
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.