Download presentation
Presentation is loading. Please wait.
Published byBrook Chrystal Daniel Modified over 9 years ago
1
CH4.1 CSE244 Type Checking Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs, CT 06269-1155 aggelos@cse.uconn.edu http://www.cse.uconn.edu/~akiayias
2
CH4.2 CSE244 Static Checking Some syntactic/semantic properties of the source language are not appropriate for incorporation into the grammar of the language. They will be checked separately. Static Checking/ Dynamic Checking. Static checking examples: Type checks. Flow-of-control checks. Uniqueness checks. Name-related checks.
3
CH4.3 CSE244 Type Expressions 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. 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 CSE244 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 CSE244 Type Systems A Type-system: collection of rules for assigning type-expressions to the variable parts of a program. 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). 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]); Recent security concerns about type-checking (the buffer overflow vulnerability).
6
CH4.6 CSE244 A simple type-checker. Use a synthesized attribute called type to carry the type expression of the corresponding language construct. 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 CSE244 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 CSE244 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 CSE244 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 CSE244 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 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 CSE244 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 CSE244 Type Coercions 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.13 CSE244Overloading Expressions might have a multitude of types. 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
© 2024 SlidePlayer.com Inc.
All rights reserved.