# Compiler Designs and Constructions

## Presentation on theme: "Compiler Designs and Constructions"— Presentation transcript:

Compiler Designs and Constructions
Chapter 9: Translation Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz Chapter 9: Translation COSC 470

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 Chapter 9: Translation

Example: X X.x Y Y.y Z Z.z A A.a A.a:= F(X.x, Y.y, Z.z) for production
A ---> XYZ State Value X X.x Y Y.y Z Z.z <--- Top A A.a After Parse ===> Value of Symbol Table Pointer to a State in LR(1) Parse Table Chapter 9: Translation

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 Chapter 9: Translation

Cont. Using LR Parser: Parse 3 * 5 + 4 \$ T.Val + E.Val E.Val
<--- Top Top – 1 Top - 2 <--- nTop = Chapter 9: Translation

Input Stack Attribute Production Used 3 * 5 + 4 \$ - * 5 + 4 \$ 3 F
F ---> digit T T ---> F 5 + 4 \$ T * + 4 \$ T * 5 3 * 5 T * F 3 * 5 F - digit 15 T ---> T * F E E ---> T 4 \$ E + \$ E + 4 E + F E + T 15 4 19 E ---> E + T L L ---> E \$ Chapter 9: Translation

Parse 3 * 5 + 4 \$ L E \$ Show Stack + T E T F T F * 4 F 5 3
Chapter 9: Translation

Intermediate Code Generation:
Type of intermediate code generation: Graphical Representation a := b * c + b * c Syntax Tree := + a * * c b c b Chapter 9: Translation

Dag := + a * b c Chapter 9: Translation

Representation of Syntax Tree:
:= + id a <--- b * c * * id b id c id b id c Chapter 9: Translation

Using array id b 1 c 2 * 3 4 5 6 + 7 a 8 := b * c b * c
id b 1 c 2 * 3 4 5 6 + 7 a 8 := b * c b * c Chapter 9: Translation

Postfix notation Advantages/Disadvantages? ( a:= ((b * c) + (b * c)))
No need for () No need for Temp Use Run-Time Stack Easy to reconstruct Parse Tree Chapter 9: Translation

X := A op B X, A, B are Const, Name, or Temp Example: a := b * c + d T0 := b * c T1 := T0 + d a := T1 Chapter 9: Translation

Three Address Code: Assignment Unary Operation Copy Statement
Operand:= Operand1 op Operand2 Unary Operation Operand:= Op Operand1 Copy Statement Operand:= Operand1 Procedure/Function Call Parm A,B Call P,n Chapter 9: Translation

Three Address Code Indexed Assignment Pointer Unconditional Jump
Operand[I]:= B Pointer A:= Address B Unconditional Jump GOTO L Conditional Jump If A RelOp B GOTO L Chapter 9: Translation

Quadruples (Quad/Three Address Code) Record structure with 4 fields Arg1, Arg3, op, Result Example: a:= -b+c*d Chapter 9: Translation

Quadruples: op Arg1 Arg2 Result U_Minus b T1 1 * c d T2 2 + T3 3 := a
U_Minus b T1 1 * c d T2 2 + T3 3 := a 4 Code: U_Minus b T1 Mul c d T2 Add T1 T2 T3 Assign T3 a Chapter 9: Translation

Triples: (3 – Tuples/Triplest) Record with 3 fields Arg1, Arg2, op Example: a := -b + c * d op Arg1 Arg2 (0) U_Minus b (1) * c d (2) + (3) := a Chapter 9: Translation

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 Chapter 9: Translation

Indirect Tuples: We use pointers to Triples Example: a:= -b + c * d
Address op Arg1 Arg2 14 U_Minus b 1 15 * c d 2 16 + (14) (15) 3 17 := a Chapter 9: Translation

Indirect Tuples: Save Space: Why? Not good for optimizing code: Why?
Not good for memory assignment prior to code generation Chapter 9: Translation