0% found this document useful (0 votes)
16 views135 pages

Module 3

The document discusses Syntax-Directed Definitions (SDD) and Translation Schemes, which associate grammar symbols with attributes and semantic rules for programming languages. It explains the concepts of synthesized and inherited attributes, the construction of annotated parse trees, and dependency graphs to determine evaluation order. Additionally, it covers the types of SDD, including S-attributed and L-attributed definitions, and their applications in constructing syntax trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views135 pages

Module 3

The document discusses Syntax-Directed Definitions (SDD) and Translation Schemes, which associate grammar symbols with attributes and semantic rules for programming languages. It explains the concepts of synthesized and inherited attributes, the construction of annotated parse trees, and dependency graphs to determine evaluation order. Additionally, it covers the types of SDD, including S-attributed and L-attributed definitions, and their applications in constructing syntax trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 135

Semantic Analysis

Syntax Directed Definition


Syntax-Directed Translation
1. Grammar symbols are associate with attributes to associate information with the
programming language constructs that they represent.

2. Values of these attributes are evaluated by the semantic rules associated with the
production rules.

3. Evaluation of these semantic rules:


– may generate intermediate codes
– may put information into the symbol table
– may perform type checking
– may issue error messages

4. An attribute may hold


– a string, a number, a memory location, a complex record.

3
Syntax-Directed Definitions and Translation Schemes

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


– Syntax-Directed Definitions

– Translation Schemes

A. 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 semantic actions, and we do not say
when they will be evaluated.

B. Translation Schemes:
– Indicate the order of evaluation of semantic actions associated with a production
rule. 4
Syntax-Directed Translation
• Conceptually with both the syntax directed translation 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 5
Syntax-Directed Definitions

1. A syntax-directed definition is a generalization of a context-free


grammar in which:
– Each grammar symbol is associated with a set of attributes.

– This set of attributes for a grammar symbol is partitioned into two


subsets called
• synthesized and

• inherited attributes of that grammar symbol.

– Each production rule is associated with a set of semantic rules.

2. The value of an attribute at a parse tree node is defined by the semantic rule
associated with a production at that node. 6
Synthesized and Inherited Attributes

• The value of a synthesized attribute at a node is computed from


the values of attributes at the children in that node of the parse
tree.
• The value of an inherited attribute at a node is computed from
the values of attributes at the siblings and parent of that node
of the parse tree

7
Synthesized and Inherited Attributes

• Terminals can have synthesized attributes, which are given to it


by the lexical analyzer.
• There are no rules in SDD giving values to attributes for
terminals.
• A non-terminal can have both inherited and synthesized
attributes.

8
Syntax-Directed Definition -- Example
Production Semantic Rules
L→En 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 → digit F.val = digit.lexval
1. Symbols 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. 9
Annotated Parse Tree

1. A parse tree showing the values of attributes at each node is


called an annotated parse tree.

2. Values of Attributes in nodes of annotated parse-tree are either,


– initialized to constant values or by the lexical analyzer.

– determined by the semantic-rules.

3. The process of computing the attributes values at the nodes is


called annotating (or decorating) of the parse tree.

4. Of course, the order of these computations depends on the


10
Annotated Parse Tree -- Example

11
Annotated Parse Tree -- Example

12
Annotated Parse Tree -- Example

13
Exercise

Give annotated parse tree for the following expression.

14
15
16
17
Evaluation Order
https://www.youtube.com/watch?v=vhu7hPnzdVE
19
20
21
Dependency Graph

1. Semantic rules set up dependencies between attributes which can be


represented by a dependency graph.

2. Dependency graph represents the flow of information among the attribute


in a parse tree.

3. This dependency graph determines the evaluation order for the attributes
in a parse tree.

4. While an annotated parse tree shows the values of attributes, a dependency


graph determines how the values can be computed.

22
Dependency Graph
• Directed Graph
• Shows interdependencies between attributes.
• If an attribute b at a node depends on an attribute c, then the semantic rule for b at that
node must be evaluated after the semantic rule that defines c.
• Construction:
– Each attribute is a node
– We add edges from the node for attribute c to the node for attribute b, if b depends
on c
– For procedure calls, we introduction a dummy synthesized attribute that depends
on the parameters of the procedure calls.

23
Dependency Graph Construction
for each node n in the parse tree do
for each attribute a of the grammar symbol at n do
construct a node in the dependency graph for a

for each node n in the parse tree do


for each semantic rule b = f(c1,…,cn)
associated with the production used at n do
for i= 1 to n do
construct an edge from
the node for ci to the node for b

24
25
Dependency Graph Construction
• Example
• Production Semantic Rule
E→E1 + E2 E.val = E1.val + E2.val

E . val

E1. val + E2 . Val


• E.val is synthesized from E1.val and E2.val
• The dotted lines represent the parse tree that is not part of the
dependency graph.

26
Syntax-Directed Definition – Inherited Attributes
Production Semantic Rules
D→TL 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)

1. Symbol T is associated with a synthesized attribute type.

2. Symbol L is associated with an inherited attribute in.

27
Dependency Graph
Input: 5+3*4 L

E.val=17 n

E.val=5 + T.val=12

T.val=5 T.val=3 * F.val=4

F.val=5 F.val=3 digit.lexval=4

digit.lexval=5 digit.lexval=3

28
Annotated parse tree
Input: real p,q,r annotated parse tree
parse tree D
D

T L T.type=real L1.in=real

real L , id3 real L1.in=real , id3

L , id2 L1.in=real , id2

id1 id1

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Types of SDD
• An attribute grammar is a syntax-directed definition in which the
functions in the semantic rules cannot have side effects (they can only
evaluate values of attributes).

• S – Attributed SDD
• L – Attributed SDD

44
S-Attribute SDD / Definition/Grammar
• A syntax directed translation that uses synthesized attributes exclusively is
said to be a S-attributed definition.
• A parse tree for a S-attributed definition can be annotated by evaluating the
semantic rules for the attributes at each node, bottom up from leaves to the
root.

Eg. A  XY

A.S = X.S A.S = Y.S


• Semantic actions are always placed at the right end of the production
• It is also called as ‘Postfix” SDD
45
• Attributes are evaluated with bottom up parsing.
46
L-Attributed SDD / Definition/Grammar
• A syntax directed translation that uses synthesized and inherited attributes is
said to be a L-attributed definition.
• Each inherited attribute is restricted to inherit from parent or left sibling
only.

Eg. A  XYZ

Y.S = A.S, Y.S=X.S, Y.S = Z.S


• Semantic actions are placed anywhere of the right hand side production
• Attributes are evaluated by traversing the parse tree depth first, left to right
order.
47
48
49
50
51
52
53
Evaluation Order

• A topological sort of a directed acyclic graph is any ordering m1,m2…


mk of the nodes of the graph such that edges go from nodes earlier in
the ordering to later nodes.

. i.e if there is an edge from mi to mj them mi appears before mj in the


ordering

• Any topological sort of dependency graph gives a valid order for


evaluation of semantic rules associated with the nodes of the parse tree.
• Translation specified by Syntax Directed Definition

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

55
56
Example

57
Example

58
Applications of Syntax Directed
Translation

59
60
61
62
Syntax Tree-Examples
Expression: if B then S1 else S2
+ if - then - else

5 * B S1 S2
Statement:
3 4 • Node’s label indicates what kind
• Leaves: identifiers or constants of a statement it is
• Internal nodes: labelled with • Children of a node correspond to
operations the components of the statement
• Children: of a node are its
operands
63
Constructing Syntax Tree for Expressions
• Each node can be implemented as a record with several fields.
• Operator node: one field identifies the operator (called label of the node) and
remaining fields contain pointers to operands.
• The nodes may also contain fields to hold the values (pointers to values) of
attributes attached to the nodes.

• Functions used to create nodes of syntax tree for expressions with binary
operator are given below.
– mknode(op,left,right) or new node
– mkleaf(id,entry) or new leaf
– mkleaf(num,val)

Each function returns a pointer to a newly created node.


64
A syntax Directed Definition for Constructing
Syntax Tree
1. It uses underlying productions of the grammar to schedule the calls of
the functions mkleaf and mknode to construct the syntax tree
2. Employment of the synthesized attribute nptr (pointer) for E and T to
keep track of the pointers returned by the function calls.
PRODUCTION SEMANTIC RULE
E  E1 + T E.nptr = mknode(“+”,E1.nptr ,T.nptr)
E  E1 - T E.nptr = mknode(“-”,E1.nptr ,T.nptr)
ET E.nptr = T.nptr
T  (E) T.nptr = E.nptr
T  id T.nptr = mkleaf(id, id.lexval)
T  num T.nptr = mkleaf(num, num.val)

65
Constructing Syntax Tree for Expressions-
Example: a-4+c
+
1. p1:=mkleaf(id,entry-a);
2. p2:=mkleaf(num,4);
3. p3:=mknode(-,p1,p2) - id
4. p4:=mkleaf(id,entry-c); to entry for c
5. p5:= mknode(+,p3,p4); num 4
id

• The tree is constructed bottom to entry for a


up.

66
Annotated parse tree depicting construction of
syntax tree for the expression a-4+c
E.nptr

E.nptr + T.nptr

E.nptr - T.nptr id
+
T.nptr num

- id
id

Entry for c
id num

Entry for a 67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
Syntax Directed Translation Scheme

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Implementation of L – Attributed SDD

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

You might also like