chapter

    Syntax-directed Translation and Intermediate Representations Pyqs for Gate Cs

    Solve 9+ Syntax-directed Translation and Intermediate Representations previous year questions for GATE CS with answers and detailed solutions. Free sample que

    Try a question

    Answer it here to see how it works. Nothing is recorded until you sign in.

    Question 1
    2026 Slot Set1 PYQ
    Level 3: Exam Standard
    Consider the following two syntax-directed definitions SDD1 and SDD2 for type declarations.

    SDD1SDD2
    Grammar
    (G1)
    Semantic RulesGrammar
    (G2)
    Semantic Rules






    is the start symbol, and , and are the three terminals. The non-terminal is the same as and the non-terminal is the same as . Here, the subscript is used to differentiate the grammar symbols on the two sides of a production. The function updates the symbol table with the type information for an identifier.

    Let P and Q be the languages specified by grammars G1 and G2, respectively.

    Which of the following statements is/are true?
    Question 2
    2025 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider the following statements about the use of backpatching in a compiler for intermediate code generation:

    (I) Backpatching can be used to generate code for Boolean expression in one pass.
    (II) Backpatching can be used to generate code for flow-of-control statements in one pass.

    Which ONE of the following options is CORRECT?
    Question 3
    2025 Slot Set2 PYQ
    Level 3: Exam Standard
    Given the following syntax directed translation rules:

    Rule 1:
    Rule 2:
    Rule 3:

    Which ONE is the CORRECT option among the following?
    Question 4
    2024 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider the following expression: . The following sequence shows the list of triples representing the given expression, with entries missing for triples (1), (3), and (6).

    (0)+pr(1)(2)uminus(1)(3)(4)/uw(5)+(3)(4)(6)(7)=(6)(5)

    Which one of the following options fills in the missing entries CORRECTLY?
    Question 5
    2024 Slot Set2 PYQ
    Level 3: Exam Standard

    Which of the following statements is/are FALSE?

    Question 6
    2024 Slot Set1 PYQ
    Level 3: Exam Standard
    Consider the following syntax-directed definition (SDD).

      
      
      
      
      
      
      
      

    Given "MMLK" as the input, which one of the following options is the CORRECT value computed by the SDD (in the attribute )?
    Question 7
    2023 PYQ
    Level 3: Exam Standard
    Consider the syntax directed translation given by the following grammar and semantic rules. Here , , and are non-terminals. is the starting non-terminal, and #, 0 and 1 are lexical tokens corresponding to input letters “#”, “0” and “1”, respectively. denotes the synthesized attribute (a numeric value) associated with a non-terminal . and denote occurrences of and on the right hand side of a production, respectively. For the tokens 0 and 1, and .


    The value computed by the translation scheme for the input string


    is __________. (Rounded off to three decimal places)
    Question 8
    2022 PYQ
    Level 3: Exam Standard
    Consider the following grammar along with translation rules. Here # and % are operators and is a token that represents an integer and 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 for root of the parse tree for the expression is_____________.
    Question 9
    2021 Slot Set2 PYQ
    Level 3: Exam Standard

    In the context of compilers, which of the following is/are NOT an intermediate representation of the source program?

    Free preview ends here

    Login to view the complete previous-year questions and solutions

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Explore complete test series, official past papers, and chapter-wise study tools.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Syntax-directed Translation and Intermediate Representations PYQs for GATE CS

    Solve 9+ Syntax-directed Translation and Intermediate Representations previous year questions for GATE CS with answers and detailed solutions. Free sample questions below.

    Chapter Roadmap: Syntax-Directed Translation and IR

    Chapter Roadmap

    Your visual journey through the compiler's middle-end.

    Step 1: Syntax-Directed Definitions
    The Heavyweight (High Exam Yield)
    Attaching semantic rules to grammar. Inherited vs. Synthesized attributes. Dependency graphs.
    Step 2: Backpatching & Control Flow
    The Current Topic (Core Logic)
    Translating boolean logic and control structures (if, while) into Three-Address Code in a single pass.
    Step 3: Intermediate Representations
    The Formats (Direct Applications)
    Quadruples, Triples, and Indirect Triples. How the code is actually stored in memory.
    What you will master: Tracing how a high-level while loop is parsed and converted into low-level jump instructions without a second pass.

    The Forward Reference Problem in One-Pass Compilation

    The Forward Reference Problem

    When generating code for control structures like if or while, the compiler faces a dilemma in a single pass:

    if (x < y) {
      // ... 50 lines of code ...
    }
    // Target Line Unknown!

    The compiler evaluates x < y and needs to emit a jump for the false condition. But the target address (after the 50 lines) hasn't been generated yet.

    Two-Pass Compilation

    Pass 1 records block ends. Pass 2 generates code. Simple but slow and memory-heavy.

    Backpatching (One-Pass)

    Emit goto _ with a blank target. Record the location. "Patch" the address when the target is finally reached.

    Syntax-directed Translation and Intermediate Representations: Solved Questions with Step-by-Step Explanations (9 Problems)

    Question 1 · Compiler Design · 2026_Set1 MSQ
    Consider the following two syntax-directed definitions SDD1 and SDD2 for type declarations.

    SDD1SDD2
    Grammar
    (G1)
    Semantic RulesGrammar
    (G2)
    Semantic Rules






    is the start symbol, and , and are the three terminals. The non-terminal is the same as and the non-terminal is the same as . Here, the subscript is used to differentiate the grammar symbols on the two sides of a production. The function updates the symbol table with the type information for an identifier.

    Let P and Q be the languages specified by grammars G1 and G2, respectively.

    Which of the following statements is/are true?
    1. A.

      The languages P and Q are the same

    2. B.

      SDD2 is S-attributed and contains only synthesized attributes

    3. C.

      SDD1 is L-attributed and contains only inherited attributes

    4. D.

      The specifications of SDD1 and SDD2 are such that the same entries get added to the symbol table

    Correct Answer:

    ["A","B","D"]

    Step-by-Step Solution

    Insight: This is an attribute classification and language equivalence question. The key is to check each SDD for synthesized vs. inherited attributes and trace the symbol table updates.

    Exam route: Evaluate each statement against the definitions of S-attributed and L-attributed, and manually trace a sample string like "int id1 id2".

    Learning route:

    Statement A: "The languages P and Q are the same."

    G1 generates: D → T V → int V → int V1 id → int id id.

    G2 generates: D → D1 id → T id id → int id id.

    Both generate a type followed by one or more identifiers. The languages are identical. TRUE.

    Statement B: "SDD2 is S-attributed and contains only synthesized attributes."

    In SDD2, D.type depends on D1.type or T.type (children). T.type depends on terminals. All attributes are synthesized. TRUE.

    Statement C: "SDD1 is L-attributed and contains only inherited attributes."

    In SDD1, V.type is inherited (from T), and V1.type is inherited (from V). However, T.type and D.type are synthesized. Since it contains synthesized attributes, "only inherited" is FALSE.

    Statement D: "The specifications of SDD1 and SDD2 are such that the same entries get added to the symbol table."

    For "int a b":

    SDD1: V.type = int. V1.type = int. put(b.entry, int), then put(a.entry, int).

    SDD2: D1.type = int. put(a.entry, int). Then put(b.entry, int).

    The same entries (a:int, b:int) are added. TRUE.

    Verification: Re-checking SDD1 confirms T.type is synthesized, invalidating Statement C. Statements A, B, and D hold.

    Question 2 · Compiler Design · 2025_Set2 MCQ
    Consider the following statements about the use of backpatching in a compiler for intermediate code generation:

    (I) Backpatching can be used to generate code for Boolean expression in one pass.
    (II) Backpatching can be used to generate code for flow-of-control statements in one pass.

    Which ONE of the following options is CORRECT?
    1. A.

      Only (I) is correct.

    2. B.

      Only (II) is correct.

    3. C.

      Both (I) and (II) are correct.

    4. D.

      Neither (I) nor (II) is correct.

    Correct Answer:

    C

    Step-by-Step Solution

    Insight: This is a conceptual question about the purpose of backpatching in one-pass compilers.

    Exam route: Recall that backpatching was invented specifically to resolve forward references in a single pass for both boolean expressions and control flow.

    Learning route:

    Statement (I): "Backpatching can be used to generate code for Boolean expression in one pass."

    When translating , the compiler emits jumps with blank targets for . These are stored in lists (truelist/falselist) and patched later when the target is known. This allows one-pass generation. TRUE.

    Statement (II): "Backpatching can be used to generate code for flow-of-control statements in one pass."

    For if (B) S, the false jumps of must skip . The target is unknown when is parsed. Backpatching records these jumps and fills the target address once is fully translated. TRUE.

    Verification: Both statements describe the exact primary use cases of backpatching as defined in standard compiler design literature (e.g., Dragon Book).

    Question 3 · Compiler Design · 2025_Set2 MCQ
    Given the following syntax directed translation rules:

    Rule 1:
    Rule 2:
    Rule 3:

    Which ONE is the CORRECT option among the following?
    1. A.

      Rule 1 is S-attributed and L-attributed; Rule 2 is S-attributed and not L-attributed; Rule 3 is neither S-attributed nor L-attributed

    2. B.

      Rule 1 is neither S-attributed nor L-attributed; Rule 2 is S-attributed and L-attributed; Rule 3 is S-attributed and L-attributed

    3. C.

      Rule 1 is neither S-attributed nor L-attributed; Rule 2 is not S-attributed and is L-attributed; Rule 3 is S-attributed and L-attributed

    4. D.

      Rule 1 is S-attributed and not L-attributed; Rule 2 is not S-attributed and is L-attributed; Rule 3 is S-attributed and L-attributed

    Correct Answer:

    C

    Step-by-Step Solution

    Insight: This is an attribute classification question. The key is checking each rule's semantic dependencies against the definitions of S-attributed and L-attributed.

    Exam route: For each rule, check if attributes are synthesized (depend on children) or inherited (depend on parent/siblings), then verify L-attributed constraints.

    Learning route:

    Rule 1: R → AB {B.i = R.i - 1; A.i = B.i; R.i = A.i + 1;}

    • B.i depends on R.i (parent) — OK for L-attributed
    • A.i depends on B.i (right sibling!) — VIOLATES L-attributed (inherited attributes cannot depend on right siblings)
    • R.i depends on A.i (child) — synthesized
    • Since A.i depends on right sibling B, Rule 1 is NEITHER S-attributed nor L-attributed.

    Rule 2: P → CD {P.i = C.i + D.i; D.i = C.i + 2;}

    • P.i depends on C.i and D.i (children) — synthesized
    • D.i depends on C.i (left sibling) — OK for L-attributed
    • Has both synthesized (P.i) and inherited (D.i) attributes, so NOT S-attributed
    • D.i depends only on left sibling C.i, so it IS L-attributed
    • Rule 2 is L-attributed but not S-attributed.

    Rule 3: Q → EF {Q.i = E.i + F.i;}

    • Q.i depends on E.i and F.i (children) — synthesized
    • No inherited attributes
    • Rule 3 is S-attributed (and therefore also L-attributed, since S-attributed is a subset).

    Matching with options: Rule 1 is neither; Rule 2 is not S-attributed and is L-attributed; Rule 3 is S-attributed and L-attributed. This matches option C.

    Verification: Re-check Rule 1: A.i = B.i where B is to the right of A in the production R → AB. This is a right-sibling dependency, violating L-attributed. Correct.

    Question 4 · Compiler Design · 2024_Set2 MCQ
    Consider the following expression: . The following sequence shows the list of triples representing the given expression, with entries missing for triples (1), (3), and (6).

    (0)+pr(1)(2)uminus(1)(3)(4)/uw(5)+(3)(4)(6)(7)=(6)(5)

    Which one of the following options fills in the missing entries CORRECTLY?
    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    A

    Step-by-Step Solution

    Insight: This is an intermediate code generation question focusing on triple representation, specifically handling array accesses and unary operators.

    Exam route: Decompose the expression by precedence, assign indices to each operation, and use the correct array operators (=[] for load, []= for store).

    Learning route:

    Expression:

    Step 1: Compute .

    (0) + p r

    Step 2: Load . Array load uses =[] with array and index as arguments.

    (1) =[] s i

    Step 3: Apply unary minus to the result of (1).

    (2) uminus (1)

    Step 4: Multiply the results of (0) and (2).

    (3) * (0) (2)

    Step 5: Compute .

    (4) / u w

    Step 6: Add the results of (3) and (4).

    (5) + (3) (4)

    Step 7: Store the result of (5) into . Array store uses []= with array and index as arguments. The value to store is implicitly the result of the preceding computation or explicitly handled in the assignment step. In standard triple notation for this GATE question, the store instruction references the array and index, and the final assignment = links the store location to the value.

    (6) []= x i

    (7) = (6) (5)

    Matching with options:

    Option A has (1) =[] s i, (3) * (0) (2), (6) []= x i. This perfectly matches our derivation.

    Verification: Re-check array operators. =[] is load (value = array[index]), []= is store (array[index] = value). Option A uses them correctly.

    Question 5 · Compiler Design · 2024_Set2 MSQ

    Which of the following statements is/are FALSE?

    1. A.

      An attribute grammar is a syntax-directed definition (SDD) in which the functions in the semantic rules have no side effects

    2. B.

      The attributes in a L-attributed definition cannot always be evaluated in a depth-first order

    3. C.

      Synthesized attributes can be evaluated by a bottom-up parser as the input is parsed

    4. D.

      All L-attributed definitions based on LR(1) grammar can be evaluated using a bottom-up parsing strategy

    Correct Answer:

    ["B","D"]

    Step-by-Step Solution

    Insight: This is a classification question on attribute evaluation strategies. The key is matching each statement against the precise definitions of S-attributed, L-attributed, and attribute grammars.

    Exam route: Evaluate each statement against the textbook definitions.

    Learning route:

    Statement A: "An attribute grammar is an SDD with no side effects in semantic rules." This is the standard definition of an attribute grammar — semantic rules are pure functions. TRUE.

    Statement B: "L-attributed attributes cannot always be evaluated in depth-first order." This is FALSE. The defining property of L-attributed definitions is precisely that inherited attributes depend only on the parent and left siblings, which guarantees they CAN be evaluated in a left-to-right depth-first traversal. That's the entire motivation for the L-attributed restriction.

    Statement C: "Synthesized attributes can be evaluated by a bottom-up parser as input is parsed." TRUE. Synthesized attributes depend only on children, which are fully reduced before the parent in bottom-up parsing. Evaluation happens naturally at reduction time.

    Statement D: "All L-attributed definitions based on LR(1) grammar can be evaluated using bottom-up parsing." FALSE. While L-attributed definitions are compatible with top-down parsing, bottom-up parsing presents a fundamental tension: when reducing A → X₁X₂...Xₙ, the inherited attributes of Xᵢ may depend on A's attributes, which aren't available until A is reduced. Handling this requires grammar transformation or embedded actions, and not all L-attributed definitions can be directly evaluated during pure bottom-up parsing without such modifications.

    Verification: Statements B and D contradict the standard evaluation theorems for attribute grammars.

    Question 6 · Compiler Design · 2024_Set1 MCQ
    Consider the following syntax-directed definition (SDD).

      
      
      
      
      
      
      
      

    Given "MMLK" as the input, which one of the following options is the CORRECT value computed by the SDD (in the attribute )?
    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    A

    Step-by-Step Solution

    Insight: This is a synthesized attribute evaluation question with recursive productions. The key is parsing the input and computing attributes bottom-up.

    Exam route: Parse "MMLK", build the parse tree, evaluate attributes from leaves to root.

    Learning route:

    Input: "MMLK"

    SDD:

    • S → DHTU {S.val = D.val + H.val + T.val + U.val}
    • D → "M" D₁ {D.val = 5 + D₁.val}
    • D → ε {D.val = -5}
    • H → "L" H₁ {H.val = 5·10 + H₁.val}
    • H → ε {H.val = -10}
    • T → "C" T₁ {T.val = 5·100 + T₁.val}
    • T → ε {T.val = -5}
    • U → "K" {U.val = 5}

    Parse tree:

    S → D H T U

    D derives "MM": D → "M" D₁ where D₁ → "M" D₂, D₂ → ε

    H derives "L": H → "L" H₁ where H₁ → ε

    T derives ε: T → ε

    U derives "K": U → "K"

    Evaluation (bottom-up):

    D₂ → ε: D₂.val = -5

    D₁ → "M" D₂: D₁.val = 5 + D₂.val = 5 + (-5) = 0

    D → "M" D₁: D.val = 5 + D₁.val = 5 + 0 = 5

    H₁ → ε: H₁.val = -10

    H → "L" H₁: H.val = 5·10 + H₁.val = 50 + (-10) = 40

    T → ε: T.val = -5

    U → "K": U.val = 5

    S → DHTU: S.val = D.val + H.val + T.val + U.val = 5 + 40 + (-5) + 5 = 45

    Verification: Re-trace. D computes 5 + (5 + (-5)) = 5. H computes 50 + (-10) = 40. T is -5. U is 5. Sum: 5 + 40 - 5 + 5 = 45. Correct.

    Question 7 · Compiler Design · 2023 NAT
    Consider the syntax directed translation given by the following grammar and semantic rules. Here , , and are non-terminals. is the starting non-terminal, and #, 0 and 1 are lexical tokens corresponding to input letters “#”, “0” and “1”, respectively. denotes the synthesized attribute (a numeric value) associated with a non-terminal . and denote occurrences of and on the right hand side of a production, respectively. For the tokens 0 and 1, and .


    The value computed by the translation scheme for the input string


    is __________. (Rounded off to three decimal places)
    Correct Answer:

    2.38

    Step-by-Step Solution

    Insight: This is a synthesized attribute evaluation question. The key is building the parse tree and computing attributes bottom-up following the semantic rules.

    Exam route: Parse the input string, build the parse tree, then evaluate attributes from leaves to root.

    Learning route:

    Input: 10#011

    Grammar:

    • N → I#F {N.val = I.val + F.val}
    • I → I₁B {I.val = 2·I₁.val + B.val}
    • I → B {I.val = B.val}
    • F → BF₁ {F.val = ½(B.val + F₁.val)}
    • F → B {F.val = ½·B.val}
    • B → 0 {B.val = 0}
    • B → 1 {B.val = 1}

    Parse tree:

    N → I # F

    I derives "10": I → I₁B where I₁ → B(1), B → 0

    F derives "011": F → BF₁ where B → 0, F₁ → BF₂ where B → 1, F₂ → B(1)

    Evaluation (bottom-up):

    • B → 1: B.val = 1
    • I₁ → B: I₁.val = B.val = 1
    • B → 0: B.val = 0
    • I → I₁B: I.val = 2·I₁.val + B.val = 2·1 + 0 = 2
    • B → 1: B.val = 1
    • F₂ → B: F₂.val = ½·B.val = ½·1 = 0.5
    • B → 1: B.val = 1
    • F₁ → BF₂: F₁.val = ½(B.val + F₂.val) = ½(1 + 0.5) = 0.75
    • B → 0: B.val = 0
    • F → BF₁: F.val = ½(B.val + F₁.val) = ½(0 + 0.75) = 0.375
    • N → I#F: N.val = I.val + F.val = 2 + 0.375 = 2.375

    Rounded to three decimal places: 2.375

    Verification: Re-trace the computation. I computes the integer part "10" in binary: 1·2 + 0 = 2. F computes the fractional part "011": 0/2 + 1/4 + 1/8 = 0 + 0.25 + 0.125 = 0.375. Total: 2.375. Correct.

    Question 8 · Compiler Design · 2022 NAT
    Consider the following grammar along with translation rules. Here # and % are operators and is a token that represents an integer and 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 for root of the parse tree for the expression is_____________.
    Correct Answer:

    80.00

    Step-by-Step Solution

    Insight: This is a synthesized attribute evaluation question with left-recursive grammar. The key is to build the unique parse tree dictated by the grammar and evaluate bottom-up.

    Exam route: Parse the string using the grammar's associativity rules, then compute values from leaves to root.

    Learning route:

    Grammar:

    • {S.val = S1.val * T.val} (Left-associative for #)
    • {S.val = T.val}
    • {T.val = T1.val / R.val} (Left-associative for %)
    • {T.val = R.val}
    • {R.val = id.val}

    Input: 20 # 10 % 5 # 8 % 2 % 2

    Since # is only produced by , and % is only produced by , the # operators must be at higher levels in the parse tree than % operators. The string is uniquely parsed as:

    Where derives "20 # 10 % 5" and derives "8 % 2 % 2".

    Further parsing of :

    (val = 20)

    (val = 10 / 5 = 2)

    .

    Further parsing of (right side):

    (val = 8 / 2 = 4)

    (val = 2)

    .

    Final computation:

    .

    Verification: The grammar enforces left associativity for both # and %. The parse tree is unique. The arithmetic is straightforward: ((20 (10/5)) ((8/2)/2)) = (20 2) (4/2) = 40 * 2 = 80.

    Question 9 · Compiler Design · 2021_Set2 MSQ

    In the context of compilers, which of the following is/are NOT an intermediate representation of the source program?

    1. A.

      Three address code

    2. B.

      Abstract Syntax Tree (AST)

    3. C.

      Control Flow Graph (CFG)

    4. D.

      Symbol table

    Correct Answer:

    ["D"]

    Step-by-Step Solution

    Insight: This is a classification question on intermediate representations. The key is understanding what constitutes an IR versus other compiler data structures.

    Exam route: Check each option against the definition of intermediate representation.

    Learning route:

    An intermediate representation (IR) is a data structure that captures the computational meaning of the source program — it encodes operations and data flow.

    Option A: Three-address code — This is a classic IR. Each instruction has at most three operands, representing operations in a structured way. IS an IR.

    Option B: Abstract Syntax Tree (AST) — This is a tree representation of the syntactic structure of the source code, capturing the hierarchical relationships and operations. IS an IR.

    Option C: Control Flow Graph (CFG) — This represents the flow of control between basic blocks in the program. It captures the computational structure (which operations execute in what order). IS an IR.

    Option D: Symbol table — This stores metadata about identifiers (names, types, scopes, memory locations) but does NOT represent the computation itself. It's a supporting data structure, not an IR. NOT an IR.

    The question asks which is NOT an IR. Only the symbol table fails the test.

    Verification: Re-check the definition. Does each structure explicitly encode operations and data flow? Three-address code: yes. AST: yes. CFG: yes. Symbol table: no (only metadata). Correct.

    More previous year questions (pyqs) in this unit