Principles of programming languages 8: Types

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

CPSC 388 – Compiler Design and Construction
Semantics Static semantics Dynamic semantics attribute grammars
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Principles of programming languages 1: Introduction (with a simple language) Isao Sasano Department of Information Science and Engineering.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Type checking © Marcelo d’Amorim 2010.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Type Checking.
Compiler Construction
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Pointer Data Type and Pointer Variables
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Basic Semantics Associating meaning with language entities.
Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
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.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
CMSC 330: Organization of Programming Languages Operational Semantics.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Windows Programming Lecture 03. Pointers and Arrays.
Principles of programming languages 10: Object oriented languages
Programming Languages and Compilers (CS 421)
The Machine Model Memory
Principles of programming languages 12: Functional programming
Context-Sensitive Analysis
Expressions and Assignment
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Information Science and Engineering
7. Inheritance and Polymorphism
Compiler Construction (CS-636)
Java Primer 1: Types, Classes and Operators
Principles of programming languages 4: Parameter passing, Scope rules
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Stacks Chapter 4.
Data Types.
Chapter 10: Pointers 1.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
More On Enumeration Types
Pointer Data Type and Pointer Variables III
Semantic Analysis Chapter 6.
CS 432: Compiler Construction Lecture 11
Compiler Construction
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Course Overview PART I: overview material PART II: inside a compiler
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Compiler Construction
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Presentation transcript:

Principles of programming languages 8: Types Department of Information Science and Engineering Isao Sasano

Typed languages Statically-typed languages Dynamically-typed languages Check the consistency of types in compile time (ex.) C, Java, Pascal, etc. Dynamically-typed languages Check the consistency of types in runtime (ex.) Lisp, Emacs Lisp, Scheme, etc.

The role of types /* example */ int f ( ) { int x, y; x = 4; y = 3 + x; return y; } Programs in statically-typed languages must have type consistency in compile time.

Type checking Consistency of programs is checked in compile time with respect to the typing constraint of the language. Type checking partially ensures the correctness of programs and decreases the runtime errors. Types are static semantics (information that is obtained without executing programs) and type checking is a kind of static program analysis. Type checking is performed after the parsing in compilers.

Variable declarations in C (ex.) int (*a) [13]; This declares a variable a of type pointer to array of length 13 of int. Expressions (*a) [ j ] (0  j < 13) have type of int. An expression (* a) [0] has type of int.

Variable declarations in C (ex.) int (*a) [13]; int b [2] [13]; Under the above declarations, the assignment a = b is consistent with respect to types. b is replaced with &b[0] in compile time. The following equation holds just after the execution of the assignment expression. (*a) [ j ] = b[0][ j ] (0  j < 13) This is obtained by adding [ j ] to the equations *a = *b = *(&b[0]) = b[0]. We study how to check the type consistency of this kind of programs.

Variable declarations in C Can you read the following variable declaration? char ( * ( * x ( ) ) [3] ) ( ) ; Read the variable x firstly and then go outside according to the precedence in the next page and then finally read char. ( ) * [3] * ( ) char By reversing this, we obtain the following. char ( ) * [3] * ( ) By writing this after x : , we obtain the following. x : char ( ) * [3] * ( ) We call this a declaration of x in the postfix notation.

Precedence The precedence is defined as ( ), [ ], * in descending order. We can use parentheses for overriding this precedence. In the declaration char ( * ( * x ( ) ) [3] ) ( ) ; the parentheses for overriding the precedence is in bold font in the following. By take into consideration the precedence and parentheses, we read the declaration in the following order. ( ) * [3] * ( ) char

Exercise 1 Rewrite the following variable declaration in C in the postfix notation. char ( * ( * y [3] ) ( ) ) [5] ;

Exercise 2 Rewrite the following variable declarations (1) and (2) in C in the postfix notation. (1) int * z; (2) int c [13];

Exercise 3 Rewrite the following variable declarations (1) and (2) in C in the postfix notation. (1) int (*a) [13]; (2) int b[2][13];

An example Under the variable declaration char ( * ( * y [3] ) ( ) ) [5] ; what type does the expression y [2] have? By rewriting the declaration in the postfix notation, we obtain y : char [5] * ( ) * [3] By removing the outermost [3], we obtain y [2] : char [5] * ( ) *

Exercise 4 Under the declaration int (*a) [13]; what type does the expression *a have?

Inference rules e :  [ n ] e [ i ] :  e :  ( ) e ( ) :  e :  * 0  i < n, where n is a positive integer. We use metavariables e and  for representing expressions and types respectively.

An example Under the declaration int (*a) [13] ; the expression *a had type of int [13] in postfix notation (cf. exercise 4). We can derive this from the type in postfix notation by applying an inference rule. a : int [13] * *a : int [13]

An example Under the declaration int (*a) [13] ; the expression (*a) [3] has type of int. We can derive this from the type in postfix notation by applying two inference rules. a : int [13] * *a : int [13] (*a) [3] : int

Exercise 5 Under the declaration int b [2] [13] ; derive the type of the expression b [1] by applying inference rules to the type of b in the postfix notation.

Exercise 6 Under the declaration int b [2] [13] ; derive the type of the expression b [1] [4] by applying inference rules to the type of b in the postfix notation.

Array types e :  [ n ] e :  & We add the following inference rule about array types. e :  [ n ] e :  & The notation e :  & shows that e :  * holds and e does not have address (i.e., e is an non-l-value expression.) The rule means that when the outermost is an array type we can change it to the corresponding pointer type.

Assignment operator = e :  e’ :  e = e’ :  We add an inference rule about the assignment operator =. e :  e’ :  e = e’ :  where e is an l-value expression and is not a constant (i.e., is modifiable).

Address operator & e :  &e :  & e :  & * e :  e :  * e’ :  & We add the following inference rules about the address operator &. e :  &e :  & e :  & * e :  e :  * e’ :  & e = e’ :  & where the outermost part of  is not &.

The first example Under the following declarations a : int [13] * b : int [13] [2] we can show that the assignment expression a = b is consistent with respect to types by applying inrefence rules to the declarations in the postfix notation. b : int [13] [2] b : int [13] & a : int [13] * a = b : int [13] &

Notes In the full set of C, functions may have parameters. The full set have several other constructs such as structures and unions. Note that in C the type of union is not checked, so programmers have to write programs with taking into account which of the components each union has at every moment.

Exercise 7 Under the following declarations p : int * a : int [10] show that the assignment expression p = &a[1] is consistent with respect to types by using the inference rules.