Related Articles
• Difficulty Level : Expert
• Last Updated : 10 Apr, 2020

Three-address code is a sequence of statements of the general form A := B op C, where A, B, C are either programmer defined names, constants or compiler-generated temporary names; op stands for an operation which is applied on A, B.In simple words, a code having at most three addresses in a line is called three address codes.

Example:

`(a+b)*(a+b+c) `

The three address code for above expression is:

```t1=a+b
t2=t1+c
t3=t1*t2
```

In compiler design the most popular intermediate code representation is Three-address code. It is globally accepted and is most widely used. There are a lot of three-address statements. All the complex three-address statements are generally a combination of simpler three-address statements.

These statements come under following seven categories and can be called as building block for three-address statements-

StatementMeaning
X = Y op ZBinary Operation
X= op ZUnary Operation
X = YAssignment
if X(rel op)Y goto LConditional Goto
goto LUnconditional Goto
A[i] = X
Y= A[i]
Array Indexing
Y = *P
*P = Z
Pointer Operations

Now, by using the above statements let us convert some popular high level constructs in Three-address code.

1. While Statement –

```while E do S
```

```L:if(E==0) goto L1
S
goto L
L1:end
```

2. For Statement –

```for(E1;E2;E3) do S
```

```  E1
L:if(E2) goto L1
goto L2
L1:S
L2:end
```

3. Switch Statement –

```switch(E){
case 1: S1
break;
case 2: S2
break;
default: S3
}
```

```  t=E
goto test
L1:S1
goto Last
L2:S2
goto Last
L3:S3
goto Last
test: if(E==1) goto L1
if(E==2) goto L2
goto L3
Last:end
```

Thus, we can see by using the seven statements we can replicate statements from higher level language into address codes.

My Personal Notes arrow_drop_up