Syntax Directed Definition
1. Definition
A syntax-directed definition (SDD) attaches productions + semantic rules ({...} actions) that compute attributes, build trees, check types, or emit code. It is the spec; syntax-directed translation (SDT) is the execution interleaved with parsing.
2. Form
E → E1 + T { E.val = E1.val + T.val; E.code = E1.code || T.code || "ADD" }
T → num { T.val = num.lexval }
Each symbol occurrence has attribute slots; rules run when production is used.
3. SDD Uses (exam list)
| Use | Example rule |
|---|---|
| Evaluate | E.val arithmetic |
| Type check | E.type = lub(E1.type, T.type) |
| Build tree | E.node = mkNode('+', E1.node, T.node) |
| Emit IR | `E.code = ... |
| Scope | env inherited down, decls synthesized up |
4. SDD vs SDT
- SDD: unordered rules (what to compute).
- SDT: embedded actions at positions (when to compute) — e.g. action before symbol uses inherited, after uses synthesized.
5. Exam Points
- Define SDD with example rules — 5 marks, very frequent.
- SDD vs SDT difference — 3 marks, frequent.
- Write SDD for expression evaluation / type / tree building — 5-7 marks, frequent.