Syntax Directed Translation

Slides:



Advertisements
Similar presentations
Compiler Designs and Constructions
Advertisements

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.
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 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 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
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.
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.
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
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
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.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
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.
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
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
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
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:

Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs, CT 06269-3155 aggelos@cse.uconn.edu http://www.cse.uconn.edu/~akiayias

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 A   have the form: b = f (c1,…,cn) where (b is synthesized) b is an attribute of A and c1…cn are attributes of symbols in . (b is inherited) b is an attribute of some symbol in  and c1…cn are attributes of symbols in A,

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; these are “Side-effects”…

Annotated Parse-Trees Parse-tree that also shows the values of the attributes at each node. Values of Attributes in nodes of annotated parse-tree are either, initialized to constant values or by the lexical analyzer. determined by the semantic-rules.

Evaluating Attributes. 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.

Example of a Syntax-Directed Definition Grammar symbols: L, E, T, F, n , + , * ,( , ) , digit Non-terminals E, T, F have an attribute called val Terminal digit has an attribute called lexval The value for lexval is provided by the lexical analyzer. PRODUCTION SEMANTIC RULE 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

Draw the Tree Example 3*5+4n

Draw the Tree Example 3*5+4n L Print(19) E val=19 T val=15 T val=4 F val=3 F val=5 F val=4 digit lexval =3 digit lexval =5 digit lexval =4 * + n

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 L PRODUCTION SEMANTIC RULE 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 addtype(id.entry, L.in)

Draw the Tree Example real id1, id2 , id3

Draw the Tree Example real id1, id2 , id3 D , , L in=real L in=real T addtype(id3,real) L in=real addtype(id2,real) L in=real T type=real addtype(id1,real) L in=real id entry=id1 id entry=id2 id entry=id3 , , real

Dependency Graph Directed Graph Shows interdependencies between attributes. Construction: Put each semantic rule into the form b=f(c1,…,ck) by introducing dummy synthesized attribute b for every semantic rule that consists of a procedure call. E.g., L  E n print(E.val) Becomes: dummy = print(E.val) Etc.

Dependency Graph Construction for each node n in the parse tree do for each attribute a of the grammar symbol at n do construct a node in the dependency graph for a for each semantic rule b = f(c1,…,cn) associated with the production used at n do for i= 1 to n do construct an edge from the node for ci to the node for b

Example I L Print(19) E val=19 T val=15 T val=4 T val=3 F val=3 digit lexval =3 digit lexval =5 digit lexval =4 * + n

Example I dummy val=19 val=15 val=4 val=3 val=3 val=5 val=4 digit lexval =3 digit lexval =5 digit lexval =4

Example II D , , L in=real L in=real T type=real L in=real id addtype(id3,real) L in=real addtype(id2,real) L in=real T type=real addtype(id1,real) L in=real id entry=id1 id entry=id2 id entry=id3 , , real

Example II in=real dummy in=real dummy T type=real in=real dummy id entry=id1 id entry=id2 id entry=id3

Evaluating Attributes Notion: Directed Acyclic Graph Dependency graph should be a DAG (why?) Any topological sort of the directed acyclic graph can be used as a “guide” for attribute evaluation.

Example I dummy val=19 val=15 val=4 val=3 val=3 val=5 val=4 digit lexval =3 digit lexval =5 digit lexval =4

Example I as a DAG A topological sort 6,8,7,5,4,3,11,10,9,2,1 1 2 3 9

Example II in=real dummy in=real dummy T type=real in=real dummy id entry=id1 id entry=id2 id entry=id3

Example II as a DAG A topological sort: 8,10,3,1,2,4,9,5,6,7 1 2 4 5 3

Syntax Trees Decoupling Translation from Parsing-Trees. Syntax-Tree: an intermediate representation of the compiler’s input. Example Procedures: mknode, mkleaf (create a labeled node – return pointer) Employment of the synthesized attribute nptr (type:pointer) PRODUCTION SEMANTIC RULE E  E1 + T E.nptr = mknode(“+”,E1.nptr ,T.nptr) E  E1 - T E.nptr = mknode(“-”,E1.nptr ,T.nptr) E  T E.nptr = T.nptr T  (E) T.nptr = E.nptr T  id T.nptr = mkleaf(id, id.lexval) T  num T.nptr = mkleaf(num, num.val)

Draw the Tree a-4+c

Draw The Tree a-4+c + - E nptr c 4 E nptr a T nptr E nptr T nptr id lexval =a num val =4 id lexval =c - +

Using DAGs for expressions Consider the expression: a+a*(b-c)+(b-c)*d

Using DAGs for expressions Consider the expression: a+a*(b-c)+(b-c)*d + + + + * * * * - - - a a b c b c d a b c d

Question How should we modify mkleaf and mknode so that they produce the DAG instead?

Question How should we modify mkleaf and mknode so that they produce the DAG instead? mknode and mkleaf create new nodes only when necessary. First they search whether one node with the same exact properties (label + pointers to children if any) has already been defined. If yes node is reused If no, new node is created.

DAG representation + i 10 input i := i + 10 DAG form Array Representation: := + i 10 1 id pointer to i 2 num 10 3 + 1,2 4 := 1,3 …

Implementing the DAG representation For each node with a signature <op,l,r> Search whether it exists in the list of nodes and if yes, return its value number (e.g., row in the array). most basic implementation: linear search. More sophisticated implementations: arrange nodes in a “array of lists” according to HASH(op || l || r). HASH(op || l || r) = bucket the node with signature <op, l, r> must be placed. Search time drops to O(bucket-length_search + hash-computation)

Bottom Up Evaluation of S-Attributed Definitions A syntax-directed definition is S-Attributed if all attributes are synthesized. Dependency graph is a (directed) tree “pointing upwards” Can be evaluated in bottom up fashion. => Consistent with (bottom-up) Shift/Reduce parsing. Possible to do the evaluation of the attributes using the stack at the time of “reduce” operations!

Using the Stack to compute the attributes Suppose the stack of a general Shift/Reduce parser is equal to $...XYZ Suppose the grammar symbols A, X, Y, Z have the attributes a,x,y,z respectively. The augmented stack with the attributes is as follows: $...[X,X.x=v1][Y,Y.y=v2][Z, Z.z=v3] If we reduce by the production AXYZ that has the semantic action A.a = f(X.x, Y.y, Z.z) we will modify the stack as follows: $...[A,A.a=f (v1,v2,v3)]

Recall the S-Attributed Definition PRODUCTION SEMANTIC RULE 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

Semantic Rules Using the Stack of the S/R parser PRODUCTION SEMANTIC RULE L  E n print(val[top]) E  E1 + T val[ntop]=val[top-2]+val[top] E  T val[ntop]=val[top] T  T1 * F val[ntop]=val[top-2]*val[top] T  F val[ntop]=val[top] F  (E) val[ntop]=val[top-1] F  digit val[ntop]=val[top] top = top of the stack ntop = top of the stack after popping right hand side of production. val[…] = attribute value of stack contents

A trace of a Shift Reduce Parser with Attribute Evaluation STACK INPUT ACTION

A trace of a Shift Reduce Parser with Attribute Evaluation $ 3*5+4n$ shift $[digit,3] *5+4n$ reduce Fdigit $[F,3] reduce TF $[T,3] $[T,3][*,.] 5+4n$ $[T,3][*,.][digit,5] +4n$ $[T,3][*,.][F,5] reduce TT*F $[T,15] reduce ET $[E,15] $[E,15][+,.] 4n$ $[E,15][+,.][digit,4] n$ $[E,15][+,.][F,4] $[E,15][+,.][T,4] reduce EE*T $[E,19] $[E,19][n,.] reduce LEn ; print(19) $[L,.] ACCEPT