![Page 1: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/1.jpg)
Chapter 9: Translation 1
Compiler Designs and Constructions
Chapter 9: TranslationObjectives:
Translation MethodThree Address Code
Dr. Mohsen Chitsaz
![Page 2: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/2.jpg)
Chapter 9: Translation 2
Bottom-up evaluation of S-attributed:
Definitions: Syntax-directed definition with only
synthesized attributes is called S-attributed Use LR Parser
Implementation: Stack to hold info about subtrees that have
been parsed
![Page 3: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/3.jpg)
Chapter 9: Translation 3
Example:
A.a:= F(X.x, Y.y, Z.z) for productionA ---> XYZ
State Value
X X.x
Y Y.y
Z Z.z A A.a
<--- Top
After Parse===>
Value of Symbol Table
Pointer to a State in LR(1) Parse Table
![Page 4: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/4.jpg)
Chapter 9: Translation 4
Example:
Production Semantic Values (Code)L ---> E$ Print (Val[Top])
E ---> E + T Val[nTop]:= Val[Top – 2] + Val[Top]
E ---> T
T ---> T * F Val[nTop]:= Val[Top – 2] * Val[Top]
T ---> F
F ---> (E) Val[nTop]:=Val[Top – 1]
F ---> digit
![Page 5: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/5.jpg)
Chapter 9: Translation 5
Cont.
Using LR Parser: Parse 3 * 5 + 4 $
T.Val
+
E.Val E.Val
<--- Top Top – 1 Top - 2 <--- nTop
=
![Page 6: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/6.jpg)
Chapter 9: Translation 6
Input Stack Attribute Production Used
3 * 5 + 4 $ - -
* 5 + 4 $ 3 3
* 5 + 4 $ F 3 F ---> digit
* 5 + 4 $ T 3 T ---> F
5 + 4 $ T * 3
+ 4 $ T * 5 3 * 5
+ 4 $ T * F 3 * 5 F - digit
+ 4 $ T 15 T ---> T * F
+ 4 $ E 15 E ---> T
4 $ E + 15
$ E + 4 15 + 4
$ E + F 15 + 4 F ---> digit
$ E + T 15 4 T ---> F
$ E 19 E ---> E + T
E 19
L 19 L ---> E $
![Page 7: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/7.jpg)
Chapter 9: Translation 7
Parse 3 * 5 + 4 $L
E $
E + T
FT
* FT
5F
3
Show Stack
4
![Page 8: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/8.jpg)
Chapter 9: Translation 8
Intermediate Code Generation:
Type of intermediate code generation:1. Graphical Representation
a := b * c + b * c Syntax Tree
:=
a +
* *
b c b c
![Page 9: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/9.jpg)
Chapter 9: Translation 9
Dag
:=
a +
*
b c
![Page 10: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/10.jpg)
Chapter 9: Translation 10
Advantages? Disadvantages?
![Page 11: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/11.jpg)
Chapter 9: Translation 11
Representation of Syntax Tree:
:=
id a +
* *
id c id bid b id c
<--- b * c
a-
![Page 12: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/12.jpg)
Chapter 9: Translation 12
b. Using array
0 id b
1 id c
2 * 0 1
3 id b
4 id c
5 * 3 4
6 + 2 5
7 id a
8 := 7 6
b * c
b * c
![Page 13: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/13.jpg)
Chapter 9: Translation 13
2. Postfix notation ( a:= ((b * c) + (b * c)))
a b c * b c * + :=
Advantages/Disadvantages?- No need for ()- No need for Temp- Use Run-Time Stack- Easy to reconstruct Parse Tree
![Page 14: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/14.jpg)
Chapter 9: Translation 14
Three Address Code:
3. Three address Code:General Form
X := A op BX, A, B are Const, Name, or Temp
Example: a := b * c + d
T0 := b * c
T1 := T0 + d
a := T1
![Page 15: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/15.jpg)
Chapter 9: Translation 15
Three Address Code: Advantages? Disadvantages?
![Page 16: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/16.jpg)
Chapter 9: Translation 16
Three Address Code: Assignment
Operand:= Operand1 op Operand2 Unary Operation
Operand:= Op Operand1 Copy Statement
Operand:= Operand1 Procedure/Function Call
Parm A,BCall P,n
![Page 17: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/17.jpg)
Chapter 9: Translation 17
Three Address Code Indexed Assignment
Operand[I]:= B Pointer
A:= Address B Unconditional Jump
GOTO L Conditional Jump
If A RelOp B GOTO L
![Page 18: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/18.jpg)
Chapter 9: Translation 18
Types of Three Address Codes:
1. Quadruples (Quad/Three Address Code)
Record structure with 4 fieldsArg1, Arg3, op, Result
Example: a:= -b+c*d
![Page 19: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/19.jpg)
Chapter 9: Translation 19
Quadruples:
op Arg1 Arg2 Result
0 U_Minus b T1
1 * c d T2
2 + T1 T2 T3
3 := T3 a
4Code: U_Minus b T1
Mul c d T2
Add T1 T2 T3
Assign T3 a
![Page 20: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/20.jpg)
Chapter 9: Translation 20
Types of Three Address Codes:
2. Triples: (3 – Tuples/Triplest)Record with 3 fieldsArg1, Arg2, op
Example: a := -b + c * d
op Arg1 Arg2
(0) U_Minus b
(1) * c d
(2) + (0) (1)
(3) := (2) a
![Page 21: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/21.jpg)
Chapter 9: Translation 21
Triples: Arg1, Arg2 are either a pointer to Symbol
Table (Name, Constant) or a pointer to Triple Structure
This is a Two Address Instruction Most Assembly Languages are made up of
Triples
![Page 22: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/22.jpg)
Chapter 9: Translation 22
3. Indirect Tuples:We use pointers to TriplesExample: a:= -b + c * d
Triple Address op Arg1 Arg2
0 14 14 U_Minus b
1 15 15 * c d
2 16 16 + (14) (15)
3 17 17 := (15) a
![Page 23: Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz](https://reader035.vdocuments.us/reader035/viewer/2022062223/551c0c23550346a84f8b5199/html5/thumbnails/23.jpg)
Chapter 9: Translation 23
Indirect Tuples:
Save Space: Why? Not good for optimizing code: Why? Not good for memory assignment prior
to code generation