Context-sensitive Analysis

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

Context-sensitive Analysis

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

Context-sensitive Analysis Part II

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

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

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

Syntactic Directed Translation

Syntax Directed Translation

Intermediate Representations

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

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

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

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

5. Semantic Analysis!

Semantic Analysis with Attribute Grammars Part 1

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

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

CMPT 379 Compilers. Parse trees

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

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables

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

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

CSCI Compiler Design

Syntax-Directed Translation

Syntax-Directed Translation

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

Syntax Directed Translation

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

Part 5 Program Analysis Principles and Techniques

Semantic Analysis Attribute Grammars

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

Lecture 14 Sections Mon, Mar 2, 2009

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Syntax-Directed Translation Part I

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

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

Semantic Analysis. Lecture 9. February 7, 2018

Semantic Analysis with Attribute Grammars Part 3

Chapter 4 - Semantic Analysis. June 2, 2015

Summary: Semantic Analysis

CS 406/534 Compiler Construction Parsing Part I

Abstract Syntax Trees Synthetic and Inherited Attributes

Parsing II Top-down parsing. Comp 412

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

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

CSCI Compiler Design

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

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

CS 406: Syntax Directed Translation

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

Abstract Syntax Trees & Top-Down Parsing

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Syntax-Directed Translation Part II

CS5363 Final Review. cs5363 1

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

Syntax-Directed Translation. Introduction

Chapter 4. Action Routines

CMSC 330: Organization of Programming Languages

LECTURE 3. Compiler Phases

1 Lexical Considerations

Context-sensitive Analysis Part IV Ad-hoc syntax-directed translation, Symbol Tables, andtypes

Syntax Analysis, III Comp 412

Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

4. Semantic Processing and Attributed Grammars

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

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

Attribute Grammars. Attribute Grammars

Principles of Programming Languages

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:

More On Syntax Directed Translation

Compiling Techniques

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

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

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

Parsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)

Some notes about Event-B and Rodin

Syntax-Directed Translation

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

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

Syntax Analysis, III Comp 412

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

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

Properties of Regular Expressions and Finite Automata

Code No: R Set No. 1

CST-402(T): Language Processors

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

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

Computing Inside The Parser Syntax-Directed Translation. Comp 412

CMSC 330: Organization of Programming Languages

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

Syntax Directed Translation

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Chapter 3 (part 3) Describing Syntax and Semantics

COS 320. Compiling Techniques

Transcription:

Context-sensitive Analysis

Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,b,c,d) int a, b, c, d; { } What is wrong with this program? (let me count the ways ) fee() { int f[3],g[0], h, i, j, k; char *p; fie(h,i, ab,j, k); k = f * i + j; h = g[7]; printf( <%s,%s>.\n, p,q); p = 0; }

Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,b,c,d) int a, b, c, d; { } fee() { int f[3],g[0], h, i, j, k; char *p; fie(h,i, ab,j, k); k = f * i + j; h = g[7]; printf( <%s,%s>.\n, p,q); p = 0; } What is wrong with this program? (let me count the ways ) declared g[0], used g[7] wrong number of args to fie() ab is not an int wrong dimension on use of f undeclared variable q 0 is not a character string All of these are deeper than syntax To generate code, we need to understand its meaning!

Beyond Syntax To generate code, the compiler needs to answer many questions Is x a scalar, an array, or a function? Is x declared? Are there names that are not declared? Declared but not used? Which declaration of x does each use reference? Is the expression x * y + z type-consistent? In a[i,j,k], does a have three dimensions? Where can z be stored? (register, local, global, heap, static) How many arguments does fie() take? What about printf ()? Does *p reference the result of a malloc()? Do p & q refer to the same memory location? Is x defined before it is used? These are beyond a CFG

Beyond Syntax These questions are part of context-sensitive analysis Questions & answers involve non-local information Answers may involve computation How can we answer these questions? Use formal methods Attribute grammars? Also known as attributed CFG or syntax-directed definitions Use ad-hoc techniques Symbol tables Ad-hoc code (action routines) In scanning & parsing, formalism won; different story here.

Beyond Syntax Telling the story The attribute grammar formalism is important Succinctly makes many points clear Sets the stage for actual, ad-hoc practice The problems with attribute grammars motivate practice Non-local computation Need for centralized information Some folks still argue for attribute grammars In practice, ad-hoc techniques used We will cover attribute grammars, then move on to ad-hoc ideas

Attribute Grammars What is an attribute grammar? A context-free grammar augmented with a set of rules Each symbol in the derivation has a set of values, or attributes X.a denotes the value of a at a particular parse-tree node labeled X The rules specify how to compute a value for each attribute Example grammar This grammar describes signed binary numbers: +0, -, +00, but not 0 We would like to augment it with rules that compute the decimal value of each valid input string Example: parse -0 and compute -5

Examples For Number Sign Number Sign For 0 Number Sign Sign Sign Sign Sign Sign 0 Sign 0 0 Number Sign 0 We will use these two throughout the lecture

Attribute Grammars Add rules to compute the decimal value of a signed binary number Symbol Number Sign Attributes val neg pos, val pos, val

Back to the Examples Rules + parse tree imply an attribute dependence graph For Number.val.val One possible evaluation order: neg true Sign Number.pos 0.val.val.pos 0.val 2.pos.pos 2 Sign.neg 3.pos 4.val 5.val 6 Number.val Other orders are possible Knuth suggested a data-flow model for evaluation Independent attributes first Others in order as input values become available Evaluation order must be consistent with the attribute dependence graph

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

The Rules of the Game Attributes associated with nodes in parse tree Rules are value assignments associated with productions Attribute is defined once, using local information Label identical terms in production for uniqueness Rules & parse tree define an attribute dependence graph Graph must be non-circular This produces a high-level, functional specification Synthesized attribute Depends on values from children Inherited attribute Depends on values from siblings & parent

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 children & from constants S-attributed grammars Evaluate in a single bottom-up pass Good match to LR parsing Inherited Attributes Use values from parent, constants, & siblings directly express context can rewrite to avoid them Thought to be more natural Not easily done at parse time We want to use both kinds of attribute

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 (passes, dataflow) Ignore rules & parse tree Pick a convenient order (at design time) & use it

Back to the Example Number Sign 0 For 0

Back to the Example Number val: Sign 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 Sign neg: true pos: 0 val: 5 pos: pos: 0 val: pos: 2 pos: val: 0 pos: 2 0 For 0

Back to the Example Number val: 5 Synthesized attributes Sign neg: true pos: 0 val: 5 pos: pos: 0 val: pos: 2 pos: val: 0 pos: 2 0 For 0

Back to the Example Number val: 5 Synthesized attributes Sign neg: true pos: 0 val: 5 pos: pos: 0 val: pos: 2 pos: val: 0 pos: 2 0 For 0

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

Back to the Example val: 5 All that is left is the attribute dependence graph. neg: true pos: 0 val: 5 This succinctly represents the flow of values in the problem instance. pos: 2 pos: 2 pos: 0 pos: val: 0 pos: 0 val: 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

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 SNC grammars were first defined by Kennedy & Warren

An Extended Example Grammar for a basic block ( 4.3.3) 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 Let s estimate cycle counts Each operation has a COST Add them, bottom up Assume a load per value Assume no reuse Simple problem for an AG

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 Load tracking adds complexity But, most of it is in the copy rules 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 These copy rules multiply rapidly Each creates an instance of the set 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