Asked again GATE 1998, Q23 (same question)
Synthesized attributes for binary fractions
Semantic Analysis & Type Checking

Shared context
Assign synthesized attributes to evaluate a binary numeral, exemplified by 101.101=5.625. Conditional intended grammar used below: S→L.L | L; L→LB | B; B→0 | 1. Write semantic rules for every production.
Answer & solution
Answer: For S→L₁.L₂, use S.val=L₁.val+L₂.val/2^(L₂.len); synthesize L.val and L.len. (no official key was published)
a radix-point production is required to parse the supplied fractional example. The following is an explicitly stated reconstruction consistent with that example, not a claim that an official original was verified. If the production is literally S→LL with no point terminal, the grammar does not parse 101.101 at all and is ambiguous about splitting an unmarked bit string.
Use synthesized attributes L.val (the bit string interpreted as an unsigned integer) and L.len (its number of bits). Let B.val be the numeric value of one bit. Distinguish repeated L occurrences by subscripts:
S → L₁ . L₂ : S.val := L₁.val + L₂.val / 2^(L₂.len) S → L : S.val := L.val L → L₁ B : L.val := 2*L₁.val + B.val; L.len := L₁.len + 1 L → B : L.val := B.val; L.len := 1 B → 0 : B.val := 0 B → 1 : B.val := 1
Appending a bit shifts the existing integer one binary place left, hence multiplication by two. The fractional substring represents its integer value divided by 2 raised to its length; this also handles leading zeros correctly. All right-hand sides depend only on child attributes, so the definition is S-attributed.
For 101, successive (val,len) pairs are (1,1),(2,2),(5,3). Both L occurrences in 101.101 therefore have (5,3), and S.val=5+5/8=5.625. For 0.001, the fractional attributes (1,3) give 1/8 rather than 1/2, showing why the length attribute is essential.
