Answer: A
To find the value of the attribute S.val for the input string "MMLK", we first construct the parse tree based on the grammar rules and then compute the synthesized attributes bottom-up.
1. Parsing the Input "MMLK"
The start symbol is S, which derives DHTU. We match the input string "MMLK" to these non-terminals:
- D matches "MM":
D→"M"D1→"M""M"D2→"M""M"ϵ
- H matches "L":
H→"L"H1→"L"ϵ
- T matches ϵ:
T→ϵ
- U matches "K":
U→"K"
2. Evaluating the Attributes
Now, we compute the synthesized attributes for each subtree:
Evaluating D.val:
- For D2→ϵ:
D2.val=−5
- For D1→"M"D2:
D1.val=5+D2.val=5+(−5)=0
- For D→"M"D1:
D.val=5+D1.val=5+0=5
Evaluating H.val:
- For H1→ϵ:
H1.val=−10
- For H→"L"H1:
H.val=5×10+H1.val=50+(−10)=40
Evaluating T.val:
- For T→ϵ:
T.val=−5
Evaluating U.val:
- For U→"K":
U.val=5
3. Computing S.val
Using the semantic rule for S→DHTU:
S.val=D.val+H.val+T.val+U.val=5+40+(−5)+5=45
Thus, the correct value computed by the SDD is 45.