ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3149Back Patching Switch S…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3149

    Compiler Construction

    Progress0 / 32 done
    Topics32
    1Introduction to interpreter and compiler2Structure of a Compiler and its Phases3Lexical Analyzer and Input Buffering4Specifications and Recognitions of Tokens5Regular Expressions and Finite Automata6Transition Table and Transition Graph7Definitions of Grammars, Derivations, and Parse Trees8Ambiguity, Associativity, and Precedence of Operators9Syntax Analysis and Role of the Parser10Eliminating Ambiguity, Left Recursion, and Left Factoring11Top-Down Parsing and Recursive-Descent Parsing12First and Follow Sets13LL(1) Grammars and Non-recursive Predictive Parsing
    14Reductions and Shift-Reduce Parsing15LR Parsing and LR(0) Parsers16LR(0) Automaton and Parsing Table17Shift-Reduce Conflicts
    18Automaton and Parsing Table
    19Automaton and Parsing Table
    20Automaton and Parsing Table21Semantic Analysis and Intermediate Code Generation22Three Address Code23Tasks of Semantic Analyzer and Types of Errors24Type Checking and Environments
    25Implicit vs Explicit26Back Patching and Switch Statements27Storage Organization and Stack Allocation of Space28Heap Management and Optimization
    29Design of a Code Generator30Target Language and Addresses in Target Code31Basic Blocks and Flow Graphs32Optimization of Basic Blocks
    4 min read
    Topic 26 of 32
    Topic Study Note Illustration
    26

    Back Patching and Switch Statements

    These topics are part of Intermediate Code Generation and Code Generation phase in a compiler. They are very important for exams because they are often asked with...

    🧠 Backpatching and Switch Statements (Compiler Construction)

    These topics are part of Intermediate Code Generation and Code Generation phase in a compiler. They are very important for exams because they are often asked with diagrams and examples.


    πŸ“Œ 1. Backpatching

    πŸ“– Definition

    Backpatching is a technique used in compiler design to handle forward jumps in intermediate code by filling in (patching) missing addresses later.

    πŸ‘‰ In simple words:

    We write code first, and fill the correct jump addresses later when they become known.


    ❓ Why Backpatching is Needed?

    In programming, sometimes we need to jump to a label that is not yet known.

    Example:

    if (a < b)
        goto L1;
    

    But at this time, we don’t know where L1 is located in code.

    πŸ‘‰ So we leave it empty and fill it later.


    βš™οΈ Basic Idea

    • Generate incomplete jump instructions
    • Store their locations in a list
    • Fill correct addresses later

    πŸ“Š Example (Three Address Code)

    1. if a < b goto __
    2. goto __
    3. ...
    4. L1: t = a + b
    

    Later we fill:

    • First blank β†’ L1
    • Second blank β†’ next instruction

    πŸ”„ Steps of Backpatching

    πŸ”Ή Step 1: Generate code with blanks

    if a < b goto _
    

    πŸ”Ή Step 2: Maintain list of incomplete jumps

    List = {1}
    

    πŸ”Ή Step 3: When label is found, fill address

    patch(1, L1)
    

    🧠 Key Data Structures in Backpatching

    πŸ”Ή 1. List of incomplete jumps

    Stores unresolved goto statements

    πŸ”Ή 2. Functions used:

    • makelist(i) β†’ creates list with instruction i
    • merge(p1, p2) β†’ merges two lists
    • backpatch(p, addr) β†’ fills address

    ⭐ Important Exam Points

    βœ” Used in control flow statements βœ” Handles forward jumps βœ” Used in if, if-else, loops βœ” Uses three address code (TAC) βœ” Helps in code generation phase


    πŸ“Œ 2. Switch Statements in Compiler Design

    πŸ“– Definition

    A switch statement is a multi-way branch statement that allows selection of one block among many based on the value of an expression.


    πŸ” Example (High-level code)

    switch(x) {
      case 1: a = 10; break;
      case 2: a = 20; break;
      default: a = 0;
    }
    

    βš™οΈ Translation into Intermediate Code

    Switch statements are converted into:

    1. Jump Table OR

    2. Sequence of conditional jumps


    πŸ“Š Method 1: If-Else Chain (Simple)

    if x == 1 goto L1
    if x == 2 goto L2
    goto L3
    
    L1: a = 10
    goto end
    
    L2: a = 20
    goto end
    
    L3: a = 0
    
    end:
    

    πŸ“Š Method 2: Jump Table (Efficient)

    Used when cases are dense.

    index = x - 1
    goto table[index]
    

    Where:

    table[0] β†’ L1
    table[1] β†’ L2
    table[2] β†’ L3
    

    βš™οΈ Working of Switch Statement

    Step-by-step:

    1. Evaluate expression
    2. Compare with cases
    3. Jump to matching label
    4. Execute block
    5. Exit switch

    πŸ“Œ Types of Implementation

    πŸ”Ή 1. Linear Search (If-Else Chain)

    • Slower
    • Used for fewer cases

    πŸ”Ή 2. Binary Search (Optimized)

    Binary search algorithm
    Visual ReferenceBinary Search Algorithm
    Wikipedia
    • For sorted cases

    πŸ”Ή 3. Jump Table (Fastest)

    • Constant time access
    • Used when cases are dense

    ⭐ Important Exam Points

    βœ” Switch is a multi-way branching statement βœ” Implemented using:

    • if-else chain
    • jump table βœ” Jump table is most efficient βœ” Used in intermediate code generation βœ” Helps reduce number of comparisons

    πŸ”— 3. Relationship Between Backpatching & Switch

    Concept Role
    Backpatching Fills missing jump addresses
    Switch statement Uses multiple jumps to labels
    Connection Both involve control flow & jumps

    πŸ“Š FINAL REVISION TABLE

    πŸ”Ή Backpatching

    Feature Description
    Purpose Handle forward jumps
    Used in Control flow statements
    Technique Fill addresses later
    Functions makelist, merge, backpatch
    Code type Three address code

    πŸ”Ή Switch Statement

    Feature Description
    Type Multi-way branch
    Implementation if-else or jump table
    Best method Jump table
    Use Efficient selection
    Optimization Reduces comparisons

    🎯 Final Exam Definitions

    πŸ“Œ Backpatching:

    Backpatching is a technique used in intermediate code generation where incomplete jump instructions are temporarily left with blank addresses and are filled later when target labels become known.

    πŸ“Œ Switch Statement:

    A switch statement is a multi-way decision construct that is translated into intermediate code using either conditional jumps or jump tables for efficient execution.


    ← Previous topic 25
    Type Conversions: Implicit vs Explicit
    Next topic 27 β†’
    Storage Organization and Stack Allocation of Space

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    683words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction