CH4.1 CSE244 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.

Slides:



Advertisements
Similar presentations
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Advertisements

Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
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.
CH4.1 CSE244 Type Checking Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
1 Beyond syntax analysis An identifier named x has been recognized. Is x a scalar, array or function? How big is x? If x is a function, how many and what.
Lecture # 17 Syntax Directed Definition. 2 Translation Schemes A translation scheme is a CF grammar embedded with semantic actions rest  + term { print(“+”)
Intermediate Code Generation Professor Yihjia Tsai Tamkang University.
CH4.1 CSE244 L-Attributed Definitions Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
Abstract Syntax Tree (AST)
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 Bottom Up Translation (revisited) Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road,
CH4.1 CSE244 Bottom Up Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155.
Syntax-Directed Translation
CH4.1 CSE244 Intermediate Code Generation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
CH5.1 CSE 4100 Chapter 5: Syntax Directed Translation Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
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.
Topic #5: Translations EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Syntax-Directed Translation
Topic: Syntax Directed Translations
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……
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.
1 Semantic Analysis. 2 Semantic Analyzer Attribute Grammars Syntax Tree Construction Top-Down Translators Bottom-Up Translators Recursive Evaluators Type.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
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.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
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.
國立台灣大學 資訊工程學系 薛智文 98 Spring Syntax-Directed Translation (textbook ch#5.1–5.6, 4.8, 4.9 )
Syntax Directed Definition and Syntax directed Translation
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
Copyright © 2009 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
CH4.1 CSE244 Midterm Subjects Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
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.
1 Syntax-Directed Translation Part II Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 9 Ahmed Ezzat Semantic Analysis.
Semantic analysis Jakub Yaghob
Semantics Analysis.
Chapter 5: Syntax Directed Translation
Syntax-Directed Translation
Context-Sensitive Analysis
Compiler Construction
Chapter 5 Syntax Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part II
Intermediate Code Generation
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
SYNTAX DIRECTED TRANSLATION
פרק 5 תרגום מונחה תחביר תורת הקומפילציה איתן אביאור.
Syntax-Directed Translation Part II
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part II
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Syntax-Directed Translation Part II
Directed Acyclic Graphs (DAG)
Syntax-Directed Translation Part I
Chapter 5 Syntax Directed Translation
Presentation transcript:

CH4.1 CSE244 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs, CT

CH4.2 CSE244General  Syntax-directed Definitions.  Translation Schemes  “Programming Language Translation will be guided by the syntax”  Use Attributes to implement translation.  Any symbol of the grammar might have several attributes associated with it.  Translation is specified by presenting the dependencies of the attributes.  This is achieved by the employment of Semantic Rules.  Execution of Semantic Rules obeying the order specified by Attribute interrelations produces the translation.

CH4.3 CSE244 Syntax-Directed Definitions, I  It is a CFG grammar augmented with:  Attributes. (assigned to each grammar symbol)  Semantic Rules (associated to each production involving the Attributes of the symbols in the production).  Attributes can be Synthesized or Inherited.  Semantic Rules for a production have the form:  Semantic Rules for a production A   have the form:  b = f (c 1,…,c n )where  (b is synthesized) b is an attribute of A and c 1 …c n are attributes of symbols in .  (b is inherited) b is an attribute of some symbol in  and c 1 …c n are attributes of symbols in A, 

CH4.4 CSE244 Syntax-Directed Defitions, II  Terminals have only synthesized attributes whose values are provided by the lexical analyzer.  The start non-terminal typically has no inherited attributes.  We may allow function calls as semantic-rules also. Side-effects… PRODUCTIONSEMANTIC RULE L  E nprint(E.val) E  E 1 + TE.val = E 1.val + T.val E  TE.val = T.val T  T 1 * FT.val = T 1.val * F.val T  FT.val = F.val F  (E)F.val = E.val F  digitF.val = digit.lexval

CH4.5 CSE244 Annotated Parse-Trees  Parse-tree that also shows the values of the attributes at each node.  Attributes in the leaves of the annotated parse-tree are determined by the lexical analyzer.  Values of Attributes in inner nodes of annotated parse-tree are determined by the semantic-rules.  If a syntax-directed definition employs only Synthesized attributes the evaluation of all attributes can be done in a bottom-up fashion.  Inherited attributes would require more arbitrary “traversals” of the annotated parse-tree.  A dependency graph suggests possible evaluation orders for an annotated parse-tree.

CH4.6 CSE244 Draw the Tree Example 3*5+4n

CH4.7 CSE244 Example with Inherited Attributes  Even though inherited can be simulated by synthesized it is more natural to write Syntax- Directed Definitions using inherited.  …below in is an inherited attribute of  …below in is an inherited attribute of L PRODUCTIONSEMANTIC RULE D  T LL.in = T.type T  int T.type = integer T  real T.type = real L  L 1, idL 1.in = L.in addtype(id.entry, L.in) L  id addtype(id.entry, L.in)

CH4.8 CSE244 Draw the Tree Example real id 1, id 2, id 3

CH4.9 CSE244 Syntax Trees  Decoupling Translation from Parsing-Trees.  Syntax-Tree: an intermediate representation of the compiler’s input.  Example Procedures: mknode, mkleaf  Employment of the synthesized attribute nptr (pointer) PRODUCTIONSEMANTIC RULE E  E 1 + TE.nptr = mknode(“+”,E 1.nptr,T.nptr) E  E 1 - TE.nptr = mknode(“-”,E 1.nptr,T.nptr) E  TE.nptr = T.nptr T  (E) T.nptr = E.nptr T  idT.nptr = mkleaf(id, id.lexval) T  numT.nptr = mkleaf(num, num.val)

CH4.10 CSE244 Draw the Syntax Tree a-4+c

CH4.11 CSE244 Bottom-Up Evaluation of S-Attributed Definitions  S-attributed Definition: Syntax-Directed Definition using only Synthesized attributes.  Stack of a LR(1) parser contains states.  Recall that each state corresponds to some grammar symbol (and many different states might correspond to the same grammar symbol)  Extend stack entries to include the attribute(s).  Modify stack contents as productions are selected. StateVal XX.x YY.y ZZ.z Top A  XYZ A.a = f(X.x,Y.y,Z.z) =f(val[top-2],val[top-1],val[top]) StateVal AA.a New Top

CH4.12 CSE244Example PRODUCTIONSEMANTIC RULE L  E nprint(E.val) E  E 1 + TE.val = E 1.val + T.val E  TE.val = T.val T  T 1 * FT.val = T 1.val * F.val T  FT.val = F.val F  (E)F.val = E.val F  digitF.val = digit.lexval PRODUCTIONSEMANTIC RULE L  E nprint(val[top]) E  E 1 + Tval[ntop] = val[top-2]+val[top] E  T T  T 1 * Fval[ntop] = val[top-2]*val[top] T  F F  (E)val[ntop] = val[top-1] F  digit

CH4.13 CSE244Execution Example: 3*5+4n