Context-Sensitive Analysis

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

Symbol Table.
Semantics Static semantics Dynamic semantics attribute grammars
Intermediate Code Generation
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
1 Compiler Construction Intermediate Code Generation.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
Chapter 6 Type Checking Section 0 Overview 1.Static Checking Check that the source program follows both the syntactic and semantic conventions of the source.
Compiler Construction
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
1 CMPSC 160 Translation of Programming Languages Fall 2002 Lecture-Modules slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Department.
Chapter 6 Type Checking Section 0 Overview
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
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],
Context-sensitive Analysis, II Ad-hoc syntax-directed translation, Symbol Tables, andTypes.
Compiler Construction Sohail Aslam Lecture Beyond Syntax  These questions are part of context-sensitive analysis  Answers depend on values, not.
Context-sensitive Analysis or Semantic Elaboration Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Parsing VII The Last Parsing Lecture. Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,b,c,d) int a, b, c, d; { … } fee()
COMPILERS Semantic Analysis hussein suleman uct csc3005h 2006.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Lecture 8 Semantic Analysis.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 9 Ahmed Ezzat Semantic Analysis.
COMPILERS Semantic Analysis hussein suleman uct csc3003s 2009.
Lecture 9 Symbol Table and Attributed Grammars
Semantic analysis Jakub Yaghob
Chapter 1 Introduction.
Semantic Analysis Type Checking
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
A Simple Syntax-Directed Translator
Constructing Precedence Table
Review: Chapter 5: Syntax directed translation
Chapter 1 Introduction.
Compiler Lecture 1 CS510.
Syntax-Directed Translation Part I
CS 3304 Comparative Languages
Lecture 11: Context Sensitive Analysis
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Context-sensitive Analysis
Chapter 6 Intermediate-Code Generation
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 4 Action Routines.
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Names and Binding In Text: Chapter 5.
Compiler Construction
COMPILERS Semantic Analysis
Syntax-Directed Translation Part I
COP4020 Programming Languages
COP4020 Programming Languages
Compiler Construction
Presentation transcript:

Context-Sensitive Analysis Chapter 4 Context-Sensitive Analysis

Context-Sensitive Problems in compilation 1. Is x a scalar, an array, or a function? 2. Is x declared before it is used? 3. Are any names declared but not used? 4. Which declaration of x does this reference? 5. Is an expression type-consistent? 6. Does the dimension of a reference match the declaration? 7. Where can x be stored? (heap, stack, . . . ) 8. Does *p reference the result of a malloc()? 9. Is x defined before it is used? 10. Is an array reference in bounds? 11. Does function foo produce a constant value?

Why is context-sensitive analysis hard? need non-local information answers depend on values, not on syntax answers may involve computation

How can we answer these questions? 1. use context-sensitive grammars general problem is P-space complete 2. use attribute grammars augment context-free grammar with rules calculate attributes for grammar symbols 3. use ad hoc techniques augment grammar with arbitrary code execute code at corresponding reduction store information in attributes, symbol tables

Attribute grammar generalization of context-free grammar each grammar symbol has an associated set of attributes augment grammar with rules that define values high-level specification, independent of evaluation scheme

Attribute Grammars A generalization of CFG An attribute grammar is a context free grammar with associated attributes and semantic rules Each grammar symbol is associated with a set of attributes Each production is associated with a set of semantic rules for computing attributes

Dependences between attributes Attribute values are computed from constants & other attributes synthesized attribute value computed from children inherited attribute value computed from siblings & parent

Example attribute grammar A grammar to evaluate signed binary numbers Production Evaluation Rules 1 NUM  SIGN LIST LIST.pos = 0 NUM.val = SIGN.neg ? -LIST.val : LIST.val 2 SIGN  + SIGN.neg = false 3 SIGN  - SIGN.neg = true 4 LIST  BIT BIT.pos = LIST.pos LIST.val = BIT.val 5 LIST  LIST BIT LIST1.pos = LIST0.pos + 1 BIT.pos = LIST0.pos LIST0.val = LIST1.val + BIT.val 6 BIT  0 BIT.val à 0 7 BIT  1 BIT.val à 2BIT.pos

val and neg are synthesized attributes pos is an inherited attribute Annotates Parse Tree

Syntax-directed translation Disadvantages of attribute grammars handling non-local information, locating answers storage management, avoiding circular evaluation Syntax-directed translation allow arbitrary actions provide central repository can place actions amid production

Examples YACC A ::= B C { $$ = concat($1,$2); } CUP A ::= B:m C:p { RESULT = concat(m,p); } Typical uses build abstract syntax tree & symbol table perform error/type checking

Abstract syntax tree An abstract syntax tree is a condensed form of parse tree useful for representing constructs. usually a parse tree with the nodes for most non-terminal symbols removed. This represents “x - 2 * y”. can use a linearized (operator) form of the tree. x 2 y * - in postfix form. A popular intermediate representation.

Symbol tables A symbol table associates values or attributes (e.g., types and values) with names. What should be in a symbol table? variable and procedure names literal constants and strings

Symbol Tables What information might compiler need? textual name data type declaring procedure lexical level of declaration if array, number and size of dimensions if procedure, number and type of parameters

Symbol Tables Implementation usually implemented as hash tables How to handle nested lexical scoping? when we ask about a name, we want the closest lexical declaration One solution use one symbol table per scope tables chained to enclosing scopes insert names in table for current scope name lookup starts in current table if needed, checks enclosing scopes in order

Type systems Types values that share a set of common properties defined by language and/or programmer Type system 1. set of types in a programming language, and 2. rules that use types to specify program behavior

Type Systems Example type rules If operands of addition are of type integer, then result is of type integer The result of the unary & operator is a pointer to the object referred to by the operand Advantages of typed languages ensure run-time safety expressiveness (overloading, polymorphism) provide information for code generation

Type checking Type checker enforces rules of type system may be strong/weak, static/dynamic Static type checking performed at compile time early detection, no run-time overhead not always possible (e.g., A[i]) Dynamic type checking performed at run time more flexible, rapid prototyping overhead to check run-time type tags

Type expressions Type expressions sed to represent the type of a language construct describes both language and programmer types Examples basic types: integer, real, character, ... constructed types: arrays, records, pointers, functions,...

Constructing new types arrays array(1: : :10, T) records T1 £ T2 £ : : : pointers pointer(T) functions T1 £ T2 £ : : : ! Tn

A simple type checker Using a synthesized attribute grammar, we will describe a type checker for arrays, pointers, statements, and functions. Grammar for source language: P ::= D ; E D ::= D ; E | id: T T ::= char | integer | array [num] of T | " T E ::= literal | num | id | E mod E | E[E] | E "

Basic types char, integer, typeError assume all arrays start at 1, e.g., array [256] of char results in the type expression array(1: : :256,char) ² " builds a pointer type, so " integer results in the type expression pointer(integer)

Type checking example Partial attribute grammar for the type system D ::= id: T f addtype(id.entry, T.type) g T ::= char f T.type à char g T ::= integer f T.type à integer g T ::= " T1 f T.type à pointer(T1.type) g T ::= array [num] of T1 f T.type à array(1: : :num.val, T1.type) g

Type checking expressions