Download presentation

Presentation is loading. Please wait.

Published byKaren Bel Modified over 2 years ago

1
Chapter 6 Type Checking

2
The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without running the program. It is a static check as opposed to a dynamic check which is performed during run time. Examples: Flow-of-Control: if control leaves a construct is there a place for it to go to? An example is a break statement in C not enclosed within a while, for, or, switch statement. Uniqueness: Each variable should only be declared once. A function or procedure should only be defined once. Labels in a case statement should be unique.

3
Basic types are atomic types with no internal structure visible to the programmer. Pascal examples are: boolean, character, integer and real. Subrange types like 1….10 and enumerated types, like (violet, indigo, blue, green, yellow, orange, red) are also basic types. Constructed types built from other types. Pascal examples are arrays, records, sets and pointers. The type of a language construct is denoted by a type expression. Type expressions are basic types or constructions of type expressions. Basic type expressions: Includes the basic types of the language like boolean, character, integer and real. A special basic type expression is type_error to signal the occurrence of a type check error.

4
Statements have no values and are assigned the basic type expression of void. Type names are basic type expressions. Array(I, T) is a type expression denoting the type of an array with elements of type T and index set I. Cartesian product: If T1 and T2 are type expressions then T1 * T2 is a type expression. The type of a list is the Cartesian product of the types of its elements. E.g. if a and b are of type real then a, b is of real * real type.

5
Records: The type expression for a record with fields F1, F2, …, Fn of types T1, T2, …, Tn resply is : record ((F1 * T1) * (F2 * T2)* … * (Fn * Tn)). Pointers: If T is a type expression then pointer(T) is the type expression for a pointer to an object of type T. Functions: The type expression for a function is D R where D is the type expression for the domain of the function and R is the type expression for the range of the function. If the function has no arguments then the domain type is void. Procedures: A procedure does not return any value so it can be treated like a function where the type expression for the range is void.

6
A Simple Type Checker The grammar is : prog decls; expr decls decls; decls | id : type type char | integer | array[num] of type | type expr literal | num | id | expr mod expr | expr [expr] | expr A prog has decls written before expr so the types of all declared identifiers can be saved in the symbol table before the expression is checked. A translation scheme to save the types of identifiers is:

7
prog decls; expr decls decls; decls decls id : type {addtype(id.entry, T.type)} type char {T.type := char} type integer {T.type := integer} type type1 {T.type := pointer(T1.type)} type array [num] of type1 {T.type := array (1..num.val, T1.type)}

8
Type checking expressions We call a function lookup (e) to fetch the type of entry e in the symbol table. A translation scheme to type check expressions is : expr literal {E.type := char} expr num {E.type := integer} expr id {E.type := lookup (id.entry)} expr expr1 mod expr2 {E.type := if E1.type = integer and E2.type = integer then integer else type_error} expr expr1 [ expr2 ] {E.type := if E2.type = integer and E1.type = array (s,t) then t else type_error} expr expr1 {E.type := if E1.type = pointer (t) then t else type_error}

9
Type Checking Statements The type expression of a statement is void if it is correct or type_error if a type error is found. E.g. prog decls; stmt stmt id := expr {S.type := if lookup (id.entry) = E.type then void else type_error} stmt if expr then stmt1 {S.type := if E.type = boolean then S1.type else type_error} stmt while expr do stmt1 {S.type := if E.type = boolean then S1.type else type_error} stmt stmt1 ; stmt2 {S.type := if S1.type = void then S2.type else type_error}

10
Type checking of calls to no-argument functions. The statement a := b sets a to the value returned by function b if b is a function or to the value of b if b is a variable. expr id {E.type := if lookup(id.entry) = (void t) or lookup (id.entry) = t then t else type_error} Type checking of procedure statements stmt id (expr) {S.type := if E.type = e and lookup(id.entry) = (e void) then void else type_error} stmt id {S.type := if lookup (id.entry) = (void void) then void else type_error}

11
Two standard types : integer and real Four types of variables: integer, real, integer array and real array The index sets of all arrays are integers. We won’t keep track of the limits in type checking. Factors, terms and expressions maybe: integer, real or boolean Each argument of a functinon or a procedure maybe: integer, real, integer array or real array Functions return either integers or reals The symbol table contains the names of variables, functions and procedures

12
Encoding of type expressions: the alphabet has 8 symbols representing integer, real, integer_array, real_array, boolen, void, type_error and . The type expression of an expression_list or a parameter_list is a string of symbols with one symbol for each element of the list. The symbol is used to encode functions and procedures: The type expression for a real function of an integer and a real is: integer real real The type expression for an integer function with no arguments is: void integer The type expression for a procedure with an integer and a real argument is: integer real void The type expression for a procedure with no arguments is: void void The type expressions for functions and procedures in the symbol table could be stored in the name array

Similar presentations

Presentation is loading. Please wait....

OK

Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.

Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Open ppt on mac Ppt on iron and steel industry in india Ppt on communist party of india Download ppt on cultural heritage of india Ppt on central limit theorem example Ppt on sentence completion test Ppt on manual metal arc welding Ppt on corruption in hindi language Ppt on endangered animals Ppt on endangered species of india and africa