Chapter 5. Syntax-Directed Translation

Slides:



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

Compiler Designs and Constructions
SYNTAX DIRECTED TRANSLATION 11CS Types of Attributes There are two types of attributes for non- terminals :- Synthesized Attributes : For a non-terminal.
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
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 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(“+”)
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 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
Syntax-Directed Translation
Chapter 2 A Simple Compiler
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.
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.
Syntax-Directed Translation
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.
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.
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.
Overview of Previous Lesson(s) Over View  In syntax-directed translation 1 st we construct a parse tree or a syntax tree then compute the values 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.
Syntax Directed Definition and Syntax directed Translation
UNIT – 5 SYNTAX-DIRECTED TRANSLATION
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.
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.
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.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
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.
Chapter 5: Syntax Directed Translation
Syntax-Directed Translation
Compiler Construction
Chapter 5 Syntax Directed Translation
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Semantic Analysis Check semantics Error reporting
Syntax-Directed Translation Part I
Syntax-Directed Translation Part II
CS 3304 Comparative Languages
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Syntax-Directed Definition
SYNTAX DIRECTED TRANSLATION
פרק 5 תרגום מונחה תחביר תורת הקומפילציה איתן אביאור.
Syntax-Directed Translation Part II
COMPILER DESIGN 11CS30013 & 11CS30014 Group October 2013
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part II
Chapter 4 Action Routines.
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Syntax-Directed Translation
Syntax-Directed Translation Part II
Directed Acyclic Graphs (DAG)
Syntax-Directed Translation Part I
Chapter 5 Syntax Directed Translation
Presentation transcript:

Chapter 5. Syntax-Directed Translation

Fig. 5.2. Syntax-directed definition of a simple desk calculator Production Semantic Rules L  E n print ( E.val ) E  E1 + T E.val := E1.val + T.val E  T E.val := T.val T  T1 * F T.val := T1.val + F.val T  F T.val := F.val F  ( E ) F.val := E.val F  digit F.val := digit.lexval Fig. 5.2. Syntax-directed definition of a simple desk calculator

Fig. 5.3. Annotated parse tree for 3*5+4n.

Fig. 5.4. Syntax-directed definition with inherited attribute L.in. Production Semantic Rules D  T L L.in := T.type T  int T.type := integer T  real T.type := real L  L1 , id L1.in := L.in addtype ( id.entry , L.in ) L  id Fig. 5.4. Syntax-directed definition with inherited attribute L.in.

Fig. 5.5. Parse tree with inherited attribute in at each node labeled L.

Fig. 5.6. E.val is synthesized from E1.val and E2.val

Fig. 5.7. Dependency graph for parse tree of Fig. 5.5

Fig. 5.16. Implementation of a desk calculator with an LR parser. Production Code Fragment L  E n print ( val [top] ) E  E1 + T val [ntop] := val [top – 2]+val [top] E  T T  T1 * F val [ntop] := val [top – 2]×val [top] T  F F  ( E ) val [ntop] := val [top – 1] F  digit Fig. 5.16. Implementation of a desk calculator with an LR parser.

L-Attributed Definitions A syntax-directed definition is L-attribute if each inherited attribute of Xj, 1≤ j≤n, on the right side of A → X1X2 · · · Xn , depends only on 1. the attributes of the symbols X1 , X2 , · · · , Xj-1 to the left of Xj in the production and the inherited attributes of A. Note that every S-attributes definition is L-attributed, because the restrictions (1) and (2) apply only to inherited attributes

Example 5.17. The type of an identifier can be passed by copy rules using inherited attributes as shown in Fig. 5.32 (adapted from Fig. 5.7). We shall first examine the moves made by a bottom-up parser on the input real p, q, r then we show how the value of attirbute T.type can be accessed when the productions for L are applied. The translation scheme we wish to implement is D  T L { L.in := T.type } T  int { T.type := integer } T  real { T.type := real } L  { L1.in := L.in } L1 , id { addtype ( id.entry , L.in ) } L  id { addtype ( id.entry , L.in ) }

Fig.5.32. At each node for L, L.in = T.type. If we ignore the actions in the above translation scheme, the sequence of moves made by the parser on the input of Fig. 5.32. is as in Fig. 5.33. For clarity, we show the corresponding grammar symbol instead of a stack state and the actual identifier instead of the token id.

Input state Production Used real p,q,r − real T T → real ,q,r T p T L L → id q,r T L , ,r T L , q L → L , id r T L , r D D → T L Fig. 5.33.Whenever a right side for L is reduced, T is just below the right side.

Fig. 5.34. The value of T.type is used in place of L.in Production Code Fragment D  T L ; T  int val [ntop] := integer T  real val [ntop] := real L  L1 , id addtype (val [top] , val [top−3] ) L  id addtype (val [top] , val [top−1] ) Fig. 5.34. The value of T.type is used in place of L.in

S → aAC C.i := A.s S → bABC C → c C.s := g ( C.i ) Example 5.18. As an instance where we cannot predict the position, consider the following translation scheme: (5.6) C inherits the synthesized attribute A.s by a copy rule. Note that there may or may not be a B between A and C in the stack. When reduction by C → c is performed, the value of C.i is either in val [top−1]or in val [top−2], but it is not clear which case applies. Production Semantic Rules S → aAC C.i := A.s S → bABC C → c C.s := g ( C.i )

Fig. 5.35. Copying an attribute value through a marker M. Production Semantic Rules S → aAC C.i := A.s S → bABMC M.i := A.s ; C.i := M.s C → c C.s := g ( C.i ) M → є M.s := M.i є (a) original production (b) modified dependencies Fig. 5.35. Copying an attribute value through a marker M.