Sub topic 21
Dependency graphs, S-attributed and L-attributed definitions
This chapter assumes you know what synthesized and inherited attributes are, how a parse tree is built, and how LL(1) and LR parsers read input. It covers the order in which attribute values can be computed and which kinds of definition a one-pass parser can evaluate.
Dependency graphs
A syntax-directed definition (SDD) attaches semantic rules to productions. A rule such as E.val = E1.val + T.val says that one attribute is computed from others. For a given parse tree, the dependency graph records these relationships:
- Each node is an attribute instance of a specific parse-tree node. Two
Tnodes have two separateT.valnodes. - A directed edge from
btocmeanscneeds the value ofb. - Synthesized attributes get edges from child attributes to the parent. Inherited attributes get edges from the parent or left siblings to the child.
Any evaluation order that respects the edges is valid. A topological sort of the graph gives one. If the graph has a cycle, no order exists and the SDD is circular for that tree. Deciding whether an SDD can produce a cycle for some tree is expensive (exponential in the worst case), so practical compilers restrict the SDD to classes that are safe by construction.
Take the LL(1) expression grammar with inherited attributes:
T -> F T' { T'.inh = F.val ; T.val = T'.syn }
T' -> * F T1' { T1'.inh = T'.inh * F.val ; T'.syn = T1'.syn }
T' -> ε { T'.syn = T'.inh }
F -> digit { F.val = digit.lexval }For the input 3 * 5, the dependency graph is:
Inherited values travel down and to the right (T'.inh, then T1'.inh). Synthesized values travel back up (T1'.syn, then T'.syn, then T.val).
You can evaluate any acyclic dependency graph with a topological sort. This program does so for the graph above, then shows that a cycle is detected:
from graphlib import TopologicalSorter, CycleError
# attribute -> (list of attributes it depends on, function of their values)
rules = {
"F_val": ([], lambda: 3),
"F2_val": ([], lambda: 5),
"Tp_inh": (["F_val"], lambda f: f),
"Tp1_inh": (["Tp_inh", "F2_val"], lambda inh, f: inh * f),
"Tp1_syn": (["Tp1_inh"], lambda inh: inh),
"Tp_syn": (["Tp1_syn"], lambda s: s),
"T_val": (["Tp_syn"], lambda s: s),
}
graph = {name: set(deps) for name, (deps, _) in rules.items()}
val = {}
for name in TopologicalSorter(graph).static_order():
deps, fn = rules[name]
val[name] = fn(*[val[d] for d in deps])
print(val["T_val"]) # 15
circular = {"A_s": {"B_i"}, "B_i": {"A_s"}}
try:
list(TopologicalSorter(circular).static_order())
except CycleError:
print("circular definition")S-attributed definitions
An SDD is S-attributed if it uses only synthesized attributes. Every edge in the dependency graph then points from child to parent. Evaluating the tree bottom-up, in any post-order, is always enough.
This fits LR parsing directly. When the parser reduces by A -> X1 ... Xn, the attribute values for X1 ... Xn are already stored with the symbols on the stack. The reduction computes A's attributes from them and stores the result with A when it is pushed. Evaluation costs no extra pass, and no cycle is possible.
The E -> E1 + T { E.val = E1.val + T.val } definition from the previous chapter is S-attributed.
L-attributed definitions
An SDD is L-attributed if every attribute is either synthesized or inherited under a restriction. Consider a production A -> X1 X2 ... Xn. An inherited attribute of Xi may depend only on:
- inherited attributes of
A(the parent), - any attributes of
X1 ... X(i-1)(siblings to the left), - attributes of
Xiitself, as long as they form no cycle.
The "L" stands for left to right. Information flows from the left siblings and the parent, never from a right sibling. This guarantees that a depth-first, left-to-right traversal can compute every attribute. It matches the order in which an LL parser, including recursive descent, builds the tree.
Every S-attributed definition is also L-attributed, because it has no inherited attributes to restrict.
| Property | S-attributed | L-attributed |
|---|---|---|
| Attributes allowed | Synthesized only | Synthesized and restricted inherited |
| Dependency direction | Child to parent | Parent and left siblings to child, and child to parent |
| Natural fit | Bottom-up (LR) parsing | Top-down (LL) parsing, depth-first traversal |
| Cycle possible | No | No |
Checking the L-attributed condition
Test each inherited attribute against the three rules above. In T' -> * F T1', the rule T1'.inh = T'.inh * F.val uses the parent's inherited attribute and the left sibling F. It is allowed.
Now consider A -> B C with the rule B.i = C.s. The inherited attribute of B depends on its right sibling C. The definition is not L-attributed. It is still acyclic if C.s does not depend on B.i, so a general topological order exists. A single left-to-right pass cannot do it.
Edge cases
- Terminals have only synthesized attributes, supplied by the lexer, such as
digit.lexval. Their values are never computed by rules. - An SDD with side effects, such as printing, needs a fixed order. Dependency edges can be added to force it.
- An L-attributed SDD can be converted for bottom-up parsing by adding marker nonterminals. The next chapter covers action placement.
Exercise
Production S -> A B has the rules B.i = A.s and A.i = B.s. Is the SDD L-attributed? Is its dependency graph acyclic?
Show answer
It is not L-attributed, because A.i depends on B.s, an attribute of the right sibling. The graph also has a cycle if A.s depends on A.i and B.s depends on B.i: A.s -> B.i -> B.s -> A.i -> A.s. If A.s and B.s do not depend on their own inherited attributes, the graph is acyclic and only L-attribution fails.
Exercise
A bottom-up parser reduces by E -> E1 + T and evaluates E.val = E1.val + T.val. Why is this safe with no extra pass, and which class does it belong to?
Show answer
The rule uses only synthesized attributes of the body symbols, and those values are already on the parser stack when the reduction happens. The definition is S-attributed, so one bottom-up pass evaluates it.