Syntactic Directed Translation

Similar documents
Syntactic Directed Translation

Grammars. CS434 Lecture 15 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Context-sensitive Analysis

Context-sensitive Analysis Part II

There is a level of correctness that is deeper than grammar. There is a level of correctness that is deeper than grammar

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

Context-sensitive Analysis

Context-sensitive Analysis. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

CS 406/534 Compiler Construction LR(1) Parsing and CSA

Syntax Directed Translation

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

Syntactic Directed Translation

CSCI Compiler Design

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables

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

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Intermediate Code Generation

Intermediate Code Generation

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412

CMPT 379 Compilers. Parse trees

Semantic Analysis with Attribute Grammars Part 1

Parsing II Top-down parsing. Comp 412

Lexical Analysis. Introduction

Summary: Semantic Analysis

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

Syntax Directed Translation

Syntactic Analysis. Top-Down Parsing

CSCI Compiler Design

Syntax-Directed Translation

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

5. Semantic Analysis!

Semantic Analysis with Attribute Grammars Part 3

Syntax-Directed Translation

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

Compiler Design Concepts. Syntax Analysis

intermediate code generator syntax tree token stream intermediate representation parser checker tree

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Syntax-Directed Translation

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

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

Code Shape II Expressions & Assignment

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

CSCI Compiler Design

Chapter 4 - Semantic Analysis. June 2, 2015

Generating Code for Assignment Statements back to work. Comp 412 COMP 412 FALL Chapters 4, 6 & 7 in EaC2e. source code. IR IR target.

Front End. Hwansoo Han

Syntax-Directed Translation Part II

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Syntax Analysis, III Comp 412

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Syntax-Directed Translation

Building a Parser Part III

Abstract Syntax Trees & Top-Down Parsing

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Syntax Analysis, III Comp 412

MIT Top-Down Parsing. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Programming Languages

CS 406: Syntax Directed Translation

Lecture 14 Sections Mon, Mar 2, 2009

Chapter 4 :: Semantic Analysis

CSE3322 Programming Languages and Implementation

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

Syntax-Directed Translation. Introduction

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

COP5621 Exam 3 - Spring 2005

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

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

Abstract Syntax Trees Synthetic and Inherited Attributes

CSE 12 Abstract Syntax Trees

LECTURE 3. Compiler Phases

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

A Short Summary of Javali

CSCI Compiler Design

Alternatives for semantic processing

Downloaded from Page 1. LR Parsing

Parsing Part II (Top-down parsing, left-recursion removal)

COS 320. Compiling Techniques

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

A programming language requires two major definitions A simple one pass compiler

CS525 Winter 2012 \ Class Assignment #2 Preparation

Syntactic Analysis. Top-Down Parsing. Parsing Techniques. Top-Down Parsing. Remember the Expression Grammar? Example. Example

Semantic Analysis Attribute Grammars

More On Syntax Directed Translation

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Lecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

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

Static semantics. Lecture 3-6: Semantics. Attribute grammars (2) Attribute grammars. Attribute grammars example. Dynamic semantics

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Introduction to Parsing. Comp 412

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

Compiler Construction

ECE468 Fall 2003, Final Exam

Transcription:

Syntactic Directed Translation Attribute Grammars & Syntax-Directed Definitions Copyright 206, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission to make copies of these materials for their personal use.

What is Syntax-Directed Translation? Translation Process guided by Context-Free Grammars Attach Attributes to Grammar Symbols Associated Semantic Rules with Productions that use these Values Attributive Grammar Two Flavors: Syntax-Directed Definitions (order is implicit; more abstract) Translation Schemes (explicit order; more concrete) Why Use This? Very Powerful Mechanism Used to Build Parse Tree Perform Semantic Analysis such as Type Checking (see book) Generate Code 2

Attribute Grammars What is an Attribute Grammar? A Context-Free Grammar Augmented with a Set of Rules Each Symbol has a Set of Values, or Attributes The Rules specify how to compute a Value for each Attribute Example Grammar Number Sign Sign + 0 This grammar describes signed binary numbers We would like to augment it with rules that compute the decimal value of each valid input string 3

Examples For Number Sign Number Sign For 0 Number Sign Sign Sign Sign Sign Sign 0 Sign 0 0 Number Sign 0 4

Attribute Grammars Add rules to compute the decimal value of a signed binary number Productions Attribution Rules Number Sign.pos 0 If Sign.neg then Number.val.val else Number.val.val Sign + Sign.neg false Sign.neg true 0.pos 0.pos +.pos 0.pos 0.val.val +.val.pos.pos.val.val 0.val 0.val 2.pos Symbol Number Sign Attributes val neg pos, val pos, val 5

Back to the Examples For neg true Sign Number Number.val.val.pos 0.val.val.pos 0.val 2.pos One possible evaluation order:.pos 2 Sign.neg 3.pos 4.val 5.val 6 Number.val Other orders are possible Possible Data-Flow Model for Evaluation Independent Attributes First Others in Order as Input Values become Available 6

Back to the Examples Rules + parse tree imply an attribute dependence graph For neg true Sign Number Number.val.val.pos 0.val.val.pos 0.val 2.pos One possible evaluation order:.pos 2 Sign.neg 3.pos 4.val 5.val 6 Number.val Other orders are possible Possible data-flow model for Evaluation Independent Attributes First Evaluation order must be consistent with the attribute dependence graph Others in Order as Input Values become Available 7

Back to the Examples Number 5 This is the complete attribute dependence graph for 0. Sign neg: true pos: 0 5 It shows the flow of all attribute values in the example. pos: 4 pos: 0 Some flow downward Inherited Attributes pos: 2 4 pos: 0 Some flow upward Synthesized Attributes pos: 2 4 0 A rule may use attributes in the parent, children, or siblings of a node For 0 8

Using Attribute Grammars Attribute Grammars Can Specify Context-Sensitive Actions Take Values from Syntax Perform Computations with Values Insert Tests, Logic, Synthesized Attributes Use values from self, children & from constants S-attributed grammars Evaluate in a single bottom-up pass Good match for LR parsing Inherited Attributes Use values from self, parent, siblings & constants Directly express context Can rewrite to avoid them Thought to be more natural Not easily done at parse time 9

Using Attribute Grammars Attribute Grammars Can Specify Context-Sensitive Actions Take Values from Syntax Perform Computations with Values Insert Tests, Logic, Synthesized Attributes Use values from self, children & from constants Inherited Attributes Use values from self, parent, siblings & constants Good match for LR parsing Not easily done at parse time 0

Attributes, Rules and Evaluation Order Attributes: Synthesized: Depend on Values from Children or Itself Inherited: Depend on Values from Parent, Siblings or Itself How to Determine the Evaluation Order of Rules? Construct the Parse Tree with Attributes Build an Attribute Dependence Graph Topological Sort It and Evaluate the Rules Input String Parse Tree Dependency Graph Evaluation Order for Semantic Rules

Evaluation Methods Dynamic, dependence-based methods Build the Parse Tree Build the Dependence Graph Topological Sort the Dependence Graph Evaluate Attributes in Topological Order Rule-based methods Analyze Rules at Compiler-Generation Time Determine a Fixed (static) Ordering Evaluate Nodes in that Order Oblivious Methods Ignore Rules & Parse Tree Pick a Convenient Order (at design time) & Use It (treewalk) (passes, dataflow) 2

Example Number Sign 0 For 0 3

Example Number Sign neg: pos: 0 pos: pos: pos: pos: pos: 0 For 0 4

Example Number Inherited Attributes Sign neg: pos: 0 pos: pos: 0 pos: 2 pos: pos: 2 0 For 0 5

Example Number Synthesized Attributes Sign neg: true pos: 0 pos: pos: 0 pos: 2 pos: 0 pos: 2 4 0 For 0 6

Example Number 5 Synthesized Attributes Sign neg: true pos: 0 5 pos: 4 pos: 0 pos: 2 4 pos: 0 pos: 2 4 0 For 0 7

Example Number 5 If we show the computation... Sign neg: true pos: 4 pos: 0 5 pos: 0 & then peel away the parse tree... pos: 2 4 pos: 0 pos: 2 4 0 For 0 8

Example 5 All that is left is the attribute dependence graph. neg: true pos: 0 5 This succinctly represents the flow of values in the problem instance. pos: 2 4 pos: 2 4 pos: 4 0 pos: 0 pos: 0 For 0 The dynamic methods sort this graph to find independent values, then work along graph edges. The rule-based methods try to discover good orders by analyzing the rules. The oblivious methods ignore the structure of this graph. The dependence graph must be acyclic 9

Circularity We can only Evaluate Acyclic Instances We can prove that some grammars can only generate instances with acyclic dependence graphs Largest such class is strongly non-circular grammars (SNC ) SNC grammars can be tested in polynomial time Failing the SNC test is not conclusive Many evaluation methods discover circularity dynamically Bad property for a compiler to have 20

A Circular Attribute Grammar Productions Attribution Rules Number.a 0 0.a 0.a + 0.b.b.c.b +.val 0.b 0.a + 0.c +.val 0.val 0.val 2.pos 2

Circular Grammar Sign Number a: b: c: a: b: c: a: 0 b: c: Number 0.a 0.a 0.a + 0.b.b.c.b +.val 0.b 0.a + 0.c +.val 0 For 0 0.val 0.val 2.pos 22

Circular Grammar Sign Number a: b: c: a: b: c: a: 0 b: c: Number 0.a 0.a 0.a + 0.b.b.c.b +.val 0.b 0.a + 0.c +.val 0 For 0 0.val 0.val 2.pos 23

Circular Grammar Sign Number a: b: c: a: b: c: a: 0 b: c: Number 0.a 0.a 0.a + 0.b.b.c.b +.val 0.b 0.a + 0.c +.val 0 For 0 0.val 0.val 2.pos 24

Circular Grammar Sign Number a: b: c: a: b: c: a: 0 b: c: Number 0.a 0.a 0.a + 0.b.b.c.b +.val 0.b 0.a + 0.c +.val 0 For 0 0.val 0.val 2.pos 25

Circular Grammar Sign Number a: b: c: a: b: c: a: 0 b: c: Number 0.a 0.a 0.a + 0.b.b.c.b +.val 0.b 0.a + 0.c +.val 0 For 0 0.val 0.val 2.pos Here is the circularity 26

Circular Grammar Sign Number a: b: c: a: b: c: a: 0 b: c: Number 0.a 0.a 0.a + 0.b.b.c.b +.val 0.b 0.a + 0.c +.val 0 For 0 0.val 0.val 2.pos Here is the circularity 27

An Example: Estimating Cycle Counts Grammar for a Basic Block Block 0 Block Assign " Assign Assign Ident = Expr ; Expr 0 Expr + Term " Expr Term " Term Term 0 Term * Factor " Term / Factor " Factor Factor ( Expr ) " Number " Identifier Assumptions: Each Operation has a COST Add them, Bottom Up Assume a Load per Value Assume no Reuse via Registers Simple problem for an Attributive Grammar 28

An Example (continued) Block0 Block Assign Block0.cost Block.cost + Assign.cost # Assign Block0.cost Assign.cost Assign Ident = Expr ; Assign.cost COST(store) + Expr.cost Expr0 Expr + Term Expr0.cost Expr.cost + COST(add) + Term.cost # Expr Term Expr0.cost Expr.cost + COST(add) + Term.cost # Term Expr0.cost Term.cost Term0 Term * Factor Term0.cost Term.cost + COST(mult ) + Factor.cost # Term / Factor Term0.cost Term.cost + COST(div) + Factor.cost # Factor Term0.cost Factor.cost Factor ( Expr ) Factor.cost Expr.cost # Number Factor.cost COST(loadI) # Identifier Factor.cost COST(load) These are all synthesized attributes! Values flow from RHS to LHS in prod ns 29

An Example (continued) Block x = y + z y = y + z = x / 2 Assign Block Assign Assign Id = Expr cost(loadi) = cost(load) = 2 cost(store) = 2 cost(add) = 3 cost(div) = 9 Id x = Expr Expr + Term Term Factor Factor Id Id z y Id = Expr y Expr + Term Term Factor Id Factor num y z Expr / Term Term Factor Factor num Id 2 x 30

An Example (continued) x = y + z y = y + z = x / 2 cost(loadi) = cost(load) = 2 cost(store) = 2 cost(add) = 3 cost(div) = 9 Id x Assign = Expr Expr + Term Term Factor Id y c: 2 c: 2 c: 2 c: 7 Block Factor Id z c: 2 c: 2 c: 7 Block Assign c: 9 c: 8 Id = Expr y Expr + Term Term Factor Id y c: 2 c: 2 c: 2 c: 3 c: 6 Factor num c: c: Assign Id = Expr z Expr / Term Term Factor Id x c: 4 c: 2 c: 2 c: 2 c: 2 Factor num 2 c: c: 3

An Example (continued) Properties of the Example Grammar All Attributes are Synthesized S-Attributed Grammar Rules can be Evaluated Bottom-Up in a Single Pass Good fit to Bottom-Up, Shift/Reduce Parser Easily Understood Solution Seems to fit the Problem Well 32

Augmenting Example - Tracking Loads Capture Values that are Loaded in Registers Avoid reloading the same value multiple times by Caching Track which values have been already loaded Assumes an Infinite Register Set to Hold Variables 33

A Better Execution Model Adding Load Tracking Need Sets Before and After for each production Must be initialized, updated, and passed around the tree Factor ( Expr ) Factor.cost Expr.cost ; Expr.Before Factor.Before ; Factor.After Expr.After # Number Factor.cost COST(loadi) ; Factor.After Factor.Before # Identifier If (Identifier.name Factor.Before) then Factor.cost COST(load); Factor.After Factor.Before Identifier.name else Factor.cost 0 Factor.After Factor.Before This looks more complex! 34

An Example (continued) x = y + z y = y + z = x / 2 cost(loadi) = cost(load) = 2 cost(store) = 2 cost(add) = 3 cost(div) = 9 b:{ } Id x b:{ } Assign = Expr b:{ } Expr + Term Term Factor Id y b:{y} c: 2 a:{y} c: 2 a:{y} b:{ } c: 9 a:{y,z} c: 2 a:{y} b:{y} c: 7 a:{y,z} Block Factor Id z c: 2 a:{y,z} c: 2 a:{y,z} Block Assign Id = Expr y b:{ } c: 5 a:{y,z} b:{y,z} b:{y,z} b:{y,z} b:{y,z} b:{y,z} Expr + Term Term Factor Id y b:{y,z} c: 0 a:{y,z} c: 0 a:{y,z} c: 0 a:{y,z} c: 6 a:{y,z} Factor num c: 29 a:{x,y,z} b:{y,z} c: 4 a:{y,z} Assign Id = Expr z c: a:{y,z} b:{y,z} b:{y,z} c: a:{y,z} b:{y,z} Expr / Term Term Factor Id x c: 4 a:{x,y,z} c: 2 a:{x,y,z} c: 2 a:{x,y,z} c: 2 a:{x,y,z} c: 2 a:{x,y,z} Factor num 2 c: a:{x,y,z} c: 35

A Better Execution Model Load Tracking adds Complexity But, most of it is in the copy rules Every production needs rules to copy Before & After A sample production Expr0 Expr + Term Expr0.cost Expr.cost + COST(add) + Term.cost ; Expr.Before Expr0.Before ; Term.Before Expr.After; Expr0.After Term.After These Copy Rules Multiply Rapidly Each creates an instance of the set Lots of work, lots of space, lots of rules to write 36

An Even Better Model What about Accounting for Finite Register Sets? Before & After must be of Limited Size Adds complexity to Factor Identifier Requires more sophisticated initialization Jump from Tracking Loads to Tracking Registers is Small Copy rules are already in place Some local code to perform the Allocation 37

Bottom Line Non-local computation needed lots of supporting rules Complex local computation was relatively easy The Problems: Copy Rules increase cognitive overhead Copy Rules increase space requirements Need copies of attributes Can use pointers, for even more cognitive overhead Result is an Attributed Tree Must build the parse tree Either search tree for answers or copy them to the root (somewhat subtle points) 38

Addressing the Problem If you gave this problem to a real programmer Introduce a central repository for facts Table of names Field in table for loaded/not loaded state Avoids all the copy rules, allocation & storage headaches All inter-assignment attribute flow is through table Clean, efficient implementation Good techniques for implementing the table When its done, information is in the table! Cures most of the problems Unfortunately, this design violates the functional paradigm Do we care? 39

Reworking the Example (with load tracking) Block 0 Block Assign " Assign cost 0; Assign Ident = Expr ; cost cost + COST(store); Expr 0 Expr + Term cost cost + COST(add); " Expr Term cost cost + COST(sub); " Term Term 0 Term * Factor cost cost + COST(mult); " Term / Factor cost cost + COST(div); " Factor Factor ( Expr ) " Number cost cost + COST(loadi); " Identifier { i hash(identifier); if (Table[i].loaded = false) { cost cost + COST(load); Table[i].loaded true; } } This looks cleaner & simpler! 40

Reworking the Example (with load tracking) Factor ( Expr ) " Number cost cost + COST(loadi); " Identifier { i hash(identifier); if (Table[i].loaded = false) { cost cost + COST(load); Table[i].loaded true; } } Factor ( Expr ) Factor.cost Expr.cost ; Expr.Before Factor.Before ; Factor.After Expr.After # Number Factor.cost COST(loadi) ; Factor.After Factor.Before # Identifier If (Identifier.name Factor.Before) { Factor.cost COST(load); Factor.After Factor.Before Identifier.name } else { Factor.cost 0 Factor.After Factor.Before } 4

Attribute Grammar Summary Augment CFG with Attributes and Rules Inherited and Synthesized Attributes Syntax-Directed Definitions Find Dependence Graph and Evaluation Order Useful for Semantic Analysis 42