Download presentation

Presentation is loading. Please wait.

Published byAdrian Gorman Modified over 4 years ago

1
CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible type? flow of control - does control pass properly from one construct to another? uniqueness - are objects multiply defined? name-checks - if required, do two references to a particular object use the same name?

2
CS3012: Formal Languages and Compilers Type Checking A type checker verifies that the type of a construct matches that expected by its context. We can define expected types by a type signature, which displays the operator, the expected types of the operands, and the type of the result. Example: _mod_ : integer x integer -> integer

3
CS3012: Formal Languages and Compilers Operator Overloading The "+" operator may be used in a number of different contexts: for example _+_ : integer x integer -> integer _+_ : integer x real -> real _+_ : real x integer -> real _+_ : real x real -> real In order to add together two different types in this way, some form of type translation is required. An operator which represents different operations in different contexts is called an overloaded operator

4
CS3012: Formal Languages and Compilers Basic and Constructed Types In many languages, types can be either basic or constructed. Basic types have, essentially, no internal structure: e.g. integer, real, boolean Constructed types are built from a number of basic types: e.g. arrays, structures,... Every language construct has a type associated with it, represented by a type expressions.

5
CS3012: Formal Languages and Compilers Type Expressions A type expression is a basic type, or is a type constructor applied to some type expressions. A basic type is a type expression. type_error is a basic type void is a basic type A type name is a basic type Type constructors include: arrays products records functions

6
CS3012: Formal Languages and Compilers arrays and products array If T is a type expression and I is some index set, then array(I,T) is a type expression. Example: var A : array[1..10] of integer; declares an array variable A with type expression of array(1..10, integer) product If T 1 and T 2 are type expressions, then T 1 x T 2 is a type expression.

7
CS3012: Formal Languages and Compilers records A record is a product with a name for its fields. The type constructor will operate on both the field names and types. Example: type row = record address : integer; lexeme : array(1..15) of char; end; declares a typename called row with the type expression: record((address x integer) x (lexeme x array(1..15,char)))

8
CS3012: Formal Languages and Compilers functions In maths, a function maps elements from one set into another. Here, a function will map elements of one type into elements of another type. function If T 1, T 2 and T 3 are type expressions, then T 1 x T 2 -> T 3 is a type expression Example: function f(a, b: char) : integer has the type expression: char x char -> integer

9
CS3012: Formal Languages and Compilers Types as Syntax Trees A type expression can be represented as a syntax tree: the leaf nodes are the basic types, and the interior nodes are the type constructors (or operators). T2T2 T1T1 product: x integer 1..10 array

10
CS3012: Formal Languages and Compilers 1.. 10 char array lexeme integeraddress x x x char integer x -> f: record:

11
CS3012: Formal Languages and Compilers Type Systems A type system is a collection of rules for assigning type expressions to the different parts of a program. A type checker is an implementation of a type system. A type checker must report the location nature of a type error, and should also recover to check the remainder of the code. Note that this may require a more complex system than for simply checking types.

12
CS3012: Formal Languages and Compilers Example Type Checker P -> D ; E D -> D ; D D -> id : T T -> char | integer | array[num] of T E -> num | id | E mod E | E [E] | id := E D -> id : T T -> char T -> integer T 1 -> array[num] of T 2 addtype(id.entry, T.type) T.type := char T.type := integer T 1.type := array(1..num.value, T 2.type)

13
CS3012: Formal Languages and Compilers E -> num E -> id E 1 -> E 2 mod E 3 E 1 -> E 2 [E 3 ] E 1 -> id := E 2 E.type := integer E.type := lookup(id.entry) E 1.type := if E 2.type = integer and E 3.type = integer then integer else type_error E 1.type := if E 3.type = integer and E 2.type = array(s,t) then t else type_error E 1.type := if lookup(id.entry) = E 2.type then void else type_error

Similar presentations

OK

1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.

1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google

Ppt on db2 introduction Ppt on change management Ppt on industrial revolution in india Ppt on power line carrier communication Ppt on business cycle phases in order Ppt on computer malware anti-malware Ppt on number system for class 10 Ppt on marketing management by philip kotler definition Ppt on pre-ignition procedure Ppt on range of hearing