Presentation is loading. Please wait.

Presentation is loading. Please wait.

C H A P T E R T H R E E Type Systems and Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.

Similar presentations


Presentation on theme: "C H A P T E R T H R E E Type Systems and Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan."— Presentation transcript:

1 C H A P T E R T H R E E Type Systems and Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

2 Type Systems :  A Type is a well-defined set of values and operations.  In C - Int, float, char.  In FORTRAN – INTEGER, REAL, COMPLEX.  A Type System associates types with variables and other objects defined in a program.  A Type Error occurs when an operation is attempted on a value for which it is not defined  A Strongly Typed language allows all type errors to be detected before the offending statement can be executed.

3 Static Typing :  A Statically Typed language associates a single type with a variable for the entire existence of that variable.  Variable types are determined at compile time.  This means that type errors can also be checked at compile time.  Run time checking can also be reduced since the need to coerce value types can be detected at compile time.  Examples – C, C++, Java.

4 Dynamic Typing :  A Dynamically Typed language allows the a variable’s type to change during program execution.  This can be very convenient but can make debugging more difficult.  There can be performance penalties (why?)  Examples – Lisp, Scheme, Perl.

5 Formalizing Typing :  BNF is not capable of defining type checking requirements for languages.  Can’t ensure all variables have unique names.  Can’t express the need to declare all variables prior to use.  A program is Type-safe if it is free of typing errors.  A Type Map is used to formally define the rules for writing type-safe programs.  Contains pairs of declared variables and their types.  Analogous to a symbol table.

6 Formalizing Typing :  Programs written in strongly typed languages are type safe.  If all the programs that can be written in a language are type safe then the language is strongly typed.  Dynamically typed languages are also type safe as long as they can always coerce values at runtime (otherwise?)  Programs written in strongly typed languages are defined as having two parts:  A declarations section.  A body section.

7 Type Checking in jay :  Each declared Variable must have a unique Identifier.  Each Variable’s type must be either int or boolean.  Each Variable referenced within any Expression in a program must have been declared.  For every Assignment statement the type of the target variable must agree with the type of the source expression.  For every Conditional and Loop the expression type must be boolean.

8 Type Checking in jay :  Expression result types are determined as follows:  If the Expression is a Variable or Value then the result is of the same type as the Variable or Value.  If the Expression’s Operator is arithmetic (+, -, *, /) then…  All terms must be of type int.  The result is of type int.  If the Operator is relational (, >=, ==, !=) then…  All terms must be of type int.  The result is of type boolean.  If the Operator is boolean (&&, ||, !) then…  All terms must be of type boolean.  The result is of type boolean.

9 A C/C++ Factorial Function Figure 3.2

10 Next time… More On Semantics

11 Programming Paradigms:  Imperative Programming.  Programs are a series of steps with input and output.  Traditional stuff like C, Fortran, and Cobol.  Object Oriented Programming (OOP).  Programs consist of objects passing messages.  Smalltalk, C++, and Java.  Functional Programming.  Programs are collections of functions.  Lisp, Scheme, Haskell, and ML.

12 Programming Paradigms:  Logic (Declarative) Programming.  Programs are collections of logical declarations.  Prolog and expert systems.  Event-driven Programming.  Programs are loops responding to events.  Visual Basic, Java, but can be almost any language.  Concurrent Programming.  Programs consist of cooperating processes.  High Performance Fortran (HPF), Linda, and SR. Choose One!


Download ppt "C H A P T E R T H R E E Type Systems and Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan."

Similar presentations


Ads by Google