31-10-2012, 04:50 PM
Type Checking
Type Checking.ppt (Size: 86.5 KB / Downloads: 20)
A compiler has to do semantic checks in addition to syntactic checks.
Semantic Checks
Static – done during compilation
Dynamic – done during run-time
Type checking is one of these static checking operations.
we may not do all type checking at compile-time.
Some systems also use dynamic type checking too.
A type system is a collection of rules for assigning type expressions to the parts of a program.
A type checker implements a type system.
A sound type system eliminates run-time type checking for type errors.
A programming language is strongly-typed, if every program its compiler accepts will execute without type errors.
In practice, some of type checking operations are done at run-time (so, most of the programming languages are not strongly-typed).
Type Expression
The type of a language construct is denoted by a type expression.
A type expression can be:
A basic type
a primitive data type such as integer, real, char, boolean, …
type-error to signal a type error
void : no type
A type name
a name can be used to denote a type expression.
A type constructor applies to other type expressions.
arrays: If T is a type expression, then array(I,T) is a type expression where I denotes index range. Ex: array(0..99,int)
products: If T1 and T2 are type expressions, then their cartesian product T1 x T2 is a type expression. Ex: int x int
pointers: If T is a type expression, then pointer(T) is a type expression. Ex: pointer(int)
functions: We may treat functions in a programming language as mapping from a domain type D to a range type R. So, the type of a function can be denoted by the type expression D→R where D are R type expressions. Ex: int→int represents the type of a function which takes an int value as parameter, and its return type is also int.