1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi
Outline 1. Data Types & Type Checking 1. Type Expressions & Type Constructors 2. Type Names, Type Declarations, and Recursive Types 3. Type Equivalence 4. Type Inference and Type Checking 2. Summary 2
Semantic Analysis Lecture:
Practical Work Construct symbol table for following code; int a; int function(int a, int b) { string temp; temp = “Hello”; } 4
Data Types & Type Checking One of the principal tasks of a compiler is the computation and maintenance of information on data types (type inference) Compiler uses this information to ensure that each part of the program makes sense under the type rules of the language (type checking) Data type information can occur in a program in several different forms Theoretically, a data type is a set of values, or more precisely a set of values with certain operations on those values 5
Data Types & Type Checking (Continue…) For instance, data type integer in a programming language refers to a subset of mathematical integers, together with the arithmetic operations These sets in compiler constructions are described by a type expression Type expressions can occur in several places in a program 6
Type Expressions & Type Constructors A programming language always contain a number of built-in types These predefined types correspond either to numeric data types like int or double OR they are elementary types like boolean or char Such data types are called simple types, in that their values exhibit no explicit internal structure An interesting predefined type in C language is void type This type has no values, and so represents empty set 7
Type Expressions & Type Constructors (Continue…) In some languages it is possible to define new simple types subrange in Pascal and enumerated types in C In Pascal, subrange of integers from 0 to 9 can be declared as type Digit = 0..9; In C, an enumerated type consisting of named values can be declared as typedef enum {red, green, blue} Color; 8
Type Expressions & Type Constructors (Continue…) Given a set of predefined types, new data types can be created using type constructors, such as array and record, or struct Such constructors can be viewed as functions that take existing types as parameters and return new types with a structure that depends on the constructor Such types are called structured types 9
Type Names, Type Declarations, and Recursive Types Languages that have a rich set of type constructors usually also have a mechanism for a programmer to assign names to type expressions Such type declarations (sometimes called type definitions) can be done in C as follows struct RealIntRec { double r; int I; }; 10
Type Names, Type Declarations, and Recursive Types (Continue…) Type declarations cause the declared type names to be entered into the symbol table just a s variable declarations cause variable names to be entered Type names are associated with attributes in the symbol table in a similar way to variable declarations These attributes include scope and type expressions corresponding to the type name Since type names can appear in type expressions, question arise about the recursive use of type names 11
Type Names, Type Declarations, and Recursive Types (Continue…) In C programming language, recursive type names cannot be declared directly because at time of declaration it is unknown that how much memory be required for the structure; struct intBST { int val; struct intBST *left, *right; }; 12
Type Equivalence Given the possible type expressions of a language, a type checker must frequently answer the question of when two type expressions represent the same type This is the question of type equivalence There are many possible ways for type equivalence to be defined by a language Type equivalence checking can be seen as a function in a compiler function typeEqual( t1, t2, TypeExp ) : Boolean 13
Type Equivalence (Continue…) The typeEqual() function takes two type expressions and returns true if they represent the same type according to the type equivalence rules of the language One issue that relates directly to the description of type equivalence algorithm is the way type expressions are represented within a compiler One straightforward method is to use a syntax tree representation 14
Type Inference & Type Checking Type checking is described in terms of semantic actions based on representation of types and a typeEqual() operation. Compiler needs symbol table as well for this purpose along with three of its basic operations insert, lookup, and delete 15
Type Inference & Type Checking (Continue…) Consider the following grammar; 16
Type Inference & Type Checking (Continue…) 17
18 Summary Any Questions?