GateAI
Syntax-Directed Translation

GATE CSE 2022 · Q65

NAT2 marksMedium

Time

Consider the following grammar along with translation rules. S→S1 # T{S∙val=S1∙val∗T∙val}S→T{S∙val=T∙val}T→T1%R{T∙val=T1∙val÷R∙val}T→R{T∙val=R∙val}R→id{R∙val=id∙val} \begin{array}{ll} S \rightarrow S_1 \text{ \# } T & \{S_{\bullet val} = S_{1\bullet val} * T_{\bullet val}\} S \rightarrow T & \{S_{\bullet val} = T_{\bullet val}\} T \rightarrow T_1 \% R & \{T_{\bullet val} = T_{1\bullet val} \div R_{\bullet val}\} T \rightarrow R & \{T_{\bullet val} = R_{\bullet val}\} R \rightarrow id & \{R_{\bullet val} = id_{\bullet val}\} \end{array} Here # and % are operators and idid is a token that represents an integer and id∙valid_{\bullet val} represents the corresponding integer value. The set of non-terminals is {S,T,R,P}\{S, T, R, P\} and a subscripted non-terminal indicates an instance of the non-terminal. Using this translation scheme, the computed value of S∙valS_{\bullet val} for root of the parse tree for the expression 20#10%5#8%2%220\#10\%5\#8\%2\%2 is __________.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 80

Syntax-Directed Translation

From the grammar and translation rules, we can determine the operator precedence and associativity:

  1. Precedence:

    • Non-terminal TT is derived from SS, which means the operator % (evaluated as division ÷\div) has higher precedence than # (evaluated as multiplication ∗*).

  2. Associativity:

    • Both productions S→S1 # TS \rightarrow S_1 \text{ \# } T and T→T1%RT \rightarrow T_1 \% R are left-recursive, meaning both operators # and % are left-associative.

Given the expression:

20#10%5#8%2%220 \# 10 \% 5 \# 8 \% 2 \% 2

Evaluating by syntax-directed translation:

  • Subtree for the first operand of #:

    T1=20T_1 = 20
    S1=T1=20S_1 = T_1 = 20
  • Subtree for the second operand of # (10%510 \% 5):

    T2=10÷5=2T_2 = 10 \div 5 = 2
  • Evaluate the first # (left-associative):

    S2=S1∗T2=20∗2=40S_2 = S_1 * T_2 = 20 * 2 = 40
  • Subtree for the third operand (8%2%28 \% 2 \% 2, left-associative division):

    T3=(8÷2)÷2=4÷2=2T_3 = (8 \div 2) \div 2 = 4 \div 2 = 2
  • Evaluate the second # at the root:

    S∙val=S2∗T3=40∗2=80S_{\bullet val} = S_2 * T_3 = 40 * 2 = 80

Thus, the computed value at the root is 80.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Syntax-Directed Translation previous year question is Q65 of the GATE CSE 2022 paper. Find more GATE CSE PYQs on Syntax-Directed Translation in Compiler Design, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Syntax-Directed Translation PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Syntax-Directed Translation in GATE CSE

Syntax-Directed Translation has 9 GATE CSE PYQs across 9 papers from 2016 to 2026. 2 of them are 2-mark NAT questions like this one, and it is the only one in GATE CSE 2022.

How GATE tests Syntax-Directed Translation

More from GATE CSE 2022

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2022 · Q65 · NAT

Consider the following grammar along with translation rules. ll S → S1 \# T & {S val = S1 val * T val} S → T & {S val = T val} T → T1 \% R & {T val = T1 val R val} T → R & {T val = R val} R → id & {R val = id val} Here # and % are operators and id is a token that represents an integer and id val represents the corresponding integer value. The set of non-terminals is {S, T, R, P} and a subscripted non-terminal indicates an instance of the non-terminal. Using this translation scheme, the computed value of S val for root of the parse tree for the expression 20\#10\%5\#8\%2\%2 is …

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.