GATE CS 2022 Set 1 — Question 65
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.NAT+2 / -0HardSyntax-Directed TranslationSemantic AnalysisCompiler Design
Compiler Design → Semantic Analysis → Syntax-Directed Translation
Last updated
Question
Consider the following grammar along with translation rules.
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 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 __________.Correct answer
80 to 80
Solution
The given grammar defines the precedence and associativity of the operators # and %.Grammar:
The expression to evaluate is
1.
2.
S → T3.
4.
T → R5.
Translation Rules:- For rule 1:
- For rule 3: (integer division)
- Precedence: Since the production for involves (i.e., can derive a string of s separated by #), and can derive a string of s separated by %, the operator % has higher precedence than the operator #.
- Associativity: The productions and are left-recursive. This means both operators # and % are left-associative.
The expression to evaluate is
20#10%5#8%2%2.Based on the precedence and associativity rules derived from the grammar, we can parenthesize the expression to show the order of evaluation:Expression: ( (20 # (10 % 5)) # ((8 % 2) % 2) )Let's evaluate the sub-expressions step-by-step according to the translation rules:1.Evaluate the innermost parentheses first.
-
(10 % 5): This corresponds to the production . The translation rule is . Here, is 10 and is 5. So, the value is . -
(8 % 2): Similarly, this evaluates to .
( (20 # 2) # (4 % 2) )3.Evaluate the next level of parentheses.
-
(4 % 2): This evaluates to .
( (20 # 2) # 2 )5.Now, evaluate the remaining expression from left to right due to left-associativity of #.
-
(20 # 2): This corresponds to the production . The translation rule is . Here, is 20 and is 2. So, the value is .
(40 # 2)7.Finally, evaluate the last operation.
-
(40 # 2): This evaluates to .
Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on Semantic Analysis
2026 Set 2 Q17In C runtime environment, which one of the following is stored in heap?2026 Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] **P3**…2026 Set 1 Q27Consider the following C statements: Which of the following options is/are correct? [figure]2026 Set 1 Q28Which of the following statements is/are true?2026 Set 2 Q35A lexical analyzer uses the following token definitions - - …