230 likes | 431 Views
Compiler Designs and Constructions. Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz. Bottom-up evaluation of S-attributed:. Definitions: Syntax-directed definition with only synthesized attributes is called S-attributed Use LR Parser
E N D
Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz Chapter 9: Translation
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: A.a:= F(X.x, Y.y, Z.z) for production A ---> XYZ <--- Top After Parse ===> Value of Symbol Table Pointer to a State in LR(1) Parse Table Chapter 9: Translation
Example: Chapter 9: Translation
Cont. • Using LR Parser: Parse 3 * 5 + 4 $ <--- Top Top – 1 Top -2 <--- nTop = 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 b c Chapter 9: Translation
Dag := + a * c b Chapter 9: Translation
Advantages? • Disadvantages? Chapter 9: Translation
Representation of Syntax Tree: a- <--- b * c Chapter 9: Translation
Using array b * c b * c Chapter 9: Translation
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 Chapter 9: Translation
Three Address Code: • Three address Code: General Form 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: • Advantages? • Disadvantages? Chapter 9: Translation
Three Address Code: • Assignment 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 Operand[I]:= B • Pointer A:= Address B • Unconditional Jump GOTO L • Conditional Jump If A RelOp B GOTO L Chapter 9: Translation
Types of Three Address Codes: • Quadruples (Quad/Three Address Code) Record structure with 4 fields Arg1, Arg3, op, Result Example: a:= -b+c*d Chapter 9: Translation
Quadruples: Code: U_Minus b T1 Mul c d T2 Add T1 T2 T3 Assign T3 a Chapter 9: Translation
Types of Three Address Codes: • Triples: (3 – Tuples/Triplest) Record with 3 fields Arg1, Arg2, op Example: a := -b + c * d 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 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