20-Module 3_ Syntax Directed Definition-21!08!2024
20-Module 3_ Syntax Directed Definition-21!08!2024
Example : S → ABC
S is said to be a synthesized attribute if it takes values from its child node (A, B, C).
• A syntax directed definition that uses synthesized
Production Semantic rules
attribute exclusively is said to be S-attribute
definition. L En Print (E.val)
F.val=3 digit.lexval=5
String: 3*2+5
An SDD based on a grammar suitable for top-down parsing
TT*F
TF
F digit
Suppose that a semantic rule associated with a production p defines the value
of inherited attribute B.i in terms of the value of X.a. Then, the dependency
graph has an edge from X.a to B.i. For each node N labeled B that corresponds
to an occurrence of this B in the body of production p, create an edge to
attribute i at N from the attribute a at the node M that corresponds to this
occurrence of X. Note that M could be either the parent or a sibling of N.
• Example
Production Semantic Rule
E→E1 + E2 E.val = E1.val + E2.val
1 T.type=real L.in=real 2
real 3 ,
L.in=real id3 4
,
5 L.in=real id2 6
7 id1
Example:
Construct a Dependency graph for the annotated parse tree, whose SDD is given in
Table . The input string is 4 * 6
• We want to build an
attribute grammar that
Attribute Grammar
annotates Number with the
value it represents.
• First we associate attributes
with grammar symbols.
Symbol Attributes
number val
sign neg
list pos, val
bit pos, val