1 / 23

Compiler Designs and Constructions

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

damian
Download Presentation

Compiler Designs and Constructions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz 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 Chapter 9: Translation

  3. 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

  4. Example: Chapter 9: Translation

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

  6. Chapter 9: Translation

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

  8. 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

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

  10. Advantages? • Disadvantages? Chapter 9: Translation

  11. Representation of Syntax Tree: a- <--- b * c Chapter 9: Translation

  12. Using array b * c b * c Chapter 9: Translation

  13. 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

  14. 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

  15. Three Address Code: • Advantages? • Disadvantages? 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,B Call P,n 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 Chapter 9: Translation

  18. 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

  19. Quadruples: Code: U_Minus b T1 Mul c d T2 Add T1 T2 T3 Assign T3 a Chapter 9: Translation

  20. Types of Three Address Codes: • Triples: (3 – Tuples/Triplest) Record with 3 fields Arg1, Arg2, op Example: a := -b + c * d 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 Chapter 9: Translation

  22. Indirect Tuples: We use pointers to Triples Example: a:= -b + c * d Chapter 9: Translation

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

More Related