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

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
Intermediate Code Generation
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.
1 C++ Syntax and Semantics The Development Process.
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.
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.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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.
Chapter 9: Functional Programming in a Typed Language.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Bernd Fischer RW713: Compiler and Software Language 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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
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.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Java Statements –Java Expressions –Postfix Expressions –Prefix Expressions –Evaluating.
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.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Announcements Quiz this Thursday 1. Multi dimensional arrays A student got a warning when compiling code like: int foo(char **a) { } int main() { char.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Chapter 02 (Part II) Introduction to C++ Programming.
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.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Principles of programming languages 10: Object oriented languages
Principles of programming languages 12: Functional programming
Principles of programming languages 8: Types
Information Science and Engineering
Java Primer 1: Types, Classes and Operators
Principles of programming languages 4: Parameter passing, Scope rules
Stacks Chapter 4.
Data Types.
Compiler Construction
Course Overview PART I: overview material PART II: inside a compiler
Compiler Construction
Presentation transcript:

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

Typed languages Statically-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. char ( * ( * x ( ) ) [3] ) ( ) ; 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 :  * * e :  We use metavariables e and  for representing expressions and types respectively. 0  i < n, where n is a positive integer.

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 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 = 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 & We add the following inference rules about the address operator &. where the outermost part of  is not &. e :  &e :  & e :  & * e :  e :  * e’ :  & e = e’ :  &

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.