Compiler CH 5

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

BORANA UNIVERSITY

COLLEGE OF NATURAL AND COMPUTATIONAL SCIENCE

DEPARTMENT OF COMPUTER SCIENCE

COURSE TITLE: COMPILER DESIGN

CHAPTER FIVE (5) TYPE CHECKING

BY DAWIT H.
By: D. Nanne H. 2023/24
Type Checking

• Type checking is the process of verifying and enforcing constraints of types in values.
• A compiler must check if the source program follows the syntactic and semantic
conventions of the source language.
• Type checking allows the programmer to limit what types may be used in certain
circumstances and assigns types to values.
• The type-checker determines whether these values are used appropriately or not.
• It checks the type of objects and reports a type error in the case of a violation, and
incorrect types are corrected.

By: D. Nanne H.
Cont…

• Whatever the compiler we use, while the compiler is compiling the


program, it has to check and follow the type rules of the language.
• Every language has its own set of type rules for the language.
• But, We know that the information about data types like INTEGER,
CHARACTER, FLOAT, and etc., are maintained and computed by the
compiler.

By: D. Nanne H.
Cont…
• The compiler contains modules, where the type checker is a module of a compiler and its task is
type checking.
Tasks:
• has to allow “Indexing is only on an array”
• has to check the range of data types used
• INTEGER (int) has a range of -32,768 to +32767
• FLOAT has a range of 1.2E-38 to 3.4E+38.

By: D. Nanne H.
Types of Type Checking
• There are two kinds of Semantic Checks or type checking:
• i. Static Type Checking.
• ii. Dynamic Type Checking.
• i. Static Type Checking:
• Here, checking is done by a compiler at compile time.
• It checks the type variables, i.e., type of the variable is known at the compile time.
• It examines the program text during the translation of the program.
• also used to determine the amount of memory needed to store the variable
By: D. Nanne H.
Cont...
Static checks include:
• Type-checks: A compiler should report an error if an operator is applied to an incompatible operand.
• Example: an error occurs if array variable is added with ‘function variable int a, c[10], d; d=c+d;//error
• flow of control checks: Statements that cause the flow of control to leave a construct must have
someplace to transfer the flow of control.
• Example: an error occurs when an enclosing statement such as ‘break’ does not exist in switch statements
• Uniqueness checks: There are situations in which an object must be defined only once.
• Example: int i, j, i;//error add( int a, int a){}//error

By: D. Nanne H.
Cont…

The Benefits of Static Type Checking:


• Runtime Error Protection.
• It catches syntactic errors like spurious words or extra punctuation.
• Detects incorrect argument types.
• It catches the wrong number of arguments.
• It catches wrong return types, like return “70”, from a function that’s
declared to return an int.
By: D. Nanne H.
Cont…

• ii. Dynamic Type Checking:


• here type checking is done at run time, when the target program runs
• Here, types are associated with values, not with variables.
• Dynamic type checking is more flexible.
• A static type system always restricts what can be conveniently expressed.
• Programming with a static type system often requires more design and
implementation effort.
By: D. Nanne H.
Cont…

• Languages like Pascal, C and C++ have static type checking.


• Type checking is one of the semantic checking operations.
• –we may not do all type checking at compile-time. –Some systems also
use dynamic type checking too.
• The main purpose of type-checking is to check the correctness, data type
assignments and type-casting of the data types in a program, whether it is
syntactically correct or not before their execution.

By: D. Nanne H.
Type systems

• Type system is a collection of rules implemented by the type checker for assigning
type expressions to the parts of a program.
• Different compilers may use different type systems for the same language
• A type checker implements a type system.
The design of the type-checker depends on:
• Syntactic Structure of language constructs.
• The Expressions of languages.
• The rules for assigning types to constructs (semantic rules).
By: D. Nanne H.
Cont…
• The Position of the Type checker in the Compiler:-

By: D. Nanne H.
Cont…

Description of this Type checking position in Compiler:-


• The token streams from the lexical analyzer are passed to the PARSER.
• The PARSER will generate a syntax tree.
• When a program (source code) is converted into a syntax tree, the type-checker plays a Crucial Role.
• So, by seeing the syntax tree, you can tell whether each data type is handling the correct variable or
not.
• The Type-Checker will check and if any modifications are present, then it will modify.
• It produces a syntax tree, and after that, INTERMEDIATE CODE Generation is done
By: D. Nanne H.
Cont…

• Type checking is carried out in semantic phase, and its information is


added to semantic rules
• The type information produced by the type checker may be needed when
the code is generated.
• It is especially important for overloaded and polymorphic functions

By: D. Nanne H.
Type Expressions

• Types have structure, which we shall represent using type expressions


• Type expression: is the idea of associating each language construct with an
expression describing its type.
• The type of a language construct (like tokens) will be denoted by a type
expression
• A type expressions are defined inductively from a basic type or from constants
using type constructor (which are formed by applying an operator)
By: D. Nanne H.
Cont…

• A type expressions can be:


1. A basic type: is a primitive data type such as integer, real, char, Boolean, … are a
type expression
• A special basic type, type-error will signal a type error during type checking
• void: denote “ the absence of a value” allows statements to be checked
2. A type name: a name can be used to denote a type expression
3. Type expression may contain variables whose values are type expressions
4. A type constructor applied to other type expression is a type expression.
By: D. Nanne H.
Cont…

• A type constructor include


• a. Arrays. If I is int index set and T is a type expression, then array (I, T) is a
type expression;
• denoting the type of an array with elements in T and indices in the range 0 ... n -
1.
• Ex: An array type int[2] can be read as array(2, integer).

By: D. Nanne H.
Cont…

• b. Products. If T1 and T2 are type expressions, the Cartesian product T1 x


T2 is a type expression, x, is left associative. Ex: int x int
• c. Pointers. If T is a type expression then pointer(T) is a type expression
denoting the type pointer to an object of type T.
• Ex: pointer (int)

By: D. Nanne H.
Cont…

• d. Functions. a functions in a programming language has a form of


mapping from a domain type D to a range type R.
• So, the type of such function is denoted by type expression D→R, where,
D is TE of the parameters and R is the TE of the returned value
• Ex: int→int represents the type of a function which takes an int value as
parameter, and its return type is also int.

By: D. Nanne H.
Cont…

• e. Record is a data structure with named fields.


• A type expression can be formed by applying the record type constructor
to the field names and their types.
• Record types can be implemented by applying the constructor record to a
symbol table containing entries for the fields.

By: D. Nanne H.
Specifications of a type checker

• In this section, we specify a type checker for a simple language, in which


the type of each identifier must be declared before the identifier is used.
• The type checker is a translation scheme that synthesizes the type of each
expression from the types of its sub expressions.
• The type declaration with list of names can be done using simplified
grammar that declares just one name at a time.
• The type checker can handle arrays, pointers, statements, and functions.
By: D. Nanne H.
Equivalence of types

• Many type-checking rules have the form; "If two type expressions are
equal then return a certain type else return type error."
• Ambiguities arise when names are given to two type expressions, and the
names are used in subsequent expressions.
• Type equivalence that implemented by a compiler can be explained using
the concepts of structural and name equivalence.

By: D. Nanne H.
Cont…

• a. Structural Equivalence of Type Expressions:-


• If a type expressions built from basic types and constructors, then the equivalence
between two type expressions is structural equivalence.
• Two type expressions are said to be structurally equivalent if and only if one of the
following conditions hold:
• They are built from the same basic type or they are identical,
• They are formed by applying the same constructor to structurally equivalent type
• One is a type name that denotes the other.
By: D. Nanne H.
Cont…

• For example, the type expression integer is equivalent only to integer


because they are the same basic type.
• Similarly, pointer (integer) is equivalent only to pointer (integer) because
the two are formed by applying the same constructor pointer to equivalent
types.

By: D. Nanne H.
Cont…

b. Name equivalence
• In some programming languages, we give a name to a type expression, and we use that
name as a type expression afterwards.
• Two type expressions are name equivalent if and only if they are identical.
• In structural equivalence, names are replaced by type expressions that they define,
• So two type expressions are structurally equivalent if they represent two structurally
equivalent type expressions when all names have been substituted.
• Example1: ptr and pointer (integer) are not name equivalent but they are structurally
equivalent.
By: D. Nanne H.
Type Conversions

• In type conversion, a data type is automatically converted into another


data type by a compiler at the compiler time.
• Type conversion can only be applied to compatible data types.
• Example: int x=30; float y; y=x; // y==30.000000.
Consider expressions:
• x + i; Where x is of type real and i of type integer

By: D. Nanne H.
Cont…

• Of course, the machine cannot execute this operation as it involves different types of
values
• However, most languages accept such expressions to be used;
• The compiler will be in charge of converting one of the operand into the type of the
other.
• The type checker can be used to insert these conversion operations into the
intermediate representation of the source program
• For example, an operator inttoreal may be inserted whenever an operand need to
implicitly converted
By: D. Nanne H.
Cont…

Coercion
• Conversion from one type to another type is known as implicit if it is to be done
automatically by the compiler.
• Implicit-type conversions are also called Coercion and coercion is limited in many
languages.
• Example: An integer may be converted to a real but real is not converted to an integer.
• Conversion is said to be Explicit if the programmer writes something to do the
Conversion.

By: D. Nanne H.
THANKS!
By: D. Nanne H.

You might also like