GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Asked again: Synthesized attributes for binary fractions

2 questions solved the same way, newest first. Back to Compiler Design

GATE asked this question again.

5 marks · Descriptive
Report issue

Asked again GATE 1998, Q23 (same question)

Synthesized attributes for binary fractions

Semantic Analysis & Type Checking

Question 9, GATE 1999: Assign synthesized attributes to evaluate a binary numeral, exemplified by 101.101=5.625. The reproduced grammar is damaged: GateForum prints “S → LL\L; L → LB\B; B → 0\1”, with no radix point; GFG has conventional alternative bars but an unclear/missing radix-point glyph. Conditional intended grammar used below: S→L.L | L; L→LB | B; B→0 | 1. Write semantic rules for every production.
Tap to enlarge
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.

Original paper, page 7

5 marks · Descriptive
Report issue

Asked again GATE 1999, Q9 (same question)

Fractional binary attributes

Semantic Analysis & Type Checking

Question 23, GATE 1998: Give synthesized semantic rules for S→L.L|L; L→LB|B; B→0|1, with 101.101=5.625. The dot is visible in the GateForum raster but faint/lost in some extraction.
Tap to enlarge
Answer & solution

Answer: Use integer value and length attributes for the supplied fractional grammar. (no official key was published)

This production parses the fractional example; replacing it by dotless S→LL would describe a different grammar.

Use L.val for the integer represented by its bit string and L.len for its number of bits. All attributes are synthesized:
S→L1 . L2: S.val=L1.val+L2.val/2L2.len
S→L: S.val=L.val
L→L1 B: L.val=2*L1.val+B.val; L.len=L1.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 prior number left by one, hence multiplication by two. A fractional bit string of length k represents its integer value divided by 2k. For each 101, (val,len) grows (1,1),(2,2),(5,3); S.val=5+5/8=5.625. The length attribute is necessary: .001 and .1 have the same integer value 1 but represent 1/8 and 1/2 respectively. Every semantic expression uses only child attributes, meeting the synthesized-only requirement. Original official wording has not been established by these secondary copies.

Original paper, page 8