Context-sensitive Analysis

Similar documents
Context-sensitive Analysis

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

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 Part II

Syntactic Directed Translation

Syntax Directed Translation

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

Intermediate Representations

Syntactic Directed Translation

Semantic Analysis with Attribute Grammars Part 1

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

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

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

5. Semantic Analysis!

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

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

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

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Summary: Semantic Analysis

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables

Syntax-Directed Translation. Introduction

CMPT 379 Compilers. Parse trees

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

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

Syntax-Directed Translation

CSCI Compiler Design

Syntactic Directed Translation

Chapter 4 - Semantic Analysis. June 2, 2015

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

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

More On Syntax Directed Translation

1 Lexical Considerations

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

Semantic Analysis Attribute Grammars

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

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

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

Attribute Grammars. Attribute Grammars

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

Lecture 14 Sections Mon, Mar 2, 2009

Syntax-Directed Translation

Syntax-Directed Translation

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

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

Syntax Directed Translation

Syntax Directed Translation

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

Computer Organization & Systems Exam I Example Questions

Semantic Analysis with Attribute Grammars Part 3

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

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

Syntax-Directed Translation Part I

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

Intermediate Code Generation

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

CS 406/534 Compiler Construction Parsing Part I

Part 5 Program Analysis Principles and Techniques

Chapter 3 (part 3) Describing Syntax and Semantics

Semantic Analysis. Role of Semantic Analysis

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

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

Parsing II Top-down parsing. Comp 412

CS Programming In C

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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

Semantic Analysis. Compiler Architecture

Syntax Analysis, III Comp 412

A Simple Syntax-Directed Translator

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

CS 406: Syntax Directed Translation

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

Intermediate Representations

Abstract Syntax Trees Synthetic and Inherited Attributes

CPS 506 Comparative Programming Languages. Syntax Specification

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Lexical Considerations

CMSC 330: Organization of Programming Languages

Defining Languages GMU

Programming Languages

Syntax Analysis, III Comp 412

Some notes about Event-B and Rodin

Syntax-Directed Translation

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Static and Dynamic Semantics

Principles of Programming Languages

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Programming Languages

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Programming Languages

CMSC 330: Organization of Programming Languages

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

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

Semantic Analysis. Lecture 9. February 7, 2018

Program Assignment 2 Due date: 10/20 12:30pm

Chapter 4 :: Semantic Analysis

CSC 467 Lecture 13-14: Semantic Analysis

CS A331 Programming Language Concepts

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; { } fee() { int f[3],g[0], h, i, j, k; char *p; fie(h,i, ab,j, k); k = f * i + j; h = g[17]; printf( <%s,%s>.\n, p,q); p = 10; } What is wrong with this program?

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? declared g[0], used g[17] fee() { int f[3],g[0], h, i, j, k; char *p; fie(h,i, ab,j, k); k = f * i + j; h = g[17]; printf( <%s,%s>.\n, p,q); p = 10; }

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[17]; printf( <%s,%s>.\n, p,q); p = 10; } What is wrong with this program? declared g[0], used g[17] wrong number of args to fie()

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? declared g[0], used g[17] wrong number of args to fie() fee() { int f[3],g[0], h, i, j, k; char *p; fie(h,i, ab,j, k); k = f * i + j; h = g[17]; printf( <%s,%s>.\n, p,q); p = 10; } ab is not an int

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? declared g[0], used g[17] wrong number of args to fie() fee() { int f[3],g[0], h, i, j, k; char *p; fie(h,i, ab,j, k); k = f * i + j; h = g[17]; printf( <%s,%s>.\n, p,q); p = 10; } ab is not an int wrong dimension on use of f

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? declared g[0], used g[17] wrong number of args to fie() fee() { int f[3],g[0], h, i, j, k; char *p; fie(h,i, ab,j, k); k = f * i + j; h = g[17]; printf( <%s,%s>.\n, p,q); p = 10; } ab is not an int wrong dimension on use of f undeclared variable q

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? declared g[0], used g[17] wrong number of args to fie() fee() { int f[3],g[0], h, i, j, k; char *p; fie(h,i, ab,j, k); k = f * i + j; h = g[17]; printf( <%s,%s>.\n, p,q); p = 10; } ab is not an int wrong dimension on use of f undeclared variable q 10 is not a character string

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? declared g[0], used g[17] wrong number of args to fie() fee() { int f[3],g[0], h, i, j, k; char *p; fie(h,i, ab,j, k); k = f * i + j; h = g[17]; printf( <%s,%s>.\n, p,q); p = 10; } ab is not an int wrong dimension on use of f undeclared variable q 10 is not a character string All of these are deeper than syntax

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? These are beyond a context-free grammar

Beyond Syntax To generate code, the compiler needs to answer many questions In a[i,j,k], does a have three dimensions? Where can z be stored? (register, local, heap, etc.) How many arguments does fie() take? Does *p reference the result of a malloc()? Do p & q refer to same memory location? Is x defined before it is used? These are beyond a context-free grammar

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 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 adhoc ideas

What is an Attribute Grammar? Context-free grammar augmented with 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 Rules specify how to compute a value for each attribute

What is an Attribute Grammar? Example grammar This grammar describes signed binary numbers: +101, -11, +10101, but not 101 We would like to augment it with rules that compute the decimal value of each valid input string Example: parse -101 and compute -5

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

Grammar and its Attributes Symbol Number Sign Attributes val neg pos, val pos, val

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

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

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

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

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

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

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

Back to the Examples For 1 Number Number.val.val 1 One possible evaluation order: Sign.neg true Sign.pos 0.val.val 1 1.pos 2 Sign.neg.pos 0.val 2.pos 1 3.pos 4.val 1 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 children Inherited attribute Downward flow of values Depends on values from siblings & parent

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 Sign 1 0 1 For 101

Back to the Example Number val: Sign neg: pos: 0 val: pos: val: pos: val: pos: val: pos: val: 1 pos: val: 0 1 For 101

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

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

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

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

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

An Extended Example Grammar for a basic block ( 4.3.3) Block 0 Block 1 Assign Assign Assign Ident = Expr ; Expr 0 Expr 1 + Term Expr 1 Term Term Term 0 Term 1 * Factor Term 1 / 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