ToolSphere

Three Address Code Generator

Computer science

Turn expressions, if-else and while loops into three-address code, quadruples, triples and indirect triples, with the syntax tree and the DAG.

Type expressions or assignments to see their intermediate instructions.

Three-address code — first three instructions
t1 = b * c
t2 = a + t1
t3 = t2 - d

Three-address code

LabelInstructionMeaning
t1 = b * ct1 holds b * c
t2 = a + t1t2 holds a + b * c
t3 = t2 - dt3 holds a + b * c - d
x = t3x = a + b * c - d

Dragon Book conventions: each operator result goes into a new temporary t1, t2, …; unary minus is written minus; a condition jumps to its true exit, otherwise falls to a goto its false exit.

On this browser only

Give feedback

Did this tool help?

Tell us what worked, what didn’t, or what you’d like next.

Opens Google Forms in a new tab with this tool’s name filled in. No sign-in or email required. Feedback is optional.

How to use Three Address Code Generator

  1. 1Type a program: assignments like x = a + b * c, plus if, if-else and while with conditions using < > <= >= == != && || and !.
  2. 2Read the three-address code. The Meaning column says what each temporary holds, for example t1 holds b * c.
  3. 3Tick Number instructions to write jumps as goto 104 instead of goto L2, the way many exam answers do.
  4. 4Switch tabs for quadruples, triples, indirect triples, the syntax tree, and the DAG with the code it optimizes to.

About Three Address Code Generator

What three-address code is

Three-address code is the intermediate representation most compilers produce between parsing and machine code. Every instruction has at most three addresses: two operands and a result, as in t1 = b * c. A complicated expression is broken into a sequence of these, one operator at a time, with temporaries t1, t2, … holding the partial results. Because each step is so simple, it is easy to optimize and easy to translate to any target machine.

How expressions are translated

The generator follows operator precedence and associativity: * and / before + and -, left to right. For x = a + b * c - d it produces t1 = b * c, then t2 = a + t1, then t3 = t2 - d, and finally x = t3. Unary minus is written with the operator minus, as in the Dragon Book: a = b * -c + b * -c becomes t1 = minus c, t2 = b * t1, t3 = minus c, t4 = b * t3, t5 = t2 + t4, a = t5.

Quadruples, triples and indirect triples

These are three ways to store the same code. A quadruple has four fields, op, arg1, arg2 and result, so the row for t2 = b * t1 is (, b, t1, t2). A triple drops the result field and refers to earlier values by their position: (, b, (0)) means "b times whatever triple 0 computed". Triples save space, but moving one breaks every reference to it. Indirect triples fix that with a separate list of pointers to the triples, so an optimizer reorders the list and leaves the triples alone.

if, while, && and ||

Conditions become jumping code. if (a < b) x = 1 compiles to "if a < b goto L1", "goto L2", "L1: x = 1", "L2:". With && and ||, only as much of the condition is tested as needed: in x < 100 || x > 200, the second test is skipped when the first is true. while loops get a label at the test and a goto back to it at the end of the body. Labels can be switched to instruction numbers starting from 100, the form many university exam answers use.

The DAG and optimized code

For a block of assignments, the DAG (directed acyclic graph) merges repeated subexpressions into a single node. In a = b * -c + b * -c, b * -c is built once, so the code regenerated from the DAG is three instructions instead of six. The DAG also handles the classic Dragon Book Example 8.10 (a = b + c, b = a - d, c = b + c, d = a - d), where b and d end up on the same node.

What it does not do

It covers the statements compiler-design courses use for intermediate code: arithmetic expressions, unary minus, assignment, if, if-else, while, relational operators and && || !. It does not handle arrays, function calls, pointers or declarations, and the DAG and triples tabs are for straight-line code only.

Where this fits

Intermediate code is generated after parsing, usually as the parser reduces each rule. The parsing side is covered by the LR parser for bottom-up and the LL(1) parser for top-down, both built on the sets from the FIRST and FOLLOW calculator.

Guides and tutorials

Frequently asked questions

How do you generate three-address code for an expression?

Evaluate the expression one operator at a time in precedence order, storing each result in a new temporary. For x = a + b * c - d: t1 = b * c, t2 = a + t1, t3 = t2 - d, x = t3. Multiplication comes first because it binds tighter than + and -.

What is the difference between quadruples and triples?

A quadruple stores op, arg1, arg2 and an explicit result, such as (*, b, t1, t2). A triple has no result field; later triples refer to it by number, like (+, (1), (3)). Triples are shorter, but reordering them breaks those references, which is why indirect triples add a pointer list.

How are temporary variables numbered in three-address code?

Each operator gets a fresh temporary in the order it is evaluated, starting at t1. An assignment then copies the last temporary into the variable, as in x = t3. The Dragon Book and most course notes use this convention, and the tool follows it.

Can three-address code handle if-else and while loops?

Yes, with conditional and unconditional jumps: if x relop y goto L and goto L. The tool generates these for if, if-else and while, including short-circuit && and ||, with labels or with instruction numbers starting from 100.

How does a DAG reduce three-address code?

A DAG gives each distinct value one node, so a repeated subexpression is computed only once. For a = b * -c + b * -c the DAG has a single b * -c node, and the regenerated code is t1 = minus c, t2 = b * t1, a = t2 + t2: three instructions instead of six.

Have a suggestion for Three Address Code Generator?Share your feedback ↗