Download presentation

Presentation is loading. Please wait.

Published byGavin Rowe Modified over 4 years ago

1
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi – 110 025 fahim.md@gmail.com http://md-fahim.tripod.com/home.htm

2
CH4.2 Static Checking Some syntactic/semantic properties of the source language are not appropriate for incorporation into the grammar of the language. Some syntactic/semantic properties of the source language are not appropriate for incorporation into the grammar of the language. Content sensitive elements. They will be checked separately. They will be checked separately. Static Checking/ Dynamic Checking. Static Checking/ Dynamic Checking. Static checking examples: Static checking examples: Type checks. Flow-of-control checks check whether a e.g. break statement has somewhere to return control. Uniqueness checks, Name-related checks. In some languages names must be unique or they must appear twice. Etc.

3
CH4.3 Type Expressions The type of a language construct will be denoted by a type- expression. The type of a language construct will be denoted by a type- expression. Type-expressions are either basic types or they are constructed from basic types using type constructors. Type-expressions are either basic types or they are constructed from basic types using type constructors. Basic types: boolean, char, integer, real, type_error, void array(I,T) where T is a type-expression and I is an integer-range. E.g. int A[10] has the type expression array({0,..,9},integer) We can take cartesian products of type- expressions. E.g. struct entry {char letter; int value; }; is of type ( letter x char) x ( value x integer)

4
CH4.4 Type Expressions,II Pointers. int* aa aa is of type pointer(integer). Functions: int divide(int i, int j) is of type integer x integer integer Representing type expressions as trees e.g. char x char pointer(integer) char char integer pointer x

5
CH4.5 Type Systems A Type-system: collection of rules for assigning type-expressions to the variable parts of a program. A Type-system: collection of rules for assigning type-expressions to the variable parts of a program. A type-checker implements a type-system. A type-checker implements a type-system. It is most convenient to implement a type-checker within the semantic rules of a syntax-directed definition (and thus it will be implemented during translation). It is most convenient to implement a type-checker within the semantic rules of a syntax-directed definition (and thus it will be implemented during translation). Many checks can be done statically (at compilation). Many checks can be done statically (at compilation). Not all checks can be done statically. E.g. int A[10]; int i; … … ; printf(%d,A[i]); Not all checks can be done statically. E.g. int A[10]; int i; … … ; printf(%d,A[i]); Recent security concerns about type-checking (the buffer overflow vulnerability). Recent security concerns about type-checking (the buffer overflow vulnerability).

6
CH4.6 A simple type-checker. Use a synthesized attribute called type to carry the type expression of the corresponding language construct. Use a synthesized attribute called type to carry the type expression of the corresponding language construct. We will use the grammar: We will use the grammar: PRODUCTION P D ; E D D ; D | id : T T char | integer | array [ num ] of T | ^T E literal | num | id | E mod E | E [E] | E^Examples CODESome TypesExpressions key:integer;array[256] of chartable[i] key mod 1999^integerppt^

7
CH4.7 A simple type-checker, II Dealing with declarations (no type-checking yet) Necessary bookkeeping for symbol-table construction + type checking PRODUCTIONSemantic Rule P D ; E{ } D D ; D{ } D id : T{ addtype(id.entry, T.type) } T char{T.type = char } T integer {T.type = integer } T array [ num ] of T {T.type=array(1..num.val,T.type)} T ^T {T.type = pointer(T.type) }

8
CH4.8 A simple type-checker, III continuation… PRODUCTIONSemantic Rule E literal {E.type = char } E num{E.type = integer } E id {E.type = lookup(id.entry)} E E 1 mod E 2 {E.type = if (E 1.type == integer) and (E 2.type == integer) then integer else type_error } E E 1 [E 2 ] {E.type = if (E 2.type == integer) and (E 1.type == array(s,t)) then t else type_error } E E 1 ^{E.type = if (E 1.type == pointer(t)) then t else type_error } In a similar fashion we can add boolean types.

9
CH4.9 Extension to Type-Checking of Statements PRODUCTIONSemantic Rule S id := E{S.type = if (lookup(id.entry)==E.type) then void else type_error } S if E then S 1 {S.type = if (E.type == boolean) then S 1.type else type_error } S while E do S 1 {S.type = if (E.type == boolean) then S 1.type else type_error } S S 1 ; S 2 {S.type = if (S 1.type == void) and (S 2.type == void) then void else type_error } Tough!

10
CH4.10 Structural Equivalence of Types Recursive procedure: function sequiv(s,t):boolean; begin if s and t are the same basic type then return true else if s=array(s 1,s 2 ) and t=array(t 1,t 2 ) then return sequiv(s 1,t 1 ) and sequiv(s 2,t 2 ) else if s=s 1 x s 2 and t=t 1 x t 2 then return sequiv(s 1,t 1 ) and sequiv(s 2,t 2 ) else if s=pointer(s 1 ) and t=pointer(t 1 ) return sequiv(s 1,t 1 ) else if s=s 1 s 2 and t=t 1 t 2 then return sequiv(s 1,t 1 ) and sequiv(s 2,t 2 ) else return false end

11
CH4.11 Type Checking of Functions Additional type production: T T 1 T 2 {T.type = T 1.type T 2.type } E E 1 (E 2 ) {E.type = if (E 2.type==s) and (E 1.type==s t) then t else type_error }

12
CH4.12 Type Coercions Tolerate type mismatch in expressions if no information gets lost. Tolerate type mismatch in expressions if no information gets lost. PRODUCTIONSemantic Rule E num {E.type = integer } E num.num{E.type = real } E id {E.type = lookup(id.entry)} E E 1 op E 2 {E.type = if (E 1.type == integer) and (E 2.type == integer) then integer else if (E 1.type == integer) and (E 2.type == real) then real else if (E 1.type == real) and (E 2.type == integer) then real else if (E 1.type == real) and (E 2.type == real) then real else type_error }

13
CH4.13Overloading Expressions might have a multitude of types. Expressions might have a multitude of types. Example with functions: Example with functions: E id {E.types = lookup(id.entry)} E E 1 (E 2 ) {E.types = { t | there exists a s in E 2.types such that s t belongs to E 1.types } Type error will occur when E.types becomes empty for some expression.

Similar presentations

OK

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.

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.

© 2018 SlidePlayer.com Inc.

All rights reserved.

By using this website, you agree with our use of **cookies** to functioning of the site. More info in our Privacy Policy and Google Privacy & Terms.

Ads by Google

Ppt on human nutrition and digestion of annelids Ppt on amplitude shift keying transmitter Ppt on obesity prevention in children Ppt on cash flow management Ppt on revolution of the earth and seasons worksheet Ppt on hepatitis b vaccine Converter pub to ppt online shopping Ppt on dynamic resource allocation in cloud computing Ppt on hydrogen fuel cell vehicles gm Ppt on social media in india