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

004chapter 4 - Syntax Directed Translation

Uploaded by

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

004chapter 4 - Syntax Directed Translation

Uploaded by

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

Chapter 4

Syntax Directed Translations


Basic Topics of Chapter -Four

 Introduction

 Semantic Analysis

 Syntax Directed Translation

 Evaluation Orders of SDD’s

 Applications of Syntax Directed Translation

 Syntax Directed Translation Schemes


Introduction
 We have learnt how a parser constructs parse trees in the syntax analysis phase.

 The plain parse-tree constructed in that phase is generally of no use for a compiler,

 as it does not carry any information of how to evaluate the tree.

 The productions of CFG, which makes the rules of the language, do not accommodate
how to interpret them.
 For example: E→E+T
 The above CFG production has no semantic rule associated with it, and it cannot help in
making any sense of the production.
Semantic Analysis
 Semantics of a language provide meaning to its constructs, like tokens and syntax structure.
 Semantic Analysis judges whether the syntax structure constructed in the source program
derives any meaning or not.
 For example: int a = “value”;
 Should not issue an error in lexical and syntax analysis phase, as it is lexically and
structurally correct,
 but it should generate a semantic error as the type of the assignment differs.

 These rules are set by the grammar of the language and evaluated in semantic analysis.
 In typed languages as C, the following tasks should be performed in semantic analysis:
 adding information to the symbol table
 performing type checking
Generate Code
 Issue error messages.
Semantic Analysis….
 The information to be computed is beyond the capabilities of standard parsing techniques,
therefore it is not regarded as syntax.
 As for Lexical and Syntax analysis, also for Semantic Analysis we need both a
Representation Formalism and an Implementation Mechanism.
 As representation formalism this lecture illustrates what are called Syntax Directed
Translations (SDT).

 In SDT, along with the grammar we associate some informal notations and these
notations are called as semantic rules.
 So we can say that:

CFG + semantic rules = Syntax Directed Translations


Syntax Directed Translations (SDT)

• In SDT, 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
grammar 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
 The Principle of SDT states that:
 the meaning of an input sentence is related to its syntactic structure, i.e., to its Parse-
Tree.
 By SDT we indicate those formalisms for specifying translations for programming language
constructs guided by CFG.
Example
Production Semantic Rules
E→E+T E.val = E.val + T.val
E→T E.val = T.val
T→T*F T.val = T.val + F.val
T→F T.val = F.val
F → (F) F.val = F.val
F → num F.val = num.lexval

E.val is one of the attributes of E.


num.lexval is the attribute returned by the lexical analyzer.
Syntax Directed Translations (SDT) ….

 There are two notations for attaching semantic rules:

1. Syntax Directed Definitions (SDD).

 High-level specification hiding many implementation details (also called Attribute Grammars).

2. Syntax Directed Translation Schemes .

 More implementation oriented:

 Indicate the order in which semantic rules are to be evaluated.


Syntax Directed Definitions (SDD)
 SDD- is a kind of abstract specification.

 It is generalization of CFG in which each grammar production X –> a is associated with it a set of production
rules.
 Attributes are associated with grammar symbols and

 Semantic actions are associated with grammar productions.

 If X is a symbol and a is one of its attributes, then we write X.a to denote the value of a at a particular
parse-tree node labeled X.

 Note: The attribute can be a string, number, type or a memory location.


Syntax Directed Definitions (SDD)…
• Semantic actions are fragments of code which are embedded usually at the end of production and enclosed in curly
braces ({ }).

• Example: E --> E1 + T { E.value = E1.value + T.value}

• If curly braces occur as grammar symbols, we enclose them within single quotes, as in ' { ' and '}'.

• The position of a semantic action in a production body determines the order in which the action is executed.

• in general, semantic actions may occur at any position in a production body.


Syntax Directed Definitions (SDD)…
 Between the two notations, SDD can be more readable, and hence more useful for
specifications.
 However, translation schemes can be more efficient, and hence more useful for
implementations.
 The most general approach to SDT is to construct a parse tree or a syntax tree,
 and then to compute the values of attributes at the nodes of the tree by visiting the nodes of
the tree.
 In many cases, translation can be done during parsing, without building an explicit tree.

 We shall therefore study a class of syntax-directed translations called "L-attributed


translations" (L for left-to-right), which encompass virtually all translations that can be
performed during parsing.
 We also study a smaller class, called "S-attributed translations" (S for synthesized),
 which can be performed easily in connection with a bottom-up parse.
Attribute Grammar
 Attribute grammar is a special form of CFG where some additional information (attributes) are
appended to one or more of its non-terminals in order to provide context-sensitive information.
 Each attribute has well-defined domain of values, such as integer, float, character, string, and
expressions.
 Attribute grammar is a medium to provide semantics to the CFG and it can help specify the
syntax and semantics of a programming language.
 Attribute grammar (when viewed as a parse-tree) can pass values or information among the
nodes of a tree.
Attribute Grammar ….
 Example #1: 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 non-terminals E and T are added together and the result is copied to the non-
terminal E.
 Note: Semantic attributes may be assigned to their values from their domain at the time of
parsing and evaluated at the time of assignment or conditions.
 Based on the way the attributes get their values, they can be broadly divided into two categories :

a. Synthesized attributes and b. Inherited attributes.


a. Synthesized attribute
 Synthesized Attributes – which derive their values from their children nodes
 i.e. value of synthesized attribute at node N is defined only interims of attribute values at
the children of N and N itself in parse tree.
 A synthesized attribute for a non-terminal A at parse-tree Node N is defined by a
semantic rule associated with the production at N.
 A Synthesized attributes never take values from their parent nodes or any sibling nodes.

 Ex #1: E --> E1 + T { E.val = E1.val + T.val}


• In this, E.val derive its values from E1.val and T.val

 Ex #2: 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.


Steps to Compute Synthesized Attributes
i. Write the SDD using appropriate semantic rules for each production in given
grammar.

ii. The annotated parse tree is generated and attribute values are computed in
bottom up manner.

iii. The value obtained at root node is the final output.


• Note: What is Annotated Parse Tree ?
– The parse tree containing the values of attributes at each node for given input
string is called annotated or decorated parse tree.
Example #1: SDD
• Consider the following grammar Production Semantic Rules
S --> E
SE { S.val=E.val}
E --> E1 + T|T
T --> T1 * F|F EE1+T {E.val=E1.val+T.val}

F --> digit ET {E.val=T.Val}


• The SDD for the above grammar
can be written as follow TT1*F {T.val=T1.val*F.val}

TF {T.val=F.val}

Fdigit {F.val=digit.lexval}
Annotated parse tree: Example
• Let us assume an input string 4 * 5 + 6 for computing synthesized
attributes. The annotated parse tree for the input string is
Annotated parse tree: Example
description.
• 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.
• 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.

• Then, for T –> T1 * F production, the corresponding semantic action is T.val = T 1.val * F.val .
Hence, T.val = 4 * 5 = 20
• Similarly, combination of E1.val + T.val becomes E.val i.e. E.val = E 1.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 . Hence, the output will be 26.
SDD of a simple desk calculator: Example #2

Production Semantic rules


L→E n L.val = E.val
E → E1 + T E.val = E1.val + T.val
E→T E.val = T.val
T → T1 * F T.val = T1.val * F.val
T→F T.val = F.val
F → (E) F.val = E.val
F → digits F.val = digits.lexval
Evaluating an SDD at the Nodes of a Parse Tree

 The SDD in Example #2- is based on our familiar grammar for


arithmetic expressions with operators + and *.
 It evaluates expressions terminated by an end marker n.

 In the SDD, each of the non terminals has a single synthesized


attribute, called val
 We also suppose that the terminal digit has a synthesized attribute
lexval, which is an integer value returned by the lexical analyzer .
Cont’d
 To visualize the translation specified by an SDD, it helps to work with parse trees,
even though a translator need not actually build a parse tree.
 Imagine therefore that the rules of an SDD are applied by first constructing a
parse tree and then using the rules to evaluate all of the attributes at each of the
nodes of the parse tree.
 How do we construct an annotated parse tree?

 In what order do we evaluate attributes?

 Before we can evaluate an attribute at a node of a parse tree, we must evaluate all
the attributes upon which its value depends.
Cont’d
 For example, if all attributes are synthesized, we must evaluate the val attributes at all of the
children of a node before we can evaluate the val attribute at the node itself.
 With synthesized attributes, we can evaluate attributes in any bottom-up order, such as that
of a postorder traversal of the parse tree.
 Example #2- shows an annotated parse tree for the input string
3 * 5 + 4 n, constructed using the grammar and rules .
 The values of lexval are presumed supplied by the lexical analyzer.
 Each of the nodes for the non-terminals has attribute val computed in a bottom-up order, and
we see the resulting values associated with each node.
 For instance, at the node with a child labeled *, after computing T.val = 3 and F.val = 5 at its
first and third children, we apply the rule that says T.val is the product of these two values, or
15.
Annotated parse tree for 3 * 5 + 4 n
b. inherited attribute
inherited attribute: Example 2
 the following production, 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.


 Note:

• Terminals can have synthesized attribute but not inherited

• Attribute for terminals have lexical values that are supplied by lexical analyzer.

• These are no semantic rules in the SDD itself for computing the values of an
attribute for a terminal.
Computation of Inherited Attributes –
• Construct the SDD using semantic actions.
• The annotated parse tree is generated and attribute values are computed in top down
manner.
Ex: Consider the The SDD for the above grammar can be written as follow
following grammar

S --> T L
T --> int
T --> float
T --> double
L --> L1, id
L --> id
Cont’d.
• Let us assume an input string int a, c for computing inherited attributes.
• The annotated parse tree for the input string is
Cont’d.

• The value of L nodes is obtained from T.type (sibling) which is basically


lexical value obtained as int, float or double.
• Then L node gives type of identifiers a and c.

• The computation of type is done in top down manner or preorder


traversal. Using function Enter_type the type of identifiers a and c is
inserted in symbol table at corresponding id.entry.
Example 3: inherited attributes
production Semantic rules

S→T L L.in = T.type

T → int T.type = integer

T → real T.type = real

L → L1, id L1.in = L.in

addtype(id.entry, L.in)

L → id addtype(id.entry, L.in)

e.g. real id1, id2, id3


Dependency Graph

 "Dependency graphs" are a useful tool for determining an evaluation order for the
attribute instances in a given parse tree.
• Also 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.
 While an annotated parse tree shows the values of attributes, a dependency graph
helps us determine how those values can be computed.
 In other words, inter dependency between Synthesized and inherited attributes at
node in the parse tree is called dependency graph.
Dependency Graph…
• In more detail:

• Dependency graph

– 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.c to B.c
Dependency graph: Example #1
Dependency graph: Example #2
S-attributed vs L-attributed

 We consider two SDD's for constructing syntax trees for expressions.

 The first, an S-attributed definition, is suitable for use during bottom-up parsing.

 The second, L-attributed, is suitable for use during top-down parsing.


S-attributed Definitions
 An SDD is S-attributed if every attribute is synthesized

• Evaluation Order. Semantic rules in a S-Attributed Definition can be evaluated by a


bottom-up, or PostOrder, traversal of the parse tree.
postorder(N) {

for (each child C of N, from the left) postorder(C);

evaluate the attributes associated with node N;

}
 S-Attributed definitions can be implemented during bottom-up parsing without the need to
explicitly create parse trees
S-attributed Definitions
 E.g. These attributes are evaluated using S-attributed that have their semantic actions
written after the production (right hand side).

 As depicted above, attributes in S-attributed are evaluated in bottom-up parsing, as the


values of the parent nodes depend upon the values of the child nodes.
S-Attributed Definition….

 Example. The above arithmetic grammar is an example of an S-Attributed


Definition.
 The annotated parse-tree for the input 3*5+4n is:
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 us 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.
L-Attributed definitions….
 In L-attributed, a non-terminal can get values from its parent, child, and sibling nodes.
 As in the following 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.


 Note: No non-terminal can get values from the sibling to its right.
 Attributes in L-attributed are evaluated by depth-first and left-to-right parsing
manner.
Figure : Hierarchy of SDT

 We may conclude that if a definition is S-attributed, then it is also L-attributed, as L


attributed definition encloses S attributed definitions.
2. Syntax directed translation schemes
 The SDT scheme is a CFG with program fragments embedded within production bodies

 Those program fragments are called semantic actions

 The SDT 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

 It is written within the right side of the production.

 They can appear at any position within production body.

 The position at which an action is to be executed is shown by enclosed between braces

 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
Example
Production Semantic Rules

S→E$ { printE.VAL }

E→E+E {E.VAL = E.VAL + E.VAL }

E→E*E {E.VAL = E.VAL * E.VAL }

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

E→I {E.VAL = I.VAL }

I → I digit {I.VAL = 10 * I.VAL + LEXVAL }

I → digit { I.VAL= LEXVAL}


Postfix translation schemes

 Simplest SDDs are those that we can parse the grammar bottom-up and
the SDD is s-attributed.
 For such cases we can construct SDT where each action is placed at the
end of the production and is executed along with the reduction of the
body to the head of that production
 SDT’s with all actions at the right ends of the production bodies are
called postfix SDT’s
Example1- Infix to Postfix translation scheme
Example #2: 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;}
Application of Syntax Directed Translation :

• SDT is used for Executing Arithmetic Expression.

• In the conversion from infix to postfix expression.

• In the conversion from infix to prefix expression.

• In creating a Syntax tree.

• SDT is used to generate intermediate code.

• In storing information into symbol table.

• SDT is commonly used for type checking also


Questions

1. Explain syntax directed translation.

2. Define sematic actions.

3. Write note on decency graph.

4. Generate a sematic action for production

S → E$
E→E + E/ E*E/(E)/(I)
I → I/Idigit (Where I is a Integer)
Exercise #1
Q1. For the SDD of a simple desk calculator below,

give annotated parse trees for the following expressions:

a) (3 + 4 ) * ( 5 + 6 ) n .
b) 1 * 2 * 3 * (4 + 5) n.
c) (9 + 8 * (7 + 6 ) + 5) * 4 n .
End of chapter-Four!

You might also like