cs160-lec12 - CMPSC 160 Translation of Programming...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPSC 160 Translation of Programming Languages Lecture 12: Type Checking
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Type Checking Type checking is a part of context-sensitive analysis A type-checker verifies that the type of a construct matches the type that is expected by its context Examples dereference operation is only applied to pointers indexing is only done for an array in a method call, number of arguments and their types match the declaration arguments of modulo operation are both integers Type checking can be static: done at compile time dynamic: done at execution time A language in which every expression can be assigned an unambiguous type is called a strongly-typed language
Background image of page 2
Type Systems Base types Programming languages typically include base types for: numbers (int, float), characters, Booleans Compound and constructed types Programmers need higher-level abstractions than the base types, such as lists, graphs, trees, tables, etc. Programming languages provide mechanisms to combine and aggregate objects and to derive types for the resulting objects arrays, structures, enumerated sets, pointers A type system consists of a set of base types and a set of type- constructors array, function, pointer, product Using base types and type-constructors each expression in a program can be represented with a type expression
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example Type Expressions Base types: integer, char, float Type constructors: array , product ( × ), record, pointer, function ( ) int A[10] type expression for A is: array (10, integer ) Example type expressions in C type expression for foo is: function ( product ( char, pointer ( integer )), integer ) or using our shorthand notation: char × pointer ( integer ) integer int foo(char a, int *b) struct fie { int a, b; } type-expression for fie is: record ((a × integer ) × (b × integer )) Note that we used product to show the field names a and b in the type expression
Background image of page 4
Type Systems A type system is a collection of rules for assigning type-expressions to language constructs A type-expression is either a base type or is formed by applying a type constructor to a type-expression Inference rules for type-expressions: (Pascal) If both operands of the arithmetic operators addition,
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 18

cs160-lec12 - CMPSC 160 Translation of Programming...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online