CH3 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Syntax-Directed Translation

1
Introduction
 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.

 Every non-terminal can get one or more than one attribute or sometimes 0 attribute
depending on the type of the attribute. The value of these attributes is evaluated by the
semantic rules associated with the production rule.

 In the semantic rule, attribute is VAL and an attribute may hold anything like a string, a
number, a memory location and a complex record.

 In Syntax directed translation, whenever a construct encounters in the programming


language then it is translated according to the semantic rules define in that particular
programming language.

2
Introduction
 E→E+T E.val := E.val + T.val, E.val is one of the attributes of E.

 We associate information with the programing language constructs by


attaching attributes to grammar symbols.

 Values of these attributes are evaluated by the semantic rules associated


with the production rules. So we can say that

Grammar + semantic rule = SDT (syntax directed translation)

3
Introduction
 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.

 When we associate semantic rules with productions, we use two notations:

 Syntax-Directed Definitions (SDD)

 Translation Schemes (SDTS)

4
Introduction
 Syntax Directed Definitions
 Give high-level specifications for translations
 Hide many implementation details such as order of evaluation of semantic
actions.
 We associate a production rule with a set of sematic actions, and we do not
say when they will be evaluated.

 Translation Schemes
 Indicate the order of evaluation of semantic actions associated with a
production rule.
 In other words, translation schemes give a little bit information about
implementation details.

5
Introduction

 Conceptually with both the syntax directed definition


and translation scheme we
 Parse the input token stream
 Build the parse tree
 Traverse the tree to evaluate the semantic rules at the parse tree
nodes.
Input string → Parse Tree → Dependency graph → evaluation order
for semantic rules.
Conceptual view of syntax directed translation.

6
Syntax Directed Definition
 A SDD is a context free grammar with attributes and rules.
 Attributes are associated with grammar symbols and rules with productions.
 Attributes may be of many kinds: numbers, types, table references, strings, etc.

Synthesized attributes
 A synthesized attribute at node N is computed from the values of attributes at
the children in that node of the parse tree.
 A Synthesized attribute is an attribute of the non-terminal on the left-hand side
of a production.
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.

7
Syntax Directed Definition
Inherited attributes

 An inherited attribute at node N is defined only in terms of attribute values at N’s


parent, N itself and N’s siblings.

 An attribute of a nonterminal on the right-hand side of a production is called an


inherited attribute.

Eg. 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.

Production semantic rules

E->E1+T E.code=E1.code||T.code||’+’

8
Syntax Directed Definition
 In a syntax-directed definition, each production A→ is associated
with a set of semantic rules of the form:

 b= f(c1,c2,…,cn) where f is a function and b can be on of the


followings.

 b is a Synthesized attribute of A and c1,c2,…,cn are attributes of the


grammar symbols in the production (A→). OR

 b is an Inherited attribute one of the grammar symbols in (on the


right side of the production), and c1,c2,…,cn are attributes of the
grammar symbols in the production(A→).

9
Syntax Directed Definition
 Syntax directed definition specifies the values of attributes by
associating semantic rules with the grammar productions.

 It is a CFG with attributes and rules together which are associated with
grammar symbols and productions respectively.

 The process of SDT is two-fold:


 Construction of syntax tree and
 Computing values of attributes at each node by visiting the nodes of syntax
tree.

10
Types of translation
 L-attributed translation
 It performs translation during parsing itself.
 No need of explicit tree construction.
 Semantic actions are placed anywhere in RHS.
 If an SDT 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 SDT.
 Attributes in L-attributed SDTs are evaluated by depth-first
traversal and left-to-right parsing manner.
11
Types of translation
 S-attributed translation
 ‘S’ represents synthesized.
 If an SDT uses only synthesized attributes, it is called as S-
attributed SDT.
 S-attributed SDTs are evaluated in bottom-up parsing, as the
values of the parent nodes depend upon the values of the
child nodes.
 Semantic actions are placed in rightmost place of RHS

12
Types of translation
L-attributed definition S-attributed definition

Uses synthesized & inherited


Uses synthesized attribute only.
attributes.

Semantic rule may be present in the Semantic rule is present at the right
middle also. end.
A → α {rule} β / A → {rule} α β A → α {rule}

Rules are evaluated in DFS from left Semantic rules are evaluated in
to right. (top down). bottom-up manner.

13
Types of translation
Note: Every S - attributed definition is L - attributed
definition but every L - attribute need not be S - attribute.

14
Evaluation Orders for SDD’s
 Dependency Graphs

 Ordering the Evaluation of Attributes

 S-Attributed Definitions

 L-Attributed Definitions

 Semantic Rules with Controlled Side Effects

15
Dependency graph
 While an annotated parse tree shows the values of attributes, a dependency graph helps
us determine how those values can be computed.

 A dependency graph depicts the flow of information among the attribute instances in a
particular parse tree.

 A dependency graph is used to determine the order of computation of attributes.

 It provides information about the order of evaluation of attributes with the help of
edges.

 An edge from the first node attribute to the second node attribute gives the information
that first node attribute evaluation is required for the evaluation of the second node
attribute.

16
Dependency graph
 Edges represent the semantic rules of the corresponding production.

 For each parse tree node, the parse tree has a node for each attribute
associated with that node

 If a semantic rule defines the value of synthesized attribute A.b in


terms of the value of X.c then the dependency graph has an edge from
X.c to A.b.

 If a semantic rule defines the value of inherited attribute B.c in terms


of the value of X.a then the dependency graph has an edge from X.a to
B.c
17
Dependency Graph

1) L -> E
2) E -> E + T
3) E -> T
4) T -> T * F
5) T -> F
6) F -> (E)
7) F -> digit

18
Ordering the Evaluation of
Attributes
 If dependency graph has an edge from M to N then M must be
evaluated before the attribute of N

 Thus 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

 The attribute of the second node is dependent on the attribute of the


first node for further evaluation. This order of evaluation gives a linear
order called topological order.

 If there is any cycle in the graph, then there are no topological sorts;
that is, there is no way to evaluate the SDD on this parse tree.

19
S-Attributed Definitions
 When an SDD is S-attributed, we can evaluate its attributes in any
Bottom-up order of the nodes of the parse tree.

 It is simple to evaluate the attributes by performing a post-order


traversal of the parse tree and evaluating the attributes at a node N
when the traversal leaves N for the last time.

 S-attributed definitions can be implemented during bottom-up


parsing, since a bottom-up parse corresponds to a post order traversal.

 Specifically, post order corresponds exactly to the order in which an


LR parser reduces a production body to its head

20
Example of S-attributed SDD

Production Semantic Rules


1) L -> E n L.val = E.val
2) E -> E1 + T E.val = E1.val + T.val
3) E -> T E.val = T.val
4) T -> T1 * F T.val = T1.val * F.val
5) T -> F T.val = F.val
6) F -> (E) F.val = E.val
7) F -> digit F.val = digit.lexval
1. Symbol E,T and F are associated with a synthesized attribute val.

2. The token digit has a synthesized attribute lexval(it is assumed that it is evaluated by the lexical
analyzer).

3. Terminals are assumed to have synthesized attributes only. Values for attributes of terminals are
usually supplied by the lexical analyzer.

4. The start symbol does not have any inherited attribute unless otherwise stated.

21
L-Attributed Definitions
 A SDD is L-Attributed if the edges in dependency graph goes from Left to
Right but not from Right to Left. More precisely, each attribute must be either

 Synthesized

 Inherited, But if there is a production A->X1X2…Xn and there is an inherited


attribute Xi.a computed by a rule associated with this production, then the rule
may only use:
 Inherited attributes associated with the head A
 Either inherited or synthesized attributes associated with the occurrences of symbols
X1,X2,…,Xi-1 located to the left of Xi
 Inherited or synthesized attributes associated with this occurrence of Xi itself, but in such a way
that there is no cycle in the graph

22
Example of L-attributed
Definition
➢ 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. No non-terminal can get values from the sibling to its right.(inherited)

23
Semantic Rules with
Controlled Side Effects
➢ With SDD's, we strike a balance between attribute grammars and translation schemes.

➢ Attribute grammars have no side effects and allow any evaluation order consistent with the
dependency graph.

➢ Translation schemes impose left-to-right evaluation and allow semantic actions to contain any
program fragment;

➢ We shall control side effects in SDD's in one of the following ways:


➢ Permit incidental side effects that do not constrain attribute evaluation. In other words, permit
side effects when attribute evaluation based on any topological sort of the dependency graph
produces a "correct" translation, where "correct" depends on the application.
➢ Constrain the allowable evaluation orders, so that the same translation is produced for any
allowable order. The constraints can be thought of as implicit edges added to the dependency
graph.

24
Evaluating Semantic rules
 Parse Tree methods
 At compile time evaluation order obtained from the topological sort of dependency graph.
 Fails if dependency graph has a cycle

 Rule Based Methods


 Semantic rules analyzed by hand or specialized tools at compiler construction time
 Order of evaluation of attributes associated with a production is pre-determined at
compiler construction time

 Oblivious Methods
 Evaluation order is chosen without considering the semantic rules.
 Restricts the class of syntax directed definitions that can be implemented.
 If translation takes place during parsing order of evaluation is forced by parsing method.

25
Syntax Directed Translation
Schemes
 A translation scheme is a context free grammar in which
 Attributes are associated with grammar symbols;
 Semantic actions are enclosed between braces{} and are inserted within the right-
hand side of production.

 Translation Schemes deal with both synthesized and inherited attributes.

 Semantic Actions are treated as terminal symbols: Annotated parse-trees


contain semantic actions as children of the node standing for the
corresponding production.

 Translation Schemes are useful to evaluate L-Attributed definitions at parsing


time (even if they are a general mechanism).

26
Syntax Directed Translation
Schemes
 An SDT is a CFG with program fragments embedded within production
bodies

 Those program fragments are called semantic actions.

 They can appear at any position within production body

 Any SDT can be implemented by first building a parse tree and then
performing the actions in a left-to-right depth first order.

 Typically SDT’s are implemented during parsing without building a parse tree.

27
Postfix Translation Schemes
 The syntax-directed translation which has its semantic actions at
the end of the production is called the postfix translation
scheme.

 This type of translation of SDT has its corresponding semantics


at the last in the RHS of the production.

 Example of Postfix SDT


 S ⇢ A#B{S.val = A.val * B.val}
 A ⇢B@1{A.val = B.val + 1}
 B ⇢num{B.val = num.lexval}

28
Example of postfix SDT
1) L -> E n {print(E.val);}
2) E -> E1 + T {E.val=E1.val+T.val;}
3) E -> T {E.val = T.val;}
4) T -> T1 * F {T.val=T1.val*F.val;}
5) T -> F {T.val=F.val;}
6) F -> (E) {F.val=E.val;}
7) F -> digit {F.val=digit.lexval;}

29

You might also like