Sub topic 16
Dependency Graphs, S-Attributed and L-Attributed Definitions
This chapter assumes that you know context-free grammars, parse trees, recursion, and basic set notation. You should also know that a grammar production describes structure, while semantic rules attach meaning to that structure.
Attributes attach meaning to a parse tree
An attribute is a value associated with a grammar symbol in a parse tree. For example, an expression node might have a val attribute containing its numerical value. A declaration node might have a type attribute.
An attribute can be:
- Synthesized: computed from attributes of children or from the token itself. Information moves upward.
- Inherited: computed from the parent or from a sibling. Information moves downward or across the tree.
A syntax-directed definition (SDD) is a grammar plus rules that compute attributes. Each rule creates dependencies between attribute occurrences.
For a production such as:
E → E1 + T
E.val = E1.val + T.valThe parent attribute E.val depends on the child attributes E1.val and T.val.
Dependency graphs and evaluation order
A dependency graph shows the order in which attribute values must be computed. Each node represents an attribute occurrence in one particular parse tree. An arrow x → y means that y cannot be computed until x is known.
If the dependency graph contains a cycle, no valid evaluation order exists without additional mechanisms. For an acyclic graph, any topological ordering gives a legal evaluation order.
A common error is to inspect only the grammar tree and ignore the dependency arrows. The tree shows structure. The dependency graph shows computation order.
S-attributed definitions
An SDD is S-attributed when every attribute is synthesized. Values flow from leaves toward the root, so a bottom-up traversal can evaluate the definition.
For expression evaluation:
E → E1 + T { E.val = E1.val + T.val }
E → T { E.val = T.val }
T → digit { T.val = digit.lexval }The value of digit is available at a leaf. That value is synthesized into T, then into E.
Every S-attributed definition is also L-attributed. The reverse is not true.
L-attributed definitions
An SDD is L-attributed when inherited attributes obey this rule:
For a production A → X1 X2 ... Xn, an inherited attribute of Xi may depend only on:
- inherited attributes of
A, and - attributes of symbols
X1throughXi-1, which are to its left.
It may not depend on an attribute of a symbol to its right. This restriction allows evaluation during a left-to-right depth-first traversal.
| Definition | Allowed information flow | Typical evaluation |
|---|---|---|
| S-attributed | Children to parent | Bottom-up |
| L-attributed | Parent and left siblings to a child | Left-to-right depth-first |
Common classification mistakes
When classifying an SDD:
- Identify every attribute as synthesized or inherited.
- Check each inherited rule against the left-sibling restriction.
- Draw dependencies if the direction is unclear.
- Check for cycles separately.
Exercise
Is this definition S-attributed, L-attributed, or neither? Production: A → B C. Rules: B.in = A.in; C.in = B.val; A.val = C.val.
Show answer
It is L-attributed but not S-attributed. B.in depends on the parent, C.in depends on the left sibling B, and A.val is synthesized from C.