Context-sensitive Analysis Part II

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

Context-sensitive Analysis

Syntactic Directed Translation

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

Syntactic Directed Translation

Context-sensitive Analysis

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

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

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

CSCI Compiler Design

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

CMPT 379 Compilers. Parse trees

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

Semantic Analysis with Attribute Grammars Part 1

CS606- compiler instruction Solved MCQS From Midterm Papers

2068 (I) Attempt all questions.

Lecture 14 Sections Mon, Mar 2, 2009

Summary: Semantic Analysis

Syntax-Directed Translation

CSCE 531, Spring 2015 Final Exam Answer Key

CSCI Compiler Design

Abstract Syntax Trees Synthetic and Inherited Attributes

Syntax-Directed Translation Part II

Syntax Directed Translation

Downloaded from Page 1. LR Parsing

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

COP5621 Exam 3 - Spring 2005

Syntax-Directed Translation

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

CSCI Compiler Design

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

Syntax-Directed Translation. Introduction

CSE302: Compiler Design

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

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

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

CS143 Midterm Sample Solution Fall 2010

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

LR Parsing LALR Parser Generators

Syntax-Directed Translation

Question Bank. 10CS63:Compiler Design

Semantic Analysis with Attribute Grammars Part 3

Syntax-Directed Translation

CSE 582 Autumn 2002 Exam 11/26/02

Building Compilers with Phoenix

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

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

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

CS 406: Syntax Directed Translation

Syntax-Directed Translation Part I

Semantic Analysis. Lecture 9. February 7, 2018

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

CS5363 Final Review. cs5363 1

CSCI Compiler Design

Principles of Programming Languages

LR Parsing LALR Parser Generators

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

CS143 Midterm Spring 2014

Compiler Construction

Compiler Construction

Attribute Grammars. Attribute Grammars

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 20th May 2016 Time: 14:00-16:00

Static and Dynamic Semantics

CSE 582 Autumn 2002 Exam Sample Solution

Part 5 Program Analysis Principles and Techniques

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

4. Semantic Processing and Attributed Grammars

5. Semantic Analysis!

UNIT IV INTERMEDIATE CODE GENERATION

Syntax Directed Translation

Bottom-up Parser. Jungsik Choi

[Syntax Directed Translation] Bikash Balami

ECE468 Fall 2003, Final Exam

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

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

Introduction to Compiler

ECE 468/573 Midterm 1 September 30, 2015

MODULE 14 SLR PARSER LR(0) ITEMS

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

Chapter 4. Action Routines

UNIVERSITY OF CALIFORNIA

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

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

Class Information ANNOUCEMENTS

CSE302: Compiler Design

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

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

Compilers. Compiler Construction Tutorial The Front-end

In One Slide. Outline. LR Parsing. Table Construction

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

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

COP4020 Spring 2011 Midterm Exam

CMSC330 Spring 2008 Final Exam

Transcription:

Context-sensitive Analysis Part II

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

Back to the Examples For Symbol Attributes.neg Number val neg pos, val pos, val

Back to the Examples For Symbol Attributes.neg Number val neg pos, val pos, val.pos.val

Back to the Examples For Symbol Attributes Number val neg.neg.pos.val pos, val pos, val.pos.val

Back to the Examples For Number.val Symbol Attributes.neg Number.pos.val Number val neg pos, val 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 Graph must be non-circular This produces a high-level, functional specification

Two kinds of Attributes Synthesized attribute Upward flow of values Depends on values from the node itself, children, or constants Inherited attribute Downward flow of values Depends on values from siblings, parent and constants

Using Attribute Grammars Attribute grammars can specify contextsensitive actions Take values from syntax Perform computations with values Insert tests, 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 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

An Extended Example 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

An Extended Example 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

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

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!

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!

A Better Execution Model Load tracking adds complexity Every production needs rules to copy Before & After A sample production Expr 0 Expr + Term Expr 0.cost Expr.cost + COST(add) + Term.cost ; Expr.Before Expr 0.Before ; Term.Before Expr.After; Expr 0.After Term.After 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 Next class Curing these problems with ad-hoc syntax-directed translation

Midterm Study Guide Focus on Phase II and III Focus on Chapters 2 and 3 Lexer Given an RE can you construct an NFA/DFA? Given a DFA/NFA can you construct an RE? Parsing Focus on LR() Parsing Construct Canonical Collections, Control DFA, ACTION, and GOTO tables