COMPILER CONSTRUCTION (Evaluation Orders for SDD s)

Similar documents
Syntax-Directed Translation

Syntax Directed Translation

Compilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1

5. Syntax-Directed Definitions & Type Analysis

Abstract Syntax Trees Synthetic and Inherited Attributes

Syntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions

COMPILER CONSTRUCTION (Intermediate Code: three address, control flow)

CSE302: Compiler Design

Syntax-Directed Translation

Syntax-Directed Translation Part I

We now allow any grammar symbol X to have attributes. The attribute a of symbol X is denoted X.a

[Syntax Directed Translation] Bikash Balami

Syntax-Directed Translation. Introduction

Programming Languages

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Chapter 4. Action Routines

Principles of Programming Languages

Syntax-Directed Translation

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

Principles of Programming Languages

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

Abstract Syntax Tree

Syntax-Directed Translation Part II

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 8a Syntax-directed Transla1on Elias Athanasopoulos

Syntactic Directed Translation

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

Syntax-Directed Translation

Semantic Analysis. Role of Semantic Analysis

Summary: Semantic Analysis

Static and Dynamic Semantics

State Space Search in AI

Syntax-Directed Translation

Semantic Analysis with Attribute Grammars Part 3

Chapter 4 :: Semantic Analysis

Compiler Principle and Technology. Prof. Dongming LU April 15th, 2019

Semantic Analysis Attribute Grammars

Abstract Syntax Trees & Top-Down Parsing

Syntax Directed Translation

Compiler Construction

Advances in Compilers

COP5621 Exam 3 - Spring 2005

UNIT IV INTERMEDIATE CODE GENERATION

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Syntax Directed Translation

CS A331 Programming Language Concepts

4. Semantic Processing and Attributed Grammars

COP4020 Spring 2011 Midterm Exam

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

Syntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG

Chapter 18: Decidability

A Simple Syntax-Directed Translator

VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur

2068 (I) Attempt all questions.

Context-sensitive Analysis Part II Chapter 4 (up to Section 4.3)

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

Lecture 14 Sections Mon, Mar 2, 2009

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 309

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

Context-sensitive Analysis

Chapter 2: Syntax Directed Translation and YACC

Error Handling Syntax-Directed Translation Recursive Descent Parsing

INSTITUTE OF AERONAUTICAL ENGINEERING

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Semantic Analysis with Attribute Grammars Part 1

DEPARTMENT OF INFORMATION TECHNOLOGY / COMPUTER SCIENCE AND ENGINEERING UNIT -1-INTRODUCTION TO COMPILERS 2 MARK QUESTIONS

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

LR Parsing LALR Parser Generators

Question Bank. 10CS63:Compiler Design

Semantic Analysis. Compiler Architecture

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

Semantic analysis. Syntax tree is decorated with typing- and other context dependent

CS 406: Syntax Directed Translation

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

More On Syntax Directed Translation

Context Free Grammars. CS154 Chris Pollett Mar 1, 2006.

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1

198 Glossary Two components p v, q w split productions.93 p v, q w split production constructors.93 pi is the i th nonterminal of p (list indexing). p

Compila(on /15a Lecture 6. Seman(c Analysis Noam Rinetzky

A language is a subset of the set of all strings over some alphabet. string: a sequence of symbols alphabet: a set of symbols

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

Context-Free Grammars and Languages (2015/11)

Single-pass Static Semantic Check for Efficient Translation in YAPL

Roll No. :... Invigilator's Signature :. CS/B.Tech(CSE)/SEM-7/CS-701/ LANGUAGE PROCESSOR. Time Allotted : 3 Hours Full Marks : 70

Attribute Grammars. Attribute Grammars

Compiler Lab. Introduction to tools Lex and Yacc

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

Introduction to GNU-Octave

Normal Forms for CFG s. Eliminating Useless Variables Removing Epsilon Removing Unit Productions Chomsky Normal Form

FreePascal changes: user documentation

Intermediate Code Generation

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

COMP 181. Prelude. Prelude. Summary of parsing. A Hierarchy of Grammar Classes. More power? Syntax-directed translation. Analysis

FS22k Transducer User Manual

Context-sensitive Analysis

Transcription:

COMPILER CONSTRUCTION (Evaluation Orders for SDD s) Prof. K R Chowdhary Email: kr.chowdhary@jietjodhpur.ac.in Campus Director, JIET, Jodhpur Thursday 18 th October, 2018 kr chowdhary Code generation 1/ 14

Dependency graphs Dependency graphs are a useful tool for determining an evaluation order for the attribute instances in a given parse tree. A dependency graph helps us determine how attributes values can be computed. Two important classes of SDDs are: S-attributed and the more general L-attributed SDDs. An edge from one attribute instance to another means that the value of the first is needed to compute the second. Edges express constraints implied by the semantic rules. kr chowdhary Code generation 2/ 14

Dependency graphs A.b M X.a X.c A XY N B.i Y BC Figure 1: Sample Dependency graphs. kr chowdhary Code generation 3/ 14

Annotated Parse tree and Dependency graph Example Synthesize E.val from E 1.val and E 2.val. Solution. Consider the following production and rule: Node N E val Production Rule : E E 1 +T Semantic Rule : E.val =E 1.val+T.val E 1 val + T val Figure 2: E.val is synthesized from E 1.val and E 2.val. kr chowdhary Code generation 4/ 14

SDD for a * b Example Construct a Dependency graph for the annotated parse tree of Fig. 3, whose SDD is given in Table 1. Table 1: An SDD based on a grammar suitable for top-down parsing. S.No. Production Semantic Rules 1. T FT T.inh=F.val T.val =T.syn 2. T FT 1 T 1.inh=T 1.inh F.val T.syn=T 1.syn 3. T ε T.syn=T.inh 4. F digit F.val = digit.lexval kr chowdhary Code generation 5/ 14

Annotated Parse tree T.val = 24 F.val = 4 T.inh = 4 T.syn = 24 digit.lexval = 4 F.val = 6 T 1 T 1.inh = 24.syn = 24 digit.lexval = 6 ε Figure 3: Annotated parse tree for 4 6. kr chowdhary Code generation 6/ 14

Annotated parse tree and its dependency graph T 9 val digit F 3 val 1 lexval inh 5 F 4 val T 8 syn 6 inh T 7 syn digit 2 lexval ε Figure 4: Dependency graph ( ) and annotated parse (...) tree of Fig. 3. kr chowdhary Code generation 7/ 14

Ordering the evaluation of Attributes If the dependency graph has an edge from node M to node N, then the attribute corresponding to M must be evaluated before the attribute of N. Sequences of nodes N 1, N 2,..., N k such that if there is an edge of the dependency graph from N i to N j ; where i <j. Such an ordering transforms a directed graph into a linear order, called topological sort of the graph. If there is any cycle in the graph, then there are no topological sorts possible. Since there are no cycles, we can surely find a node with no edge entering. For if there were no such node, we could proceed from predecessor to predecessor until we came back to some node we had already seen, yielding a cycle. kr chowdhary Code generation 8/ 14

Topological Sort Example Topological Sort. The dependency graph of Fig. 4 has no cycles. One topological sort is the order in which the nodes have already been numbered: 1,2,...,9. Every edge of the graph goes from a node to a higher-numbered node, so this order is surely a topological sort. The other topological sort is 1,3,5,2,4,6,7,8,9. kr chowdhary Code generation 9/ 14

S-Attributed Definitions Translations can be implemented using classes of SDD s that guarantee an evaluation order, since they do not permit dependency graphs with cycles. Definition An SDD is S-attributed if every attribute is synthesized. Example The SDD of Table. 2 is an example of an S-attributed definition. Each attribute, L.val, E.va1, T.val, and F.val is synthesized. (See the table in next slide.) kr chowdhary Code generation 10/ 14

S-Attributed Definitions Table 2: Syntax-directed definition of a simple desk calculator (S-attributed). Production Semantic Rules 1. L E n L.val =E.val 2. E E 1 +T E.val =E 1.val+T.val 3. E T E.val =T.val 4. T T 1 F T.val =T 1.val F.val 5. T F T.val =F.val 6. F (E) F.val =E.val 7. F digit F.val = digit.lexval When an SDD is S-attributed, we can evaluate its attributes in any bottom-up order of the nodes of the parse tree. kr chowdhary Code generation 11/ 14

L-Attributed Definitions For L-attributed definitions, edges of dependency graph corresponding to the attributes can go from left to right in productions. An attribute must be either 1 Synthesized, or 2 Inherited, but with the rules limited as follows. let A X 1 X 2...X n, and an inherited attribute X i.a is computed by a rule associated with this production. Then the rule may use only: 1 Inherited attributes associated with the head A. 2 Either inherited or synthesized attributes associated with the occurrences of symbols X 1, X 2,..., X i 1 occurring to the left of X i. 3 Inherited or synthesized attributes associated with of X i itself, such that there are no cycles in a dependency graph of this X i. kr chowdhary Code generation 12/ 14

L-Attributed Definitions Theorem Show that the SDD given in Table 1 is L-attributed. Proof. 1 The SDD in Table 1 is L-attributed. To see why, consider the semantic rules for inherited attributes: 1 and 2. 2 The first of these rules defines the inherited attribute T.inh using only F.val, and F appears to the left of T in the production body, as required. The second rule defines T 1.inh using the inherited attribute T.inh associated with the head, and F.va1, where F appears to the left of T 1 in the production body. 3 In each of these cases, the rules use information from above or from the left, as required by the class. The remaining attributes are synthesized. Hence, the SDD is L-attributed. kr chowdhary Code generation 13/ 14

L-Attributed Definitions Example Any SDD containing the following production and rules cannot be L-attributed: Production:A BC Semantic Rules :A.s = B.b;B.i =f(c.c,a.s) Solution. The first rule, A.s =B.b, is a legitimate rule in either an S-attributed or L-attributed SDD. It defines a synthesized attribute A.s in terms of an attribute b at child B (that is, a symbol within the production body). The second rule defines an inherited attribute B.i, so the entire SDD cannot be S-attributed. Further, although the rule is legal, the SDD cannot be L-attributed, because the attribute C.c is used to help define B.i, and C is to the right of B in the production body. kr chowdhary Code generation 14/ 14