ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Compiler Construction
    COMP3149
    Progress0 / 32 topics
    Topics
    1. Introduction to interpreter and compiler2. Structure of a Compiler and its Phases3. Lexical Analyzer and Input Buffering4. Specifications and Recognitions of Tokens5. Regular Expressions and Finite Automata6. Transition Table and Transition Graph7. Definitions of Grammars, Derivations, and Parse Trees8. Ambiguity, Associativity, and Precedence of Operators9. Syntax Analysis and Role of the Parser10. Eliminating Ambiguity, Left Recursion, and Left Factoring11. Top-Down Parsing and Recursive-Descent Parsing12. First and Follow Sets13. LL(1) Grammars and Non-recursive Predictive Parsing14. Bottom-Up Parsing: Reductions and Shift-Reduce Parsing15. LR Parsing and LR(0) Parsers16. LR(0) Automaton and Parsing Table17. Shift-Reduce Conflicts18. SLR(1) Parsers: Automaton and Parsing Table19. LR(1) Parsers: Automaton and Parsing Table20. LALR Parsing: Automaton and Parsing Table21. Semantic Analysis and Intermediate Code Generation22. Three Address Code23. Tasks of Semantic Analyzer and Types of Errors24. Type Checking and Environments25. Type Conversions: Implicit vs Explicit26. Back Patching and Switch Statements27. Storage Organization and Stack Allocation of Space28. Heap Management and Optimization29. Code Generation: Design of a Code Generator30. Target Language and Addresses in Target Code31. Basic Blocks and Flow Graphs32. Optimization of Basic Blocks
    COMP3149›Three Address Code
    Compiler ConstructionTopic 22 of 32

    Three Address Code

    4 minread
    606words
    Beginnerlevel

    📘 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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count606
      Code examples0
      DifficultyBeginner