• Difficulty Level : Hard
• Last Updated : 10 Sep, 2021

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:

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.

`(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-

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
E3
goto L
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