Translating Arithmetic Expressions into Three-Address Code (TAC): From Syntax Tree to TAC
Three-address code []{def="An intermediate code where each instruction uses at most three operands: two inputs and one result"} is a linearized intermediate representation used in compilers. Each TAC instruction typically has the form (binary) or (unary), where operands are names (identifiers, constants, or compiler-generated temporaries).2
A standard translation workflow is:
- Parse the expression into a tree structure (AST/parse tree).
- Traverse the tree (commonly postorder) so that child computations are emitted before the parent.
- For each operator node, emit one TAC instruction using temporaries to hold intermediate results.
- The final TAC value is the “place”/temporary of the root node.2
Mermaid view of the idea (AST node → emitted TAC line):
Key ideas/terms you will use:
- AST
- Temporary variable
- Postorder traversal
- Unary minus
- Intermediate code generation
Footnotes
-
Lecture 8: Intermediate-Code Generation - Defines TAC and shows unraveling into temporaries. ↩
-
Three-Address Code (UWO CS447) - Gives TAC instruction forms and syntax-directed translation rules. ↩ ↩2
-
CS316 Compilers Lab PA4.pdf - Describes generating TAC from an AST via post-order with temporaries. ↩
Syntax-directed translation intuition (expression to intermediate code)
Three-address code (TAC) translation process
TAC instruction constraints
In TAC, “complicated arithmetic expressions are represented as a sequence of three-address instructions, using temporaries for intermediate values.”2 Concretely:
- Binary operators (e.g., , , ):
emit - Unary minus (e.g., ):
emit (often written as a unary operator likeminusto distinguish it from binary subtraction).2
The core generation can be described with a syntax-directed translation scheme where each expression node has attributes:
In practice, the compiler performs a postorder walk: compute children TAC first, then emit the parent TAC using their resulting places.
Algorithm (tree → TAC)
Let each node in the AST return its :
- If the node is an identifier : and .
- If the node is a binary operator :
- translate and to get
- create a new temporary
- emit
t := L.place op R.place - return
- If the node is unary minus :
- translate to get
- create a new temporary
- emit
t := minus U.place - return .2
Unary minus vs binary minus: many compiler presentations explicitly use a special operator minus for unary negation so that TAC generation uses different instruction forms for unary negation and subtraction.
Footnotes
-
Lecture 8: Intermediate-Code Generation - Defines TAC and shows unraveling into temporaries. ↩
-
Three-Address Code (UWO CS447) - Gives TAC instruction forms and syntax-directed translation rules. ↩ ↩2 ↩3 ↩4
-
Lecture 7 (compiler/three-address-code PDF snippet) - Shows unary minus handled with a special unary operator and provides a TAC example. ↩ ↩2 ↩3
-
CS316 Compilers Lab PA4.pdf - Describes generating TAC from an AST via post-order with temporaries. ↩
Generating TAC by postorder traversal for an arithmetic expression
- 1Step 1
Parse the expression with correct precedence/associativity into an AST.
- 2Step 2
For each operator node, recursively translate children (postorder).
- 3Step 3
For identifiers/constants, set to the identifier itself and emit no TAC.
- 4Step 4
If the node is unary minus, create a fresh temporary and emit .
- 5Step 5
If the node is binary operator (e.g., , , ), create fresh temporary and emit .
- 6Step 6
After processing the root operator, the returned place is the TAC result; optionally emit a final copy into the destination.
Expression to translate
Translate into TAC the arithmetic expression:
We must respect precedence:
- Unary minus binds to first.
- Multiplication binds tighter than addition .
- Parentheses force subtraction before the multiplication by .
Corresponding syntax tree (AST)
Using an AST whose internal nodes are operators and leaves are operands, the structure is:
- Root is
- Left child is between and
- Right child is between and
Mermaid syntax tree:
Translate AST to TAC
We perform postorder:
- Compute
- Compute
- Compute
- Compute
- Compute the final sum
Let temporaries be in creation order. TAC instructions (quadruple style omitted, using the common assignment form):
TAC sequence
- Unary minus (i.e., )2
- Left multiplication
- Subtraction in parentheses
- Right multiplication
- Final addition
If the expression is assigned to some destination , you would finish with: (though your prompt only asks to translate the expression itself).
These steps mirror the standard rule that each TAC instruction corresponds to one operator node, and operator outputs flow upward via temporaries.3
Footnotes
-
Three-Address Code (UWO CS447) - Gives TAC instruction forms and syntax-directed translation rules. ↩ ↩2
-
Lecture 7 (compiler/three-address-code PDF snippet) - Shows unary minus handled with a special unary operator and provides a TAC example. ↩
-
Lecture 8: Intermediate-Code Generation - Defines TAC and shows unraveling into temporaries. ↩
-
CS316 Compilers Lab PA4.pdf - Describes generating TAC from an AST via post-order with temporaries. ↩
Operator-to-TAC mapping (one instruction per AST operator node)
Counts show how many TAC instructions correspond to each AST operator type in this expression.
Common pitfalls and how to avoid them
Pro Tip: Think “place” + “code”
When translating each AST node, return two things: where its value lives (place) and the TAC emitted to compute it (code). This is the classic SDT setup for TAC generation.
Footnotes
-
Three-Address Code (UWO CS447) - Gives TAC instruction forms and syntax-directed translation rules. ↩
Beware of precedence errors
If you accidentally parse as (or similar), the TAC order of emitted operations will change. Always reflect precedence and parentheses in the AST first.2
Footnotes
-
Lecture 8: Intermediate-Code Generation - Defines TAC and shows unraveling into temporaries. ↩
-
Three-Address Code (UWO CS447) - Gives TAC instruction forms and syntax-directed translation rules. ↩
Knowledge Check
In typical three-address code (TAC) generation for expressions, what is the purpose of temporaries like ?