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.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Symbol Table.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
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 2 Syntax A language that is simple to parse.
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 14 Functional Programming It is better to.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 6: Type Systems Fall 2009.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Imperative Programming
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Syntax – Intro and Overview CS331. Syntax Syntax defines what is grammatically valid in a programming language –Set of grammatical rules –E.g. in English,
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
7-1 7 Contextual analysis  Aspects of contextual analysis  Scope checking  Type checking  Case study: Fun contextual analyser  Representing types.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
CPS 506 Comparative Programming Languages Syntax Specification.
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.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Syntax (2).
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Bernd Fischer RW713: Compiler and Software Language 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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
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.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 8 Semantic Interpretation To understand a.
Arithmetic Instructions. Integer and Float Conversions.
Lecture 9 Symbol Table and Attributed Grammars
Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
Chapter 7: User-Defined Functions II
Semantic Analysis Chapter 4.
Constructing Precedence Table
Suppose we want to print out the word MISSISSIPPI in big letters.
Programming Languages 2nd edition Tucker and Noonan
Explicit and Implicit Type Changes
Programming Languages 2nd edition Tucker and Noonan
CSE 3302 Programming Languages
Conditional Statements
Representation, Syntax, Paradigms, Types
Programming Languages 2nd edition Tucker and Noonan
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

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 the number of errors which cannot be made, or if made, can be reliably detected at compile time. C.A.R. Hoare

Type Systems – Review Type Checking: the detection of type errors, either at compile time or at run time. –Type error: attempt to perform an operation on a value from a data type for which it is not defined Type System: a precise definition of the bindings between a variable’s type, its values, and the possible operations on the values –Describes how new types are defined and how to tell when a type is used correctly Recall that a language is strongly typed if its type system makes it possible to detect type errors either at compile time or run time. Type safety is similar to strong typing: use of a type system prevents some/most/all type errors.

Implementing a Type System Cannot be defined in BNF or EBNF notation –Cannot represent context-sensitive constraints, e.g. uniqueness of an identifier within a given scope Other possible approaches: –Write boolean functions to define each type rule –Express the rules in English and give an algorithm to implement the rules

Type System for Clite A set of validity functions define the various rules, such as “variables have unique names”. Each function returns a boolean value to tell whether a part of the program’s abstract syntax tree is type valid. The type checking system uses a type map: a function that binds a variable name to its type.

6.1 Type System for CLite Single function: main Single scope: no nesting, no globals Name resolution errors detected at compile time Static binding –Each declared variable must have a unique identifier –Identifier must not be a keyword (syntactically enforced) –Each variable referenced must be declared –Etc. Type rules test validity of different abstract syntax classes (see p. 53)

Clite Type Rules All referenced variables must be declared All declared variables must have unique names A Program is type valid if it Declarations decpart is valid and its Block body is valid with respect to the type map for those particular Declarations A Statement is valid with respect to the program’s type map if it satisfies the following constraints: … (specific validity conditions for each statement type) The validity of an Expression is defined using the program’s type map and each of the Expression’s subclasses: … (validity conditions for the various types of Expressions: Value, Variable, Binary, or Unary Every Expression’s result type is determined as follows: … (includes rules for implicit type conversions)

Example Clite Program (Fig 6.1) // compute 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; }

Type Maps A Type map is a set of ordered pairs; e.g., {,, } Can implement as a hash table where the identifier is the key & the type is the value. The method typing (p. 137) creates a type map based on a program’s declarations Given a typemap map and a variable vr, method typeOf retrieves the type of a variable: vartype = typeOf(vr,map)

public static TypeMap typing (Declarations d) { TypeMap map = new TypeMap( ) for (Declaration di : d) { map.put (di.v, di.t); } return map; } based on abstract syntax for Clite Declaration: Declaration = VariableDecl | ArrayDecl VariableDecl = Variable v ; Type t ArrayDecl = Variable v ; Type t ; Integer size The typing function creates a type map

Clite Type Rules Type rule 6.1: All referenced variables must be declared. –Check the type map for each variable reference Type rule 6.2: all declared variables must have unique names –See function on next page

Implementing 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); }

Clite Type Rules 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)); }

Clite type rules Type rule 6.4: a statement is valid if –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.

Type Rule 6.4 (continued) –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.

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).

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

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, …

Type Rule 6.6: The result type of an Expression e is determined as follows: –If e is a Value, then the result type is the type of that Value. –If e is a Variable, then the result type is 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 …

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 and result is int.

6.3: Formalizing the Clite Type System Example of a formal statement of the Clite type rules In 6.1, less formal presentation + an example of Java functions that can be used to perform type checking

6.3 Formalizing the Clite Type System Type map: Created by: (Type Rule 6.1) Validity of Declarations: (Type Rule 6.2)

Validity of a Clite Program (Type Rule 6.3)