Intermediate Representations: Three-Address Code Fundamentals Quiz Quiz

Explore the essentials of Three-Address Code (TAC) in compiler intermediate representations with this easy quiz designed for beginners. Assess your understanding of TAC syntax, operations, control flow, and its role in optimizing source code during compilation.

  1. Defining Three-Address Code

    Which statement best describes Three-Address Code (TAC) in compiler design?

    1. It is a type of assembly language specific to hardware.
    2. It is an intermediate representation that uses at most three operands per instruction.
    3. It is an input format for high-level programming languages.
    4. It only supports two operands per instruction.

    Explanation: Three-Address Code is an intermediate representation where each instruction typically contains at most three addresses: two for source operands and one for the result. It is not hardware-specific like assembly language, ruling out the second option. Unlike the third option, TAC can include three operands, not just two. The fourth option is incorrect because TAC is not an input format for high-level languages; rather, it is used internally within compilers.

  2. TAC Syntax Example

    Given the arithmetic expression a = b + c * d, which of the following best represents its equivalent in Three-Address Code?

    1. b c d * + = a
    2. t1 = b + c; t2 = t1 * d; a = t2
    3. t1 = c * d; t2 = b + t1; a = t2
    4. a = b + c * d

    Explanation: The correct TAC sequence breaks the expression into steps by first computing t1 = c * d, then t2 = b + t1, and finally assigning t2 to a. The second option keeps the high-level syntax without breaking it down, which is not TAC. The third option incorrectly computes the addition before multiplication. The fourth option jumbles operation order and uses a nonstandard form.

  3. Support for Temporary Variables

    Why does Three-Address Code frequently use temporary variables like t1, t2, and t3?

    1. To store intermediate results during decomposed computations
    2. To define loop bounds directly
    3. To manage input and output devices
    4. To declare new functions

    Explanation: TAC often breaks complex computations into smaller steps, storing results in temporary variables. This modularizes operations for easier translation and optimization. Temporary variables are not used for function declarations or managing devices, which rules out options two and three. They do not directly define loop bounds, making the fourth choice incorrect.

  4. Common TAC Operation Types

    Which operation is NOT typically found in Three-Address Code instructions?

    1. Branching instructions such as if t1 u003C t2 goto L1
    2. Direct hardware device access
    3. Arithmetic assignment like t1 = a + b
    4. Assignment operations like a = t2

    Explanation: TAC is an abstraction meant for code transformation and optimization, not for controlling hardware directly. Arithmetic assignments, branching, and simple variable assignments are all common and valid in TAC. Direct hardware instructions are more associated with low-level machine code, not intermediate representations.

  5. Role in Compiler Optimization

    What is a primary reason compilers convert source code into Three-Address Code during optimization?

    1. It adds extra complexity to make debugging harder.
    2. It simplifies code to enable easier and more effective optimizations.
    3. It generates efficient machine code immediately.
    4. It bypasses the need for syntax analysis.

    Explanation: TAC breaks source code into simple steps, making optimizations like constant folding or dead code elimination more manageable. It does not produce machine code directly, so option two is incorrect. The process aims to simplify, not complicate or obfuscate code, making option three wrong. Syntax analysis is still required before TAC generation, so option four is not accurate.

  6. TAC for Control Flow Statements

    Which TAC instruction best represents an if statement such as if (x u003E y) z = x?

    1. goto L1 if x u003E y; z = x
    2. if (x u003E y) { z = x }
    3. if x u003E y goto L1; goto L2; L1: z = x; L2:
    4. z = x if x u003E y

    Explanation: The correct sequence uses explicit 'goto' instructions and labels, which is a standard approach in TAC for control flow. The second and third choices use conditional assignment syntax unsupported by TAC. The fourth option misplaces the jump instruction and does not use proper labeling or order.

  7. TAC Limitations

    Which of the following is a limitation of Three-Address Code?

    1. It requires machine-specific syntax.
    2. It is only used for graphical user interfaces.
    3. It does not support direct memory addressing or pointer operations.
    4. It cannot represent arithmetic operations.

    Explanation: TAC abstracts away from low-level memory details, so it typically omits direct pointer operations. Arithmetic operations are fundamental to TAC, making option two incorrect. It is not tied only to graphical applications, and its syntax is intentionally machine-independent, not specific, so options three and four are also wrong.

  8. Three-Address Code Assignment Format

    Which format is correct for a typical TAC assignment operation?

    1. result = operand1 op operand2
    2. result : operand1, operand2, op
    3. op = operand1 + operand2 = result
    4. operand1 op operand2 result =

    Explanation: The standard TAC format places the result variable on the left of the assignment, followed by the operation involving two operands. The second and fourth choices mix up the order or use nonstandard syntax. The third option introduces misplaced colons and commas, which are not part of TAC’s notation.

  9. Function Call Representation in TAC

    How is a function call typically represented in Three-Address Code?

    1. function-call: f, n
    2. call f, n or t1 = call f, n
    3. invoke f with n arguments
    4. func f(n)

    Explanation: TAC commonly denotes function calls by using instructions like 'call f, n' where 'f' is the function and 'n' is the number of arguments, with the assignment form for return values. The other options use incorrect or nonstandard keywords not associated with TAC syntax. The correct form helps maintain clarity and uniformity.

  10. TAC as a Bridge in Compilation

    What role does Three-Address Code play between parsing and target code generation in a compiler?

    1. It is an initial step for tokenization.
    2. It acts as an intermediate, machine-independent representation for further optimization.
    3. It directly converts source code to executable files.
    4. It stores debug information only.

    Explanation: TAC is designed to sit between source parsing and machine code generation, providing a machine-independent layer for optimizations. It does not directly generate executables or serve as a tool for tokenization, which occur at different stages. While debug information can be linked with TAC, this is not its primary function.