1. Sanjivani Rural Education Society’s
Sanjivani College of Engineering, Kopargaon-423 603
(An Autonomous Institute, Affiliated to Savitribai Phule Pune University, Pune)
NACC ‘A’ Grade Accredited, ISO 9001:2015 Certified
Department of Computer Engineering
(NBA Accredited)
Dr. S. N. Gunjal
Assistant Professor
E-mail : [email protected]
Contact No: 91301 91301 Ext :145, 9503916876
Course- System Software
(CO312)
Unit-III Syntax Directed Definitions
Dr. S.N Gunjal
2. “Dependency graphs” are a useful tool for determining an evaluation order for the attribute
instances in a given parse tree.
While an annotated parse tree shows the values of attributes, a dependency graph helps us
determine how those values can be computed.
We define two important classes of SDD’s: the “S-attributed” and the more general “L-attributed”
SDDs
Evaluation Orders for SDD’s
3. A dependency graph indicate the flow of information among the instances of attributes in a
given parse tree.
An edge from one attribute instance to another means that the value of the first is needed to
compute the second.
Edges express constraints implied by the semantic rules.
Dependency Graphs
4. For each parse-tree node, say a node labeled by grammar symbol X, the dependency graph has a
node for each attribute associated with X.
Evaluation Orders for SDD’s
5. Suppose that a semantic rule associated with a production p defines the value of synthesized
attribute A.b in terms of the value of X.c (the rule may define A.b in terms of other attributes in
addition to X.c) . Then, the dependency graph has an edge from X.c to A.b.
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.
6. Example 5.1 Synthesize E.val from E1.val and E2.val.
Solution. Consider the following production and rule:
Production Rule :
E → E1 + T
Semantic Rule :
E.val = E1.val + T.val
As a convention, we shall show the parse tree edges as dotted lines, while the edges of the
dependency graph are solid.
8. Example 5.2 Construct a Dependency graph for the annotated parse tree of Fig. 5.3, whose
SDD is given in Table 5.1.
11. 15/06/20 DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon
• The dependency graph characterizes the possible orders in which we can evaluate the attributes at the
various nodes of a parse tree.
• If the dependency graph has an edge from node M to node N, then the attribute corresponding to M must
be evaluated before the attribute of N.
• Thus, the only allowable orders of evaluation are those sequences of nodes N1, N2,. . . , Nk such that if
there is an edge of the dependency graph from Ni to Nj; then i < j. Such an ordering embeds a directed
graph into a linear order, and is called a topological sort of the graph.
Ordering the Evaluation of Attributes
Ordering the Evaluation of Attributes
12. 15/06/20 DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon
• 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.
• If there are no cycles, however, then there is always at least one topological sort. To see why, since there
are no cycles, we can surely find a node with no edge entering.
• For if there were no such node, we could proceed from predecessor to predecessor until we came back to
some node we had already seen, yielding a cycle. Make this node the first in the topological order,
remove it from the dependency graph, and repeat the process on the remaining nodes.
13. An SDD is S-attributed if every attribute is synthesized.
Example 5.5: The SDD of Table. 5.2 is an example of an S-attributed definition. Each
attribute, L.val, E.va1, T.val, and F.val is synthesized.
S-Attributed Definitions
14. The second class of SDD’s is called L-attributed definitions.
For these definitions, edges of dependency graph, corresponding to the attributes associated with
production body, can go from left to right but not from right to left in production symbols. More
precisely, each attribute must be either
1. Synthesized, or
2. Inherited, but with the rules limited as follows. Suppose that there is a production
A → X1,X2...Xn, and that there is an inherited attribute Xi.a computed by a rule
associated with this production. Then the rule may use only:
L-Attributed Definitions
15. (a) Inherited attributes associated with the head A.
(b) Either inherited or synthesized attributes associated with the occurrences of symbols X1, X2, ... ,
Xi−1 occurring to the left of Xi.
(c) Inherited or synthesized attributes associated with this occurrence of Xi itself, but only in such a
way that there are no cycles in a dependency graph formed by the attributes of this Xi.
L-Attributed Definitions