0% found this document useful (0 votes)
54 views

UNIT 3 - Chapter 1 in Compiler Design

Uploaded by

vamsimanne2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

UNIT 3 - Chapter 1 in Compiler Design

Uploaded by

vamsimanne2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

UNIT 3 – CHAPTER 1

Syntax Directed Translation

Syntax Directed Translation: Syntax Directed definition-construction of


syntax trees, Bottom-up evaluation of S-attribute Definitions-L-attribute
Definitions.
Introduction
 Semantic Analysis Phase :

o Semantic Analysis Phase verifies the parse tree, whether it’s meaningful or
not. It furthermore produces a verified parse tree. It also does type checking,
Label checking, and Flow control checking.
o We have mentioned 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.
 Actual and formal parameter mismatch.
 There are two ways to represent the semantic rules associated with grammar symbols.
o Syntax-Directed Definitions (SDD)
o Syntax-Directed Translation Schemes (SDT)
 Syntax-Directed Definitions (SDD)
o A syntax-directed definition (SDD) is a context-free grammar together with
attributes and rules. Attributes are associated with grammar symbols and
rules are associated with productions.

o An attribute may be a string, a number, a memory location, datatypes etc

Production Semantic Rules


E → E1 + T E.val = E 1.val + T.val
E→T E.val = T.val
 Syntax-Directed Translation Schemes (SDT)
o The Syntax directed translation scheme is a context -free grammar. The
syntax directed translation scheme is used to evaluate the order of semantic
rules. In translation scheme, the semantic rules are embedded within the
right side of the productions.
o Why SDT : After implementing the Semantic Analysis, the source program is
modified to an intermediate form. There is some information that is required
by the Intermediate code generation phase to convert the semantically
checked parse tree into intermediate code. But this information or attributes
of variables cannot be represented alone by Context- Free Grammar. So,
some semantic actions have to be attached with Context-Free grammar
which helps the intermediate code generation phase to generate
intermediate code.
o So, attaching attributes to the variables of the context Free Grammar and
defining semantic action (meaning) of each production of grammar is
called Syntax Directed Translation.

o The syntax-directed translation (SDT) scheme is beneficial because it allows


the compiler designer to define the generation of intermediate code directly
in terms of the syntactic structure of the source language.

Annotated Parse Tree

o A parse tree showing the values of attributes at each node is called an

Annotated parse tree. (or) The process of computing the attributes values
at the nodes is called annotating of the parse tree.
o In generally, the order of these computations depends on the dependency
graph induced by the semantic rules.
TYPES OF SDT’S
 SDT is divided into two subsets known as synthesized and inherited
attributes of grammar.

1. Synthesized attributes: A Synthesized attribute is an attribute of the


non-terminal on the left-hand side of a production. Synthesized
attributes represent information that is being passed up the parse tree.
The attribute can take value only from its children (Variables in the RHS
of the production).
o For eg. Let’s say A -> BC is a production of a grammar, and A’s
attribute is dependent on B’s attributes or C’s attributes then it
will be synthesized attribute.
2. Inherited attributes: An attribute of a nonterminal on the right-hand
side of a production is called an inherited attribute. The attribute can
take value either from its parent or from its siblings (variables in the LHS
or RHS of the production).
o For example, let’s say A -> BC is a production of a grammar and
B’s attribute is dependent on A’s attributes or C’s attributes then
it will be inherited attribute.
 Examples for Synthesized attributes:
 Example: Consider the following grammar:

S --> E
E --> E1 + T
E --> T
T --> T1 * F
T --> F
F --> digit

 The SDD for the above grammar can be written as follow:

 Let us assume an input string 4 * 5 + 6 for computing synthesized attributes.


 The annotated parse tree for the given input string is:
o For computation of attributes, we start from leftmost bottom node. The rule F –> digit is
used to reduce digit to F and the value of digit is obtained from lexical analyzer
which becomes value of F i.e. from semantic action F.val = digit.lexval.
o Hence, F.val = 4 and since T is parent node of F so, we get T.val = 4 from semantic
action T.val= F.val.
o Then, for T –> T1 * F production, the corresponding semantic action is
T.val = T1 .val * F.val . Hence, T.val = 4 * 5 = 20
o Similarly, combination of E1 .val + T.val becomes E.val = E1.val + T.val = 26. Then, the
production S –> E is applied to reduce E.val = 26 and semantic action associated with it
prints the result E.val .
o Hence, the output will be 26.
o Examples for Inherited attributes:
o Example: Consider the following grammar
S --> T L
T --> int
T --> float
T --> double
L --> L1, id
L --> id
o The SDD for the above grammar can be written as follow:

o Let us assume an input string: int a, c for computing inherited attributes.


o The annotated parse tree for the input string int a, c is:
o The value of L nodes is obtained from T.type (sibling) which is basically lexical value
obtained as int, float or double.
o Then L node gives type of identifiers a and c. The computation of type is done in top
down manner or preorder traversal.
o Using function Enter_type the type of identifiers a and c is inserted in symbol table at
corresponding id.entry.
TYPES OF SDD’S
o Syntax Directed definitions (SDD) are used to specify syntax directed translations.
There are two types of SDD.

1. S-attributed SDD:
 If an SDD uses only synthesized attributes, it is called as S-attributed
SDD.
 S-attributed SDDs are evaluated in bottom-up parsing, as the values of
the parent nodes depend upon the values of the child nodes.
 Semantic actions are always placed at rightmost place of RHS.
 For eg. Let’s say A -> BC is a production of a grammar, and A’s attribute is
dependent on B’s attributes or C’s attributes then it will be synthesized
attribute.

2. L-attributed SDD:
 If an SDD uses both synthesized attributes and inherited attributes with
a restriction that inherited attribute can inherit values from left siblings
only, it is called as L-attributed SDD.
 Attributes in L-attributed SDDs are evaluated by depth-first and left-to-
right parsing manner.
 Semantic actions are placed anywhere in RHS.
 For example,
A -> XYZ {Y.S = A.S, Y.S = X.S}
is an L-attributed grammar.
 For example,
A -> XYZ {Y.S = A.S, Y.S = X.S, Y.S = Z.S}
is not an L-attributed grammar
since Y.S = A.S and Y.S = X.S are allowed but Y.S = Z.S violates the L-attributed
SDD definition as attributed is inheriting the value from its right sibling.
 Note – If a definition is S-attributed, then it is also L-attributed but NOT vice-versa.
Dependency Graph
 A dependency graph is used to represent the flow of information among the attributes
in a parse tree.
 In a parse tree, a dependency graph basically helps to determine the evaluation order
for the attributes.
 The main aim of the dependency graphs is to help the compiler to check for various
types of dependencies between statements in order to prevent them from being
executed in the incorrect sequence, i.e. in a way that affects the program’s meaning.
 Example of Dependency Graph:
 Design dependency graph for the following grammar:

 The dependency graph for the above grammar is represented as –


Construction of Syntax Trees
 A syntax tree is a tree in which each leaf node represents an operand, while each inside node
represents an operator.
 The Parse Tree is abbreviated as the syntax tree.
 The syntax tree is usually used when representing a program in a tree structure.
Rules for constructing a syntax tree:
 Each node in a syntax tree can be executed as data with multiple fields. In the node for
an operator, one field recognizes the operator and the remaining field includes a pointer
to the nodes for the operands. The operator is known as the label of the node. The
following functions are used to create the nodes of the syntax tree for the expressions
with binary operators. Each function returns a pointer to the recently generated node.
o mknode (op, left, right) − It generates an operator node with label op and two
field including pointers to left and right.
o mkleaf (id, entry) − It generates an identifier node with label id and the field
including the entry, a pointer to the symbol table entry for the identifier.
o mkleaf (num, val) − It generates a number node with label num and a field
including val, the value of the number.
Syntax Directed Translation of Syntax Trees:

Production Semantic Action

E → E(1) + E(2) {E. VAL = Node (+, E(1) . VAL, E(2). VAL)}

E → E(1) ∗ E(2) {E. VAL = Node (∗, E(1). VAL, E(2). VAL)})

E → (E(1)) {E. VAL = E(1). VAL}

E → - E(1) {E. VAL = UNARY (−, E(1). VAL}

E → id {E. VAL = Leaf (id)}

Node (+, E(1). VAL, E(2). VAL) will create a node labeled +.

E(1). VAL &E(2). VAL are left & right children of this node.
Similarly, Node (∗, E(1). VAL, E(2). VAL) will make the syntax as −

Function UNARY (−, E(1). VAL) will make a node – (unary minus) & E(1). VAL will be the only child
of it.
Function LEAF (id) will create a Leaf node with label id.
 Example 1 : construct a syntax tree for an expression a − 4 + c.
 In this sequence, p1, p2, …. . p5 are pointers to the symbol table entries for identifier 'a'
and 'c' respectively.

p1− mkleaf (id, entry a);


p2− mkleaf (num, 4);
p3− mknode ( ′−′, p1, p2)
p4− mkleaf(id, entry c)
p5− mknode(′+′, p3, p4);

The tree is generated in a bottom-up fashion. The function calls mkleaf (id, entry a) and mkleaf
(num 4) construct the leaves for a and 4. The pointers to these nodes are stored using p 1and p2.
The call mknodes (′−′, p1, p2 ) then make the interior node with the leaves for a and 4 as
children. The syntax tree will be
Example 2 − Draw Syntax Tree for the string a + b ∗ c − d.

Example 3 − Construct a syntax tree for the expression.


a = b ∗ −c + d
Solution
Example 4 − Construct a syntax tree for a statement.
If a = b then b = 2 * c
Solution

Example 5 − Consider the following code.


Draw its syntax Tree : If x > 0 then x = 2 * (a + 1) else x = x + 1.
Example6 − Draw syntax tree for following arithmetic expression a * (b + c) – d /2. Also, write
given expression in postfix form.

Postfix Notation
abc+*d2/-
Example 7: Syntax Tree for the string a – b ∗ c + d is:

Syntax tree for example 1


BOTTOM-UP EVALUATION OF S-ATTRIBUTED
DEFINITIONS
 Syntax-directed definitions with only synthesized attributes (S- attribute) can be
evaluated by a bottom-up parser (BUP) as input is parsed.
 In this approach, the parser will keep the values of synthesized attributes associated with
the grammar symbol on its stack.
 The stack is implemented as a pair of state and value.
 When a reduction is made, the values of the synthesized attributes are computed from the
attribute appearing on the stack for the grammar symbols on the right side of the reducing
production.
 Example:
o Consider the syntax-directed definition of the desk calculator in the following figure.

Figure: Syntax-directed definition of a simple desk calculator

o The synthesized attributes in the annotated parse tree of Figure can be evaluated by
an LR parser during a bottom-up parse of the input line 3* 5+4n.
o The following figure shows the sequence of moves made by the parser on input 3*5+4n.
o The contents of the state and val fields of the parsing stack are shown after each move.
o We again take the liberty of replacing stack states by their corresponding grammar
symbols.
o We take the further liberty of showing, instead of token digit, the actual input digit.
o Consider the sequence of events on seeing the input symbol 3.
o In the first move, the parser shifts the state corresponding to the token digit (whose
attribute value is 3) onto the stack. (The state is represented by 3 and the value 3 is in the
val field,)
o On the second move, the parser reduces by the production F -> digit and implements the
semantic rule F.val := digit.lexval.
o On -the third move the parser reduces by T->F.
o No code fragment is associated with this production, so the val array is left unchanged.
o Note that after each reduction the top of the val stack contains the attribute value
associated with the left side of the reducing production.
o Note:
At each shift of digit, we also push digit.lexval into val-stack. • At all other shifts, we do
not put anything into val-stack because other terminals do not have attributes (but we
increment the stack pointer for val-stack).
Figure: Moves made by translator on input 3*5+4 n.
ADDITIONAL Examples
o Example 1:
o Consider the grammar and Semantic Action and Construct Syntax tree, parse tree
and annotated tree for the string 5*6+7;

o Given CFG is:


S→ EN
E→ E+T
E→E-T
E→ T
T→ T*F
T→T/F
T→F
F→ (E)
F→digit
N→;
o Solution:
o The SDD can be written for the above grammar by using semantic actions for each
production.

o For the Non-terminals E,T and F the values can be obtained using the attribute “Val”.
o The taken digit has synthesized attribute “lexval”.
o In S→EN, symbol S is the start symbol. This rule is to print the final answer of
expressed.

o The corresponding annotated parse tree is shown below for the string 5*6+7;
o Example 2:
o PROBLEM: Consider the grammar that is used for Simple desk calculator.
o Obtain the Semantic action and also the annotated parse tree for the string 3*5+4n.

L→En
E→E1+T
E→T
T→T1*F
T→F
F→ (E)
F→digit

o Solution:

o The corresponding annotated parse tree shown below, for the string 3*5+4n.
o Example 3:
o Implementation of Syntax directed translation.
o Syntax direct translation is implemented by constructing a parse tree and performing the
actions in a left to right depth first order.
o SDT is implementing by parse the input and produce a parse tree as a result.

o The corresponding annotated parse tree shown below 23*5+4$

You might also like