chapter

    Syntax-directed Translation and Intermediate Representations Notes for Gate Cs

    Syntax-directed Translation and Intermediate Representations notes for GATE CS: 49 study cards covering concepts, formulas, shortcuts and exam traps, plus sol

    Syntax Directed Translation and Intermediate Representations notes

    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.

    The Backpatching Toolkit: Functions and Lists

    The Backpatching Toolkit

    To manage unresolved jumps, we use three core functions operating on linked lists of instruction indices.

    Function Signature Action
    makelist makelist(i) Creates a new list containing only index i. Returns pointer.
    merge merge(p1, p2) Concatenates lists p1 and p2. Returns pointer to combined list.
    backpatch backpatch(p, i) Fills blank target field of every instruction in list p with address i.
    Synthesized Attributes:
    B.truelist Jumps to execute when is true.
    B.falselist Jumps to execute when is false.
    S.nextlist Jumps to code immediately following statement .

    46 more cards in this chapter

    Try a question

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

    Question 1
    Level 1: Warm-up

    Which of the following statements is strictly TRUE regarding the attribute dependency graph for a specific parse tree in a syntax-directed definition?

    Question 2
    Level 1: Warm-up

    A syntax-directed definition contains exactly 2 semantic rules where an inherited attribute depends on a right sibling. What is the minimum number of these specific rules that must be modified to guarantee the definition is strictly L-attributed?

    Question 3
    Level 1: Warm-up

    Assertion (A): A syntax-directed definition is classified as L-attributed if every inherited attribute depends only on the parent's attributes or the left siblings' attributes.

    Reason (R): This left-to-right dependency restriction ensures that attributes can be evaluated during a single depth-first, left-to-right traversal of the parse tree.

    Question 4
    Level 1: Warm-up

    When translating the arithmetic expression into strict triple format using 0-based indexing for the instruction sequence, what is the maximum instruction index assigned to any triple in the generated sequence?

    Question 5
    Level 1: Warm-up

    Which of the following statements is strictly TRUE regarding the triple representation of array accesses in intermediate code?

    Question 6
    Level 1: Warm-up

    Consider the grammar production . How many of the following inherited attribute dependencies are strictly VALID in an L-attributed definition?

    1. depends on
    2. depends on
    3. depends on
    4. depends on
    Question 7
    Level 1: Warm-up

    Match the syntax-directed definition type with its correct classification rule. (Select all correct matches)

    Question 8
    Level 1: Warm-up

    Assertion (A): A valid evaluation order for the attributes in a syntax-directed definition can be determined by finding a topological sort of the attribute dependency graph.

    Reason (R): In a topological sort, every node appears after all the nodes it depends on, ensuring values are computed before they are needed.

    Question 9
    Level 1: Warm-up

    Which of the following statements is strictly TRUE regarding the evaluation of attributes using depth-first traversal without explicitly building the dependency graph?

    Question 10
    Level 1: Warm-up

    Consider an L-attributed definition with the production .

    The inherited attribute of depends on the inherited attribute of .

    The inherited attribute of depends on the inherited attribute of .

    Rank the following evaluation steps from first to last:

    1. Evaluate
    2. Evaluate
    3. Evaluate

    Select the correct ranking sequence.

    Free preview ends here

    Login to view the complete notes

    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 Notes for GATE CS

    Syntax-directed Translation and Intermediate Representations notes for GATE CS: 49 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    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.

    The Backpatching Toolkit: Functions and Lists

    The Backpatching Toolkit

    To manage unresolved jumps, we use three core functions operating on linked lists of instruction indices.

    Function Signature Action
    makelist makelist(i) Creates a new list containing only index i. Returns pointer.
    merge merge(p1, p2) Concatenates lists p1 and p2. Returns pointer to combined list.
    backpatch backpatch(p, i) Fills blank target field of every instruction in list p with address i.
    Synthesized Attributes:
    B.truelist Jumps to execute when is true.
    B.falselist Jumps to execute when is false.
    S.nextlist Jumps to code immediately following statement .

    Translating Boolean Expressions

    Translating Basic Boolean Expressions

    For a relational expression , we generate two branch instructions with blank targets.

    B.truelist = makelist(nextquad)
    B.falselist = makelist(nextquad + 1)
    emit('if' E1.addr '<' E2.addr 'goto _')
    emit('goto _')

    Visualizing the TAC generated:

    Index Instruction Status
    100 if a < b goto _ Added to truelist
    101 goto _ Added to falselist

    The actual patching happens later when this Boolean expression is used inside a control structure.

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

    Question 1 · Compiler Design MCQ

    Which of the following statements is strictly TRUE regarding the attribute dependency graph for a specific parse tree in a syntax-directed definition?

    1. A.

      It is constructed statically for the grammar, independent of the specific parse tree.

    2. B.

      A directed edge from node X to node Y indicates that the semantic rule for Y requires the value of X.

    3. C.

      It is permitted to contain cycles as long as the grammar is L-attributed.

    4. D.

      It only maps dependencies for synthesized attributes, ignoring inherited attributes.

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: The dependency graph is dynamic (per parse tree) and must be acyclic to allow valid evaluation.

    Step 1: The graph is constructed for each specific parse tree, not statically for the grammar (eliminates option A).

    Step 2: A directed edge from X to Y means Y depends on X, so X must be evaluated before Y. This matches option B.

    Step 3: The graph must never contain cycles; a cycle means the attributes cannot be evaluated (eliminates option C).

    Step 4: The graph maps dependencies for both synthesized and inherited attributes (eliminates option D).

    Answer: A directed edge from node X to node Y indicates that the semantic rule for Y requires the value of X.

    Question 2 · Compiler Design MCQ

    A syntax-directed definition contains exactly 2 semantic rules where an inherited attribute depends on a right sibling. What is the minimum number of these specific rules that must be modified to guarantee the definition is strictly L-attributed?

    1. A.

      0

    2. B.

      1

    3. C.

      3

    4. D.

      2

    Correct Answer:

    D

    Step-by-Step Solution

    Key idea: An L-attributed definition strictly prohibits any inherited attribute from depending on a right sibling.

    Step 1: Identify the violation. The definition has 2 rules where an inherited attribute depends on a right sibling.

    Step 2: Recall the L-attributed constraint. No inherited attribute can depend on a right sibling.

    Step 3: To make the definition strictly L-attributed, every single violating rule must be changed.

    Step 4: Therefore, all 2 of these specific rules must be modified.

    Answer: 2

    Question 3 · Compiler Design MCQ

    Assertion (A): A syntax-directed definition is classified as L-attributed if every inherited attribute depends only on the parent's attributes or the left siblings' attributes.

    Reason (R): This left-to-right dependency restriction ensures that attributes can be evaluated during a single depth-first, left-to-right traversal of the parse tree.

    1. A.

      Both A and R are true, and R is the correct explanation of A.

    2. B.

      Both A and R are true, but R is NOT the correct explanation of A.

    3. C.

      A is true, but R is false.

    4. D.

      A is false, but R is true.

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: The definition of L-attributed is intrinsically tied to its evaluability via left-to-right traversal.

    Step 1: Evaluate Assertion (A). By definition, an L-attributed definition restricts inherited attributes to depend only on the parent or left siblings. This is true.

    Step 2: Evaluate Reason (R). A left-to-right dependency restriction is exactly what allows a depth-first, left-to-right traversal to encounter all dependencies before they are needed. This is true.

    Step 3: Check the link. Reason (R) is the fundamental justification for why the restriction in (A) is defined that way and why it is useful. Therefore, R is the correct explanation of A.

    Answer: Both A and R are true, and R is the correct explanation of A.

    Question 4 · Compiler Design MCQ

    When translating the arithmetic expression into strict triple format using 0-based indexing for the instruction sequence, what is the maximum instruction index assigned to any triple in the generated sequence?

    1. A.

      3

    2. B.

      2

    3. C.

      1

    4. D.

      4

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: Triples use 0-based instruction indices to reference intermediate results, and every computational or assignment step receives a new index.

    Step 1: Determine the evaluation order based on operator precedence. Multiplication () occurs before addition (), followed by the final assignment to .

    Step 2: Generate the triples sequentially, assigning indices starting from 0.

    • Index (0): Compute . Triple: (0) * a b
    • Index (1): Compute . Triple: (1) + (0) c
    • Index (2): Assign the result to . Triple: (2) = w (1)

    Step 3: Identify the maximum index. The sequence of indices used is 0, 1, and 2. The maximum index assigned is 2.

    Answer: B

    Question 5 · Compiler Design MCQ

    Which of the following statements is strictly TRUE regarding the triple representation of array accesses in intermediate code?

    1. A.

      The load operator =[] takes the index as its first argument and the array name as its second.

    2. B.

      The store operator []= takes the array name, index, and value as its three arguments in that exact order.

    3. C.

      Array accesses cannot be represented in triples and must be converted to quadruples.

    4. D.

      The result of a load operation =[] A i is referenced in subsequent triples by the array name .

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: Array accesses in triples require specialized operators to denote the direction of data flow, and the order of arguments is strictly enforced.

    Step 1: Evaluate Option A. The load operator =[] retrieves a value from an array. The strict argument order is array name first, then index (e.g., =[] A i). Option A reverses this, so it is false.

    Step 2: Evaluate Option B. The store operator []= writes a value into an array. It requires three arguments: the array name, the index, and the value to be stored (e.g., []= A i val). This statement is true.

    Step 3: Evaluate Option C. Array accesses can absolutely be represented in triples using the specialized =[] and []= operators. They do not require conversion to quadruples. Option C is false.

    Step 4: Evaluate Option D. The result of a load operation is an intermediate value, which is referenced in subsequent triples by its instruction index (e.g., (0)), not by the array name. Option D is false.

    Answer: B

    Question 6 · Compiler Design MCQ

    Consider the grammar production . How many of the following inherited attribute dependencies are strictly VALID in an L-attributed definition?

    1. depends on
    2. depends on
    3. depends on
    4. depends on
    1. A.

      1

    2. B.

      2

    3. C.

      3

    4. D.

      4

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: In an L-attributed definition, an inherited attribute of a symbol can only depend on the parent's attributes or the left siblings' attributes.

    Step 1: Analyze dependency 1: depends on . is the left sibling of . This is VALID.

    Step 2: Analyze dependency 2: depends on . is the left sibling of . This is VALID.

    Step 3: Analyze dependency 3: depends on . is the parent of . This is VALID.

    Step 4: Analyze dependency 4: depends on . is the right sibling of . L-attributed definitions strictly forbid dependencies on right siblings. This is INVALID.

    Answer: Exactly 3 dependencies (1, 2, and 3) are valid.

    Question 7 · Compiler Design MSQ

    Match the syntax-directed definition type with its correct classification rule. (Select all correct matches)

    1. A.

      S-attributed: Uses exclusively synthesized attributes.

    2. B.

      L-attributed: Inherited attributes depend only on parent or left siblings.

    3. C.

      S-attributed: Allows inherited attributes that depend on right siblings.

    4. D.

      L-attributed: Allows inherited attributes to depend on child's synthesized attributes.

    Correct Answer:

    ["A","B"]

    Step-by-Step Solution

    Key idea: S-attributed and L-attributed definitions are classified by the direction and scope of their attribute dependencies.

    Step 1: S-attributed definitions use only synthesized attributes. There are no inherited attributes. This matches Option A.

    Step 2: L-attributed definitions allow inherited attributes, but with a strict boundary: they can only depend on the parent's attributes or the left siblings' attributes. This matches Option B.

    Step 3: Options C and D violate the fundamental constraints of S-attributed (no inherited allowed) and L-attributed (no right sibling or child dependencies) definitions.

    Answer: Options A and B are correct matches.

    Question 8 · Compiler Design MCQ

    Assertion (A): A valid evaluation order for the attributes in a syntax-directed definition can be determined by finding a topological sort of the attribute dependency graph.

    Reason (R): In a topological sort, every node appears after all the nodes it depends on, ensuring values are computed before they are needed.

    1. A.

      Both A and R are true, and R is the correct explanation of A.

    2. B.

      Both A and R are true, but R is NOT the correct explanation of A.

    3. C.

      A is true, but R is false.

    4. D.

      A is false, but R is true.

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: Topological sorting is the standard graph-theoretic method for resolving dependencies.

    Step 1: Assertion (A) is true. A topological sort of the dependency graph provides a valid linear evaluation order.

    Step 2: Reason (R) is true. The definition of a topological sort is that for every directed edge U -> V, U comes before V in the ordering.

    Step 3: Reason (R) correctly explains why (A) works: because dependencies are satisfied before a node is evaluated, the order is valid.

    Answer: Both A and R are true, and R is the correct explanation of A.

    Question 9 · Compiler Design MCQ

    Which of the following statements is strictly TRUE regarding the evaluation of attributes using depth-first traversal without explicitly building the dependency graph?

    1. A.

      It requires the dependency graph to contain at least one cycle.

    2. B.

      It is only possible if all attributes are inherited.

    3. C.

      It is always possible for any syntax-directed definition.

    4. D.

      It is guaranteed to work for L-attributed definitions due to left-to-right dependency restrictions.

    Correct Answer:

    D

    Step-by-Step Solution

    Key idea: Depth-first traversal can implicitly evaluate attributes without building the graph, but only if the definition guarantees a valid evaluation order (like S-attributed or L-attributed).

    Step 1: Evaluate option A. A cycle makes evaluation impossible, so this is false.

    Step 2: Evaluate option B. Depth-first traversal works perfectly for S-attributed definitions (all synthesized), so it is not "only possible" for inherited.

    Step 3: Evaluate option C. It is not always possible for ANY definition; arbitrary definitions might have right-to-left dependencies that break a simple left-to-right traversal.

    Step 4: Evaluate option D. L-attributed definitions specifically restrict dependencies to parents and left siblings, which perfectly aligns with a left-to-right depth-first traversal. This is strictly true.

    Answer: It is guaranteed to work for L-attributed definitions due to left-to-right dependency restrictions.

    Question 10 · Compiler Design MCQ

    Consider an L-attributed definition with the production .

    The inherited attribute of depends on the inherited attribute of .

    The inherited attribute of depends on the inherited attribute of .

    Rank the following evaluation steps from first to last:

    1. Evaluate
    2. Evaluate
    3. Evaluate

    Select the correct ranking sequence.

    1. A.

      1, 2, 3

    2. B.

      2, 3, 1

    3. C.

      3, 2, 1

    4. D.

      2, 1, 3

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: In L-attributed definitions, inherited attributes flow top-down and left-to-right. An inherited attribute must be evaluated before the symbol it belongs to is parsed, and it depends only on the parent and left siblings.

    Step 1: Analyze the dependency chain. depends on . This means must be evaluated before .

    Step 2: Analyze the next dependency. depends on . This means must be evaluated before .

    Step 3: Combine the constraints. The strict order of evaluation must be .

    Step 4: Map this order to the given numbered items:

    • is item 2.
    • is item 3.
    • is item 1.

    Step 5: The correct sequence is 2, 3, 1.

    Answer: Option B.

    More notes in this unit