The GATE Grind

GATE 2023 CS – Question 60

Compiler Design · Syntax-Directed Translation · 2 marks · Numerical answer

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. $I_1$ and $F_1$ 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 = ½ (B.val + F1.val)
F → B : F.val = ½ 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)

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 2.375

Explanation

I for '10' = 2·1+0 = 2. For F on '011': F('1') = 0.5, F('11') = ½(1+0.5) = 0.75, F('011') = ½(0+0.75) = 0.375. So N.val = 2 + 0.375 = 2.375.