UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 6: Type Systems Fall 2009.

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

CPSC 388 – Compiler Design and Construction
Intermediate Code Generation
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types and Arithmetic Operators
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Lecture # 20 Type Systems. 2 A type system defines a set of types and rules to assign types to programming language constructs Informal type system rules,
Compiler Construction
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 7: Semantics Fall 2009 Marco.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 4: Names Fall 2009 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Haskell Fall 2005 Marco Valtorta
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Types I was eventually persuaded of the need to design programming notations so as to maximize.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
Imperative Programming
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Chapter 2 part #4 Operator
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Computer Science Department Relational Operators And Decisions.
Programming Language C++ Xulong Peng CSC415 Programming Languages.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded of the need to design programming notations so as.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
Syntax (2).
Semantics (1).
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Chapter Seven: Expressions and Assignment Statements Lesson 07.
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.
Department of Electronic & Electrical Engineering Statements Blocks{} Semicolons ; Variables Names keywords Scope.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 8 Semantic Interpretation To understand a.
Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
7.2 Arithmetic Expressions
Chapter 7: Expressions and Assignment Statements
Constructing Precedence Table
C# Operator Overloading and Type Conversions
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Data Types, Identifiers, and Expressions
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Explicit and Implicit Type Changes
Lecture 8.
Conditional Statements
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
UNIT V Run Time Environments.
Chapter 3 Operators and Expressions
Compiler Construction
CSCE 314: Programming Languages Dr. Dylan Shell
Operator King Saud University
Compiler Construction
Presentation transcript:

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 6: Type Systems Fall 2009 Marco Valtorta I was eventually persuaded of the need to design programming notations so as to maximize the number of errors that cannot be made, or if made, can be reliably detected at compile time. C.A.R Hoare

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Contents 6.1 Type System for Clite 6.2 Implicit Type Conversion 6.3 Formalizing the Clite Type System

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Motivation: Detecting Type Errors The detection of type errors, either at compile time or at run time, is called type checking. –Type errors occur frequently in programs. –Type errors can’t be prevented/detected by EBNF –If undetected, type errors can cause severe run- time errors. –A type system can identify type errors before they occur.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 6.1 Type System for Clite Static binding Single function: main Single scope: no nesting, no globals Name resolution errors detected at compile time –Each declared variable must have a unique identifier –Identifier must not be a keyword (syntactically enforced) –Each variable referenced must be declared.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Example Clite Program (Fig 6.1) // compute the factorial of integer n void main ( ) { int n, i, result; n = 8; i = 1; result = 1; while (i < n) { i = i + 1; result = result * i; }

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Designing a Type System A set of rules V in highly-stylized English –return true or false –based on abstract syntax Note: standards use concrete syntax –Mathematically a function: V: AbstractSyntaxClass → Boolean Facilitates static type checking Implementation throws an exception if invalid

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Type Rule 6.1 All referenced variables must be declared. –Type map is a set of ordered pairs E.g., {,, } –Can implement as a hash table –Function typing creates a type map –Function typeOf retrieves the type of a variable: typeOf(id) = type

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering public static TypeMap typing (Declarations d) { TypeMap map = new TypeMap( ); for (Declaration di : d) { map.put (di.v, di.t); } return map; } The typing Function Creates a Type Map

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Type Rule 6.2 All declared variables must have unique names. public static void V (Declarations d) { for (int i=0; i<d.size() - 1; i++) for (int j=i+1; j<d.size(); j++) { Declaration di = d.get(i); Declaration dj = d.get(j); check( ! (di.v.equals(dj.v)), "duplicate declaration: " + dj.v); }

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Rule 6.2 example // compute the factorial of integer n void main ( ) { int n, i, result; n = 8; i = 1; result = 1; while (i < n) { i = i + 1; result = result * i; } These must all be unique

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Type Rule 6.3 A program is valid if its Declarations are valid and its Block body is valid with respect to the type map for those Declarations public static void V (Program p) { V (p.decpart); V (p.body, typing (p.decpart)); }

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Rule 6.3 Example // compute the factorial of integer n void main ( ) { int n, i, result; n = 8; i = 1; result = 1; while (i < n) { i = i + 1; result = result * i; } These must be valid.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Type Rule 6.4 Validity of a Statement: A Skip is always valid An Assignment is valid if: –Its target Variable is declared –Its source Expression is valid –If the target Variable is float, then the type of the source Expression must be either float or int –Otherwise if the target Variable is int, then the type of the source Expression must be either int or char –Otherwise the target Variable must have the same type as the source Expression.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering –A Conditional is valid if: Its test Expression is valid and has type bool Its thenbranch and elsebranch Statements are valid –A Loop is valid if: Its test Expression is valid and has type bool Its Statement body is valid –A Block is valid if all its Statements are valid. Type Rule 6.4 (continued)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Rule 6.4 Example // compute the factorial of integer n void main ( ) { int n, i, result; n = 8; i = 1; result = 1; while (i < n) { i = i + 1; result = result * i; } This assignment is valid if: n is declared, 8 is valid, and the type of 8 is int or char (since n is int).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Rule 6.4 Example // compute the factorial of integer n void main ( ) { int n, i, result; n = 8; i = 1; result = 1; while (i < n) { i = i + 1; result = result * i; } This loop is valid if i < n is valid, i < n has type bool, and the loop body is valid

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Type Rule 6.5 Validity of an Expression: A Value is always valid. A Variable is valid if it appears in the type map. A Binary is valid if: –Its Expressions term1 and term2 are valid –If its Operator op is arithmetic, then both Expressions must be either int or float –If op is relational, then both Expressions must have the same type –If op is && or ||, then both Expressions must be bool A Unary is valid if: –Its Expression term is valid, –…

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Type Rule 6.6 The type of an Expression e is: –If e is a Value, then the type of that Value. –If e is a Variable, then the type of that Variable. –If e is a Binary op term1 term2, then: If op is arithmetic, then the (common) type of term1 or term2 If op is relational, && or ||, then bool –If e is a Unary op term, then: If op is ! then bool …

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Rule 6.5 and 6.6 Example // compute the factorial of integer n void main ( ) { int n, i, result; n = 8; i = 1; result = 1; while (i < n) { i = i + 1; result = result * i; } This Expression is valid since: op is arithmetic (*) and the types of i and result are int. Its result type is int since: the type of i is int.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 6.2 Implicit Type Conversion Clite Assignment supports implicit widening conversions We can transform the abstract syntax tree to insert explicit conversions as needed. The types of the target variable and source expression govern what to insert.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Suppose we have an assignment f = i - int(c); ( f, i, and c are float, int, and char variables). The abstract syntax tree is: Example: Assignment of int to float

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering So an implicit widening is inserted to transform the tree to: Here, c2i denotes conversion from char to int, and itof denotes conversion from int to float Note: c2i is an explicit conversion given by the operator int() in the program Example (cont’d)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Contents 6.1 Type System for Clite 6.2 Implicit Type Conversion 6.3 Formalizing the Clite Type System

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 6.3 Formalizing the Clite Type System Type map: Created by: (Type Rule 6.1) Validity of Declarations: (Type Rule 6.2)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Validity of a Clite Program (Type Rule 6.3)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Validity of a Clite Statement (Type Rule 6.4)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Validity of a Clite Expression (Type Rule 6.5, abbreviated versions for Binary and Unary)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Type of a Clite Expression (Type Rule 6.6, abbreviated version)