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 PortalCOMP3149Three Address Code
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 22 of 32
    Topic Study Note Illustration
    22

    Three Address Code

    (Compiler Construction β€” exam-ready, simple explanation with examples, rules, and key points)

    πŸ“˜ Three Address Code (TAC)

    (Compiler Construction β€” exam-ready, simple explanation with examples, rules, and key points)


    🧠 1. What is Three Address Code (TAC)?

    βœ… Definition

    Three Address Code (TAC) is an intermediate representation (IR) used in compilers where each instruction has at most three addresses (operands).

    πŸ‘‰ It breaks complex expressions into simple step-by-step instructions.


    πŸ“Œ General Form

    x = y op z
    

    Where:

    • x β†’ result
    • y, z β†’ operands
    • op β†’ operator (+, -, *, /, etc.)

    🎯 2. Why TAC is Used?

    βœ” Simplifies complex expressions βœ” Helps in optimization βœ” Easy to convert into machine code βœ” Machine-independent


    πŸ”§ 3. Structure of TAC Instructions

    TAC supports different types of statements:


    πŸ”Ή 1. Assignment

    x = y
    

    πŸ”Ή 2. Binary Operation

    x = y + z
    

    πŸ”Ή 3. Unary Operation

    x = -y
    

    πŸ”Ή 4. Conditional Jump

    if x < y goto L1
    

    πŸ”Ή 5. Unconditional Jump

    goto L1
    

    🧠 4. TAC for Expressions

    πŸ“Œ Example 1:

    Expression:

    a = b + c * d
    

    πŸ”„ Step-by-step TAC:

    t1 = c * d
    t2 = b + t1
    a = t2
    

    🌳 5. TAC using Precedence Rules

    πŸ‘‰ Multiplication first, then addition


    πŸ“Œ Example 2:

    x = (a + b) * (c - d)
    

    TAC:

    t1 = a + b
    t2 = c - d
    t3 = t1 * t2
    x = t3
    

    πŸ”„ 6. TAC for Boolean Expressions

    πŸ“Œ Example:

    if (a < b && c < d)
    

    TAC:

    if a < b goto L1
    goto L2
    L1: if c < d goto L3
    goto L2
    L3: ...
    

    πŸ“Œ 7. TAC for Control Statements


    πŸ”Ή If Statement

    if (x > 0)
        y = 1;
    

    TAC:

    if x > 0 goto L1
    goto L2
    L1: y = 1
    L2:
    

    πŸ”Ή If-Else Statement

    if (x > 0) y = 1;
    else y = 2;
    

    TAC:

    if x > 0 goto L1
    goto L2
    L1: y = 1
    goto L3
    L2: y = 2
    L3:
    

    πŸ”Ή While Loop

    while (x < 10)
        x = x + 1;
    

    TAC:

    L1: if x >= 10 goto L2
        x = x + 1
        goto L1
    L2:
    

    🧠 8. Types of TAC Representation


    πŸ”Ή 1. Quadruples

    Format:

    (op, arg1, arg2, result)
    

    Example:

    op arg1 arg2 result
    * c d t1
    + b t1 t2
    = t2 a

    πŸ”Ή 2. Triples

    Format:

    (index) op arg1 arg2
    

    Example:

    i op arg1 arg2
    0 * c d
    1 + b (0)
    2 = (1) a

    πŸ”Ή 3. Indirect Triples

    • Uses pointer table
    • Easier optimization
    • More flexible than triples

    πŸ”„ 9. TAC vs High-Level Code

    Feature High-Level Code TAC
    Complexity High Simple
    Structure Complex Linear
    Optimization Hard Easy
    Machine dependency No No

    🎯 10. Advantages of TAC

    βœ” Simplifies expressions βœ” Easier optimization βœ” Helps code generation βœ” Machine-independent


    ❌ 11. Disadvantages

    ❌ Needs extra memory (temporary variables) ❌ Not executable directly ❌ Requires further translation


    πŸ“Œ 12. Important Exam Points

    πŸ‘‰ Frequently asked:

    • Define Three Address Code
    • Convert expression to TAC
    • TAC for if-else / loops
    • Quadruples vs triples
    • Why TAC is used in compiler
    • Advantages of TAC

    πŸ“ 13. Short Notes (Quick Revision)

    πŸ”΅ TAC

    • Intermediate code
    • At most 3 operands
    • Uses temporaries

    πŸ”΅ Forms

    • Quadruples
    • Triples
    • Indirect triples

    πŸ“Š 14. Final Summary Table

    Aspect Three Address Code
    Type Intermediate representation
    Format x = y op z
    Purpose Simplify code
    Output Linear instructions
    Use Optimization & code generation
    Machine dependency No

    βœ… Final Conclusion

    • Three Address Code (TAC) is the most important intermediate representation in compilers
    • It breaks complex expressions into simple instructions using temporary variables
    • It is widely used for optimization and machine code generation

    ← Previous topic 21
    Semantic Analysis and Intermediate Code Generation
    Next topic 23 β†’
    Tasks of Semantic Analyzer and Types of Errors

    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
    606words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction