Understanding attributes in syntax-directed definitions (SDD) is very important for compiler design exams. Many questions test whether you know how attributes relate to parsing techniques.
In this post, weβll break it down simply and solve a common MCQ.
π΅ What are Attributes in Compiler Design?
In syntax-directed definitions, attributes store information at grammar symbols.
There are two types:
- Synthesized attributes
- Inherited attributes
π΄ What are Synthesized Attributes?
π A synthesized attribute is computed using values from child nodes.
β Key Idea:
Information flows from children β parent
π Example:
X β Y Z
X.i = f(Y.i, Z.i)
π Here:
- X gets value from Y and Z
- So attribute is synthesized
π΄ Parsing Connection (Very Important)
π₯ Key Rule:
π Synthesized attributes are naturally evaluated in:
β Bottom-Up Parsing (BUP)
π΅ Why Bottom-Up Parsing?
In bottom-up parsing:
- Children are processed first
- Then parent is formed
π So computing:
X.i = f(Y.i, Z.i)
becomes easy
π΄ Types of Bottom-Up Parsers
- LR(0)
- SLR(1)
- LALR(1)
- LR(1)
π All belong to LR grammar family
π΅ Top-Down Parsing (Contrast)
Top-down parsers:
- LL(1)
- Recursive descent
π Work parent β children
β Not suitable for synthesized attributes directly
π₯ MCQ: Synthesized attribute can easily be simulated by?
Options:
- LL grammar
- Ambiguous grammar
- LR grammar
- None of the above
π§ Step-by-Step Solution
β Option 1: LL grammar
- Top-down approach
- Does not naturally support synthesized attributes
β Option 2: Ambiguous grammar
- Not related to attribute evaluation
β Option 3: LR grammar
β Correct
π Because:
- LR parsing is bottom-up
- Matches synthesized attribute flow (children β parent)
β Option 4: None of the above
Wrong since option 3 is correct
π― Final Answer
π LR grammar
β‘ Exam Trick to Remember
- Synthesized attribute β Bottom-Up β LR parser
- Inherited attribute β Top-Down β LL parser (generally)
π§ Quick Comparison
| Attribute Type | Direction | Parsing Type |
|---|---|---|
| Synthesized | Children β Parent | Bottom-Up (LR) |
| Inherited | Parent β Children | Top-Down (LL) |
π― Conclusion
Synthesized attributes align perfectly with bottom-up parsing because of their evaluation order. Thatβs why LR parsers are the best fit for such attributes.