Compiler Designs and Constructions

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

Compiler Designs and Constructions
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
8 Intermediate code generation
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Semantic Analysis Chapter 4. Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are – semantic analysis – (intermediate)
1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Syntax Directed Translation
Syntax-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called.
CH4.1 CSE244 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
Syntax-Directed Translation
Copyright © 2005 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
Syntax Directed Definitions Synthesized Attributes
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
Topic #5: Translations EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
Syntax-Directed Translation
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Topic: Syntax Directed Translations
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Overview of Previous Lesson(s) Over View  An ambiguous grammar which fails to be LR and thus is not in any of the classes of grammars i.e SLR, LALR.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
Chapter 1: Introduction 1 Compiler Designs and Constructions Chapter 1: Introduction Objectives: Course Objectives Introduction Dr. Mohsen Chitsaz.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
1 Syntax-Directed Translation We associate information with the programming language constructs by attaching attributes to grammar symbols. 2.Values.
Syntax Directed Definition and Syntax directed Translation
Copyright © 2009 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
SDTs used to implement SDDs A non-cyclic SDD (having definitions of attributes) can always be implemented by a SDT (having actions that assign values to.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
Semantic Analysis Attribute Grammar. Semantic Analysis  Beyond context free grammar  Is x declared before it is used?  Is x declared but never used?
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
Semantic analysis Jakub Yaghob
Semantics Analysis.
Syntax-Directed Translation
Review: Chapter 5: Syntax directed translation
Compiler Construction
Compiler Construction
Chapter 5 Syntax Directed Translation
Syntax-Directed Translation Part I
Compiler Designs and Constructions
Syntax-Directed Translation Part II
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
CPSC 388 – Compiler Design and Construction
SYNTAX DIRECTED TRANSLATION
פרק 5 תרגום מונחה תחביר תורת הקומפילציה איתן אביאור.
Syntax-Directed Translation Part II
COMPILER DESIGN 11CS30013 & 11CS30014 Group October 2013
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Construction
Syntax-Directed Translation Part II
Subject: Language Processor
Chapter 4 Action Routines.
Syntax-Directed Translation Part I
Syntax-Directed Translation Part II
Directed Acyclic Graphs (DAG)
Syntax-Directed Translation Part I
Chapter 5 Syntax Directed Translation
Review for the Midterm. Overview (Chapter 1):
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 15 + 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

Advantages? Disadvantages? 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

Three Address Code: Three address Code: General Form Example: 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 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

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: 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

Types of Three Address Codes: 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