IAE Academy

GATE 2023 CS Q.60: solution and answer

GATE 2023 · CSQ.60 · 2 marks · NAT

Consider the syntax directed translation given by the following grammar and semantic rules. Here N, I, F and B are non-terminals. N is the starting non-terminal, and #, 0 and 1 are lexical tokens corresponding to input letters "#", "0" and "1", respectively. X.val denotes the synthesized attribute (a numeric value) associated with a non-terminal X. I1 and F1 denote occurrences of I and F on the right hand side of a production, respectively. For the tokens 0 and 1, 0.val = 0 and 1.val = 1.

N → I # F N.val = I.val + F.val
I → I1 B I.val = (2 I1.val) + B.val
I → B I.val = B.val
F → B F1 F.val = (1/2)(B.val + F1.val)
F → B F.val = (1/2) B.val
B → 0 B.val = 0.val
B → 1 B.val = 1.val

The value computed by the translation scheme for the input string 10#011 is ______. (Rounded off to three decimal places)

See the question in the paper

Answer and solution

ANSWER

The value computed for the input 10#011 is 2.375.

Official answer: 2.374 to 2.376Source: Official answer key, GATE 2023 CS (IIT Kanpur)

  1. Split the input as with and . All attributes are synthesized, so evaluate bottom-up.

  2. Integer part: uses , so . Then with gives .

  3. Fraction part, . The last digit comes from : .

  4. The second digit uses with : .

  5. The first digit uses with : . This agrees with the binary fraction .

  6. At the root, : .

KEY IDEA

The grammar reads the integer part as a binary number (each new digit doubles the value so far and adds itself), and the fraction part as a binary fraction built from the right: each digit adds to the value of the rest and halves it. So 10 gives 2, 011 gives 0.375, and the sum is 2.375.

WATCH OUT FOR

Evaluating the fraction from the left, or halving the digits in the wrong order. The rule puts the leftmost digit at weight 1/2, so the recursion must start from the last digit. Starting from the first digit gives a wrong value.

From the GATE 2023 CS paper. Every question of this paper · Download the full paper · All 7 Syntax-directed translation questions, with notes

More Syntax-directed translation questions