Context-sensitive analysis context-sensitive questions might the compiler What ask? 1. Is x a scalar, anarray, 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? Does the dimension of a reference match the 6. declaration? 7. Where can xbe stored? èheap, stack, :::è 8. Does *p reference the result of a mallocèè? 9. Is x dened before it is used? 10. Is an array reference in bounds? 11. Does function foo produce a constant value? hese cannot be answered with a context-free grammar CMC 430 Lecture 9, Page 1 Context-sensitive analysis 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 CMC 430 Lecture 9, Page 2
Attribute grammars Attribute grammar generalization of context-free grammar each grammar symbol has an associated set of attributes augment grammar with rules that dene values high-level specication, independent of scheme evaluation Dependences between attributes values are computed from constants & other attributes synthesized attribute í value computed from children inherited attribute í value computed from & parent siblings CMC 430 Lecture 9, Page 3 xample attribute grammar A grammar to evaluate signed binary numbers due to cott K. Warren, Rice Ph.D. Production valuation Rules 1 NUM ::= IGN LI LI.pos è 0 è if IGN.neg NUM.val -LI.val then else LI.val IGN ::= + IGN.neg è false 2 IGN ::= - IGN.neg è true 3 LI ::= BI BI.pos è LI.pos 4 è BI.val LI.val LI 0 ::= LI 1 BI LI 1.pos è LI 0.pos + 1 5 è LI 0.pos BI.pos LI 0.val è LI 1.val + BI.val BI ::= 0 BI.val è 0 6 BI ::= 1 BI.val è 2 BI.pos 7 CMC 430 Lecture 9, Page 4
Attribute grammars xample IGN neg: - NUM val: -5 LI BI pos: 2 val: 4 pos: 2 val: 4 1 LI pos: 1 val: 4 LI BI pos: 0 val: 5 pos: 1 val: 0 0 BI val and neg are synthesized attributes pos is an inherited attribute pos: 0 val: 1 CMC 430 Lecture 9, Page 5 1 yntax-directed translation Disadvantages of attribute grammars handling non-local information storage management locating answers avoiding circular evaluation yntax-directed translation allow arbitrary actions provide central repository can place actions amid production xamples YACC A ::= BCf$$ = concatè$1,$2è; g CUP A:n ::= B:m C:p f: n = concatèm,pè; :g ypical uses build symbol table perform errorètype checking CMC 430 Lecture 9, Page 6
If operands of addition are of type integer, then is of type integer result he result of the unary & operator is a pointer the object referred to by the operand to CMC 430 Lecture 9, Page 7 CMC 430 Lecture 9, Page 8 ype checking ype systems ypes token stream parser syntax tree type checker syntax tree intermediate code generator intermediate representation values that share a set of common properties dened by language andèor programmer ype checker ype system enforces rules of type system may be strongèweak, staticèdynamic 1. set of types in a programming language, and 2. rules that use types to specify program behavior tatic type checking xample type rules performed at compile time early detection, no run-time overhead not always possible èe.g., Aëiëè Dynamic type checking Advantages of typed languages performed at run time ensure run-time safety more exible, rapid prototyping expressiveness èoverloading, polymorphismè overhead to check run-time type tags provide information for code generation
ype expressions ype expressions used to represent the type of a language construct describes both language and programmer types xamples basic types: integer, real, character,... constructed types: arrays, records, pointers, functions,... Constructing new types arrays arrayè1:::10, è records 1 2 ::: pointers pointerèè functions 1 2 :::! n CMC 430 Lecture 9, Page 9 A simple type checker a synthesized attribute grammar, we will Using a type checker for arrays, pointers, describe statements, and functions. Grammar for source language: ::= D; P ::= D;jid: D ::= char j integer j array ënumë of j " ::= literal j num j id j mod j ëë j " Basic types char, integer, typerror assume all arrays start at1,e.g., ë256ë of char array in the type expression results ::256,charè arrayè1: builds a pointer type, so " integer " in the type expression pointerèintegerè results CMC 430 Lecture 9, Page 10
ype checking example Partial attribute grammar for the type system ::= id: f addtypeèid.entry,.typeè g D ::= char f.type è char g ::= integer f.type è integer g ::= " 1 f.type è pointerè 1.typeè g ::= array ënumë of 1 f.type è 1.typeè g arrayè1:::num.val, CMC 430 Lecture 9, Page 11 ype checking expressions expression is assigned a type using rules ach with the grammar. associated ::= literal f.type è char g ::= num f.type è integer g ::= id f.type è lookupèid.entryè g ::= 1 mod 2 f.type è if 1.type = integer and = integer then integer 2.type ::= 1 ë 2 ë f.type è if 2.type = integer and = arrayès,tè then t 1.type ::= 1 " f.type è if 1.type =pointer t then CMC 430 Lecture 9, Page 12
ype checking statements do not typically have values, therefore tatements assign them the type void. If an error is detected we within the statement, it gets type typerror. ::= id è f.type è if id.type =.type void then ::= if then 1 f.type è if.type =boolean 1.type then ::= while do 1 f.type è if.type =boolean 1.type then ::= 1 ; 2 f.type è if 1.type =void void then CMC 430 Lecture 9, Page 13 ype checking functions add two new productions to the grammar to We function declarations and applications represent ::=! declaration ::= èè application o capture the argument and return type, we use ::= 1! 2 f.type è è 1.type! 2.typeè g ::= 1 è 2 è f.type è if 1.type =s!t 2.type =s then t and CMC 430 Lecture 9, Page 14