Let the attribute 'val' give the value of a binary number generated by S in the following grammar: S -> L . L S -> L L -> L B L -> B B -> 0 B -> 1 For example, an input 101.101 gives S.val = 5.625. Construct a syntax directed translation scheme using only synthesized attributes, to determine S.val.

GATE 1998 · Compiler Design · Syntax Directed Translation · medium

Answer: SDTS with synthesized attributes: B -> 0 { B.val = 0 } B -> 1 { B.val = 1 } L -> B { L.val = B.val; L.len = 1 } L -> L1 B { L.val = L1.val*2 + B.val; L.len = L1.len+1 } S -> L { S.val = L.val } S -> L1 . L2 { S.val = L1.val + L2.val / 2^(L2.len) }

  1. Base case rules for B and single-digit L: B -> 0 { B.val = 0 } B -> 1 { B.val = 1 } L -> B { L.val = B.val; L.len = 1 }
  2. Recursive L rule: L -> L1 B { L.val = L1.val * 2 + B.val; L.len = L1.len + 1 } This works left-to-right: each new digit B is appended, shifting the existing value left (multiply by 2) and adding B.
  3. S rules combining integer and fractional parts: S -> L { S.val = L.val } S -> L1 . L2 { S.val = L1.val + L2.val / 2^(L2.len) } Verification: 101.101 -> L1.val=5, L1.len=3, L2.val=5, L2.len=3 S.val = 5 + 5/2^3 = 5 + 5/8 = 5 + 0.625 = 5.625 ✓