GATE / CS ARCHIVE

GATE CSE 2023 / QUESTION 60

GATE CSE 2023 Q60

Compiler Design · Semantic Analysis & Type Checking · Full GATE CSE 2023 paper

GATE 2023, Q60 2 marks · NAT
Report issue

Semantic Analysis & Type Checking

Question 60, GATE 2023: Syntax-directed rules: N→I#F gives N.val=I.val+F.val; I→I1 B gives I.val=2I1.val+B.val and I→B copies B.val; F→B F1 gives F.val=(B.val+F1.val)/2 and F→B gives B.val/2; B→0 or 1 takes the bit value. Evaluate 10#011 to three decimals.
Tap to enlarge
Answer & solution

Official answer: 2.374 to 2.376

Our answer: 2.375

Step through it (19 steps)

The I rules interpret the left substring as a binary integer: I(10)=2*1+0=2. Evaluate the fractional suffix from right to left: F(1)=1/2, F(11)=(1+1/2)/2=3/4, and F(011)=(0+3/4)/2=3/8. Thus N.val=2+3/8=2.375, within the official interval 2.374 to 2.376.

Teaching detail. The separator # plays the role of a binary radix point in these semantic rules, not a multiplication or concatenation operator on numbers. The left-recursive integer production multiplies the accumulated prefix by two before appending the next bit. The right-recursive fractional production halves the sum of the current bit and the suffix fraction, placing successive bits at weights 1/2,1/4,1/8. Directly, 10#011 represents 2+0/2+1/4+1/8. This corroborates the synthesized-attribute evaluation and yields an exact terminating decimal.

Original paper, page 63 · Answer key