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

Similar documents
Context-sensitive Analysis Part II

Context-sensitive Analysis

Context-sensitive Analysis

Syntactic Directed Translation

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

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

Syntax Directed Translation

Syntactic Directed Translation

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

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

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

CSCI Compiler Design

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

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

CMPT 379 Compilers. Parse trees

Semantic Analysis with Attribute Grammars Part 1

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

Syntax-Directed Translation

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

Syntax Directed Translation

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

Static and Dynamic Semantics

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Syntax-Directed Translation Part I

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

5. Semantic Analysis!

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

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

Syntax-Directed Translation

Syntax-Directed Translation

Summary: Semantic Analysis

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

Syntax-Directed Translation. Introduction

CSCI Compiler Design

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

Lecture 14 Sections Mon, Mar 2, 2009

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables

Syntax-Directed Translation Part II

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

Syntax Directed Translation

CS 406: Syntax Directed Translation

[Syntax Directed Translation] Bikash Balami

Syntax-Directed Translation

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

COP5621 Exam 3 - Spring 2005

Instruction Selection: Peephole Matching. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Syntactic Directed Translation

2068 (I) Attempt all questions.

CS143 Midterm Sample Solution Fall 2010

Abstract Syntax Trees Synthetic and Inherited Attributes

Chapter 4. Action Routines

COMPILER CONSTRUCTION (Evaluation Orders for SDD s)

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

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

Attribute Grammars. An Example. Using an Attribute Grammar. Recall the context-sensitive language from Chapter 1: L = { a n b n c n n!

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

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

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

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

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

CS606- compiler instruction Solved MCQS From Midterm Papers

Principles of Programming Languages

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Intermediate Code Generation

CSCI Compiler Design

Algorithms and Data Structures

Instruction Selection, II Tree-pattern matching

Building Compilers with Phoenix

Introduction to Automated Text Analysis. bit.ly/poir599

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano

Abstract Syntax Trees & Top-Down Parsing

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

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

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

UNIT IV INTERMEDIATE CODE GENERATION

CSE3322 Programming Languages and Implementation

Downloaded from Page 1. LR Parsing

Chapter 4 - Semantic Analysis. June 2, 2015

Question Bank. 10CS63:Compiler Design

CSCI Compiler Design

Generic Language Technology

Error Handling Syntax-Directed Translation Recursive Descent Parsing. Lecture 6

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

Static Checking and Intermediate Code Generation Pat Morin COMP 3002

CS415 Compilers. Intermediate Represeation & Code Generation

Chapter 3 (part 3) Describing Syntax and Semantics

Class Information ANNOUCEMENTS

Attribute Grammars. Attribute Grammars

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

4. Semantic Processing and Attributed Grammars

CHAPTER 10 GRAPHS AND TREES. Alessandro Artale UniBZ - artale/

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

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

CMSC330 Spring 2008 Final Exam

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

COP4020 Spring 2011 Midterm Exam

G53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32

2.2 Syntax Definition

Transcription:

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

Attribute Grammars Add rules to compute the decimal value of a signed binary number

Two kinds of Attributes Synthesized attribute Bottom-Up flow of values Depends on values from the node itself, children, or constants Inherited attribute Top-down flow of values Depends on values from siblings, parent and constants

Synthesized Attributes Depends on values from the node itself, children, or constants.val.val False.neg false True.neg true Bottom-Up flow (Children) Constants

Synthesized Attributes Depends on values from the node itself, children, or constants.val.val False.neg false True.neg true Bottom-Up flow (Children) Constants

Inherited Attributes Depends on values from siblings, parent and constants 0.pos 0.pos +.pos 0.pos Top-down flow

Back to the Examples For.neg

Back to the Examples For.neg.pos.val

Back to the Examples For.neg.pos.val.pos.val

Back to the Examples For Number.val Number.neg.pos.val.pos.val

Back to the Examples For Number.val Number.neg.pos.val.pos.val

Back to the Examples For Number.val Number.neg.pos.val.pos.val

Back to the Examples For Number.val Number.neg.pos 0.val.pos.val

Back to the Examples For Number.val Number.neg.pos 0.val.pos.val

Back to the Examples For Number.val Number.neg true.pos 0.val.pos.val

Back to the Examples For Number.val Number.neg true.pos 0.val.pos.val

Back to the Examples For Number.val Number.neg true.pos 0.val.pos 0.val

Back to the Examples For Number.val Number.neg true.pos 0.val.pos 0.val

Back to the Examples For Number.val Number.neg true.pos 0.val.pos 0.val 2.pos º

Back to the Examples For Number Number.val.val º.neg true.pos 0.val.val º.pos 0.val 2.pos º

Back to the Examples For Number Number.val.val º One possible evaluation order:.neg true.pos 0.val.val º.pos 2.neg.pos 0.val 2.pos º 3.pos 4.val 5.val 6 Number.val Evaluation order must be consistent with the attribute dependence graph Other orders are possible

Attributes + Parse Tree Attributes associated with nodes in parse tree Rules are value assignments associated with productions Rules & parse tree define an attribute dependence graph Dependence graph must be non-circular (no cycles) This produces a high-level, functional specification

Using Attribute Grammars Attribute grammars can specify contextsensitive actions Take values from syntax Perform computations with values Insert type tests, type inference, logic,

Evaluation Methods Dynamic, dependence-based methods Build the parse tree Build the dependence graph Topological sort the dependence graph Define attributes in topological order Rule-based methods Analyze rules at compiler-generation time Determine a fixed (static) ordering Evaluate nodes in that order (treewalk) Oblivious methods Ignore rules & parse tree Pick a convenient order (at design time) & use it (passes, dataflow)

Back to the Example Number 0 For 0

Back to the Example Number val: neg: pos: 0 val: pos: val: pos: val: pos: val: pos: val: pos: val: 0 For 0

Back to the Example Number val: 5 Inherited Attributes neg: true pos: val: 4 pos: 0 val: 5 pos: 0 val: Note: downward flow (pointing arrows) of information pos: 2 val: 4 pos: val: 0 pos: 2 val: 4 0 For 0

Back to the Example Number val: 5 Synthesized attributes neg: true pos: 2 val: 4 pos: val: 4 pos: val: 0 pos: 0 val: 5 pos: 0 val: Note: upward flow (pointing arrows) of information and the flow from node s (self) attributes pos: 2 val: 4 0 For 0

Back to the Example Number val: 5 neg: true pos: val: 4 pos: 0 val: 5 pos: 0 val: If we show the computation... pos: 2 val: 4 pos: 2 val: 4 0 pos: val: 0 then peel away the parse tree... For 0

Back to the Example neg: true pos: 2 val: 4 pos: 2 val: 4 val: 5 pos: val: 4 0 pos: val: 0 pos: 0 val: 5 pos: 0 val: For 0 All that is left is the attribute dependence graph. This succinctly represents the flow of values in the problem instance. The dependence graph must be acyclic (no cycles!)

An Extended Example Grammar for a basic block

An Extended Example Grammar for a basic block

An Extended Example Grammar for a basic block

An Extended Example Grammar for a basic block Example basic block a = -5 b = a * 7 c = b / 2 d = a + b - c How many clock cycles will this block take to execute?

An Extended Example Grammar for a basic block Simple Attribute Grammar Estimate cycle count for the block of instructions Each operation has a COST Add them, bottom up Assume a load per value Assume no reuse

An Extended Example (continued) Adding attribution rules All these attributes are synthesized!

An Extended Example (continued) Adding attribution rules All these attributes are synthesized!

An Extended Example (continued) Adding attribution rules All these attributes are synthesized!

An Extended Example (continued) Adding attribution rules All these attributes are synthesized!

An Extended Example (continued) Adding attribution rules All these attributes are synthesized!

An Extended Example 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 What about an improvement? (see backup slides) Values are loaded only once per block (not at each use) Need to track which values have been already loaded

Backup Slides

A Better Execution Model Adding load tracking Need sets Before and After for each production Must be initialized, updated, and passed around the tree This looks more complex!

A Better Execution Model Adding load tracking Need sets Before and After for each production Must be initialized, updated, and passed around the tree This looks more complex!

A Better Execution Model Load tracking adds complexity Every production needs rules to copy Before & After A sample production Lots of work, lots of space, lots of rules to write

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 complex initialization Jump from tracking loads to tracking registers is small Copy rules are already in place Some local code to perform the allocation