COS 140: Foundations of Computer Science

Similar documents
COS 140: Foundations of Computer Science

CPSC 3740 Programming Languages University of Lethbridge. Data Types

NOTE: Answer ANY FOUR of the following 6 sections:

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

22c:111 Programming Language Concepts. Fall Types I

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Data Types In Text: Ch C apter 6 1

Types. What is a type?

Chapter 6. Data Types

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Types II. Hwansoo Han

TYPES, VALUES AND DECLARATIONS

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

COSC252: Programming Languages: Basic Semantics: Data Types. Jeremy Bolton, PhD Asst Teaching Professor

CS 3360 Design and Implementation of Programming Languages. Exam 1

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Chapter 5 Names, Binding, Type Checking and Scopes

Chapter 5. Names, Bindings, and Scopes

CSE 452: Programming Languages. Where are we? Types: Intuitive Perspective. Data Types. High-level Programming Languages.

CSCI312 Principles of Programming Languages!

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CS558 Programming Languages

Lecture 12: Data Types (and Some Leftover ML)

CS 230 Programming Languages

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 6. Structured Data Types. Topics. Structured Data Types. Vectors and Arrays. Vectors. Vectors: subscripts

Programming Languages Third Edition. Chapter 7 Basic Semantics

Programming Languages, Summary CSC419; Odelia Schwartz

Chapter 8 :: Composite Types

Array. Prepared By - Rifat Shahriyar

CS558 Programming Languages

Language Reference Manual simplicity

MIDTERM EXAMINATION - CS130 - Spring 2003

Programmiersprachen (Programming Languages)

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

COS 140: Foundations of Computer Science

G Programming Languages - Fall 2012

learning objectives learn about variables, their types and their values learn about different number representations

Lecture 13: Complex Types and Garbage Collection

Programming Languages

CS 314 Principles of Programming Languages

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Week 3 Lecture 2. Types Constants and Variables

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Programming Methodology

CS 3360 Design and Implementation of Programming Languages. Exam 1

Values and Variables 1 / 30

Names, Bindings, Scopes

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Programming Languages: Lecture 11

Concepts of Programming Languages

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

Program Fundamentals

Lecture Notes on Programming Languages

BM214E Object Oriented Programming Lecture 4

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

Final-Term Papers Solved MCQS with Reference

CSC 533: Organization of Programming Languages. Spring 2005

Chapter 6. Data Types ISBN

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Derived and abstract data types. TDT4205 Lecture 15

EMBEDDED SYSTEMS PROGRAMMING Language Basics

How to declare an array in C?

Type Bindings. Static Type Binding

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Full file at

Properties of an identifier (and the object it represents) may be set at

Programming Lecture 3

CS558 Programming Languages. Winter 2013 Lecture 3

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

HANDLING NONLOCAL REFERENCES

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

LECTURE 18. Control Flow

VARIABLES AND TYPES CITS1001

The Warhol Language Reference Manual

Topic 9: Type Checking

Topic 9: Type Checking

Chapter 6. Data Types ISBN

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

COMP2121: Microprocessors and Interfacing. Number Systems

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

Zheng-Liang Lu Java Programming 45 / 79

Continuations provide a novel way to suspend and reexecute

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

CS112 Lecture: Primitive Types, Operators, Strings

Example: Haskell algebraic data types (1)

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Computing Seminar Introduction Oct

MIDTERM EXAMINATION - CS130 - Spring 2005

Python Input, output and variables

JAVA Programming Fundamentals

Informatica 3 Marcello Restelli

Transcription:

COS 140: Foundations of Variables and Primitive Data Types Fall 2017 Copyright c 2002 2017 UMaine School of Computing and Information S 1 / 29

Homework Reading: Chapter 16 Homework: Exercises at end of chapter Homework due 10/23 Copyright c 2002 2017 UMaine School of Computing and Information S 2 / 29

What is a variable? What is a variable? Variable attributes Binding In this lecture: imperative/oo languages A variable is an area of memory that: can have a value can have its value changed by the program Different sizes: byte, word, multiple words Copyright c 2002 2017 UMaine School of Computing and Information S 3 / 29

Variable attributes What is a variable? Variable attributes Binding Name what do we call it? Address where does it live in memory? Type what kind of thing can it hold? Value contents of the variable Scope who can see this variable? Lifetime duration of program, or shorter period? Copyright c 2002 2017 UMaine School of Computing and Information S 4 / 29

Binding What is a variable? Variable attributes Binding Associates an attriburte value with an attribute Not just value attribute Two types: Static binding: occurs before run-time, not changed afterward Allows compiler to check for errors, but......need information about the binding at compile time Dynamic binding: takes place/is changed at run-time Copyright c 2002 2017 UMaine School of Computing and Information S 5 / 29

Name length Special names Name: identifier associated variable Same name different variables in different contexts Different names same variable Unnamed variables: access via pointer Copyright c 2002 2017 UMaine School of Computing and Information S 6 / 29

Name length Special names Strings (symbols) in a symbol table Symbol tables created at compile/interpretation time Associate names with entities (e.g., variables) Copyright c 2002 2017 UMaine School of Computing and Information S 7 / 29

Name length Special names Which characters to allow? Have to recognize names during lexical analysis no spaces Sometimes special initial character e.g., a letter For enhanced readability, may allow connectors (e.g., -, ), case-sensitivity Copyright c 2002 2017 UMaine School of Computing and Information S 8 / 29

Length of variable names Name length Special names Names should be meaningful longer names But symbol table should be reasonable size, easy to maintain shorter names BASIC: one letter + optional number Can t complain about the size! Readable for mathematical applications, but not much else Limited number of characters e.g., FORTRAN (6 or 31), C (63 significant) Trades-off readability and wasted space Table maintenance easy, but whatever the size, someone always wants more Unlimited number of characters e.g., Ada, Lisp Complete flexibility, but......potentially great deal of space, hard to maintain Copyright c 2002 2017 UMaine School of Computing and Information S 9 / 29

Special names Name length Special names Some names not allowed or limited Reserved words can only be used in language-defined context(s) if in C, e.g. Predefined words defined in the language, but can be changed (e.g., Ada, C [libraries]) Keywords special meaning in some contexts, but can be used as a name (e.g.,for in Lisp,if in FORTRAN) Copyright c 2002 2017 UMaine School of Computing and Information S 10 / 29

Giving variables values construct binds a value to a variable Often, a construct such as: x = a+b x s value is bound to result ofa+b Not algebra: can have: x = x+1 Copyright c 2002 2017 UMaine School of Computing and Information S 11 / 29

construct examples C, Java, Python... x = x + 1; Pascal x := x + 1 Lisp (setf x (1+ x)) R x <<- x + 1 orx + 1 -> x Smalltalk x x + 1 Forth x 1 + x! COBOL ADD X 1 GIVING X TCL set x x + 1 Copyright c 2002 2017 UMaine School of Computing and Information S 12 / 29

All data has a type: integer, floating point, etc. Type species size, interpretation, operations for data All variables have a type attribute Depending on language, variables type static or dynamic Determining variable type: Declared somewhere in program (static, explicit) Inferred by compiler/interpreter (static, implicit; dynamic) Copyright c 2002 2017 UMaine School of Computing and Information S 13 / 29

What are primitive data types? : defined by language Not (usually) defined in terms of other types Building blocks of new types Unstructured/atomic data types (scalars): Integers Floating point numbers Booleans Characters Pointers Structured data types: Strings Arrays Records Copyright c 2002 2017 UMaine School of Computing and Information S 14 / 29

Integers Integer type represents subset of the integersz(duh) Most common data type You ve seen: sign-magnitude, two s complement Most languages: several sizes (byte, 2 byte, 4 byte...) Can t represent all possible integers in c instead i...j, wherei 0 j For two s complement, withnbits, can represent 2 n 1...2 n 1 1 E.g., 8 bits can represent10000000 2 (= 128 10 ) to 01111111 2 (=127 10 ). Some languages: unsigned integers (C) Copyright c 2002 2017 UMaine School of Computing and Information S 15 / 29

Floating point numbers Represent subset of real numbersr Usually two sizes Two parts: fraction and exponent E.g.: 0.1101 2, exponent101 2 Copyright c 2002 2017 UMaine School of Computing and Information S 16 / 29

Floating point numbers Represent subset of real numbersr Usually two sizes Two parts: fraction and exponent E.g.: 0.1101 2, exponent101 2 Fractional part= 1 2 + 1 4 + 0 8 + 1 16 = 0.8125 10 Copyright c 2002 2017 UMaine School of Computing and Information S 16 / 29

Floating point numbers Represent subset of real numbersr Usually two sizes Two parts: fraction and exponent E.g.: 0.1101 2, exponent101 2 Fractional part= 1 2 + 1 4 + 0 8 + 1 16 = 0.8125 10 Exponent = 5, so multiply fraction by2 5 Copyright c 2002 2017 UMaine School of Computing and Information S 16 / 29

Floating point numbers Represent subset of real numbersr Usually two sizes Two parts: fraction and exponent E.g.: 0.1101 2, exponent101 2 Fractional part= 1 2 + 1 4 + 0 8 + 1 16 = 0.8125 10 Exponent = 5, so multiply fraction by2 5 So number is0.8125 32 = 26.0 10 Copyright c 2002 2017 UMaine School of Computing and Information S 16 / 29

Floating point numbers Represent subset of real numbersr Usually two sizes Two parts: fraction and exponent E.g.: 0.1101 2, exponent101 2 Fractional part= 1 2 + 1 4 + 0 8 + 1 16 = 0.8125 10 Exponent = 5, so multiply fraction by2 5 So number is0.8125 32 = 26.0 10 Can t represent all numbers accurately e.g.,π, 1 3 Copyright c 2002 2017 UMaine School of Computing and Information S 16 / 29

Booleans True/false values Good for flags (switches), conditions to be checked Sometimes languages provide as primitive data type If not: 0 is false, everything else true (C) 0,, [], etc., is false, everything else is true (Python) Representation: smallest efficiently-addressable unit (often byte) Copyright c 2002 2017 UMaine School of Computing and Information S 17 / 29

Characters Character represented as a pattern of 1s and 0s Representations: ASCII 7 bits code; 8-bit variants of this (e.g., ISO 8859 1) are the most common codes used E.g.: a = 33, b = 34,...; space = 32 Unicode 16 bits (or longer); accommodates other alphabets and symbols E.g.: - codepoints 47196, 51060; ; EBCDIC 8 bits; old IBM code Copyright c 2002 2017 UMaine School of Computing and Information S 18 / 29

Strings Sequence of characters: textual data Special operators (e.g., substring, concatenation), special relational operators, sometimes special assignment Lengths: Fixed length: always same size Limited-dynamic can change, but there s a maximum (e.g., C) Length + sequence (often array) of characters Null-terminated sequence of characters Dynamic can change without limit (e.g., Lisp) more flexible, but overhead for allocation/deallocation Copyright c 2002 2017 UMaine School of Computing and Information S 19 / 29

Arrays Sometimes need to represent groups of related data items (e.g., integers, characters, etc.) An array is a data type that stores homogeneous data in contiguous memory Can be single-dimensional (vectors) or multi-dimensional Use index to find element Value of index doesn t affect time taken to find element random access storage Copyright c 2002 2017 UMaine School of Computing and Information S 20 / 29

Design issues for Arrays Syntax for array indices e.g.,temperatures(20) or Temperatures[20] Subscripts: what is the lower bound? bounds checking or not? must subscripts be integers? Number of dimensions allowed Usually no real limits C: limits to one dimension, but allows array elements to be themselves arrays Copyright c 2002 2017 UMaine School of Computing and Information S 21 / 29

Array descriptors What information is needed by the programming language/program about an array? Base address where is the first element Element type Index type doesn t have to be integer for some languages Index lower, upper bounds Number of locations in array Copyright c 2002 2017 UMaine School of Computing and Information S 22 / 29

Finding address of array element Element location (or address, A) depends on base address (B), index (I), index lower bound (L), and element size (S) A = B +(I L) S Copyright c 2002 2017 UMaine School of Computing and Information S 23 / 29

Finding address of array element Element location (or address, A) depends on base address (B), index (I), index lower bound (L), and element size (S) A = B +(I L) S Given an array Taxes whose start location is 1024, element type is a long integer (8 bytes), and whose indices start at 0, find location of elementtaxes[15] 1024 1028 1032... Array "Taxes" Taxes[0] Taxes[1] Taxes[15] Copyright c 2002 2017 UMaine School of Computing and Information S 23 / 29

Finding address of array element Element location (or address, A) depends on base address (B), index (I), index lower bound (L), and element size (S) A = B +(I L) S Given an array Taxes whose start location is 1024, element type is a long integer (8 bytes), and whose indices start at 0, find location of elementtaxes[15] 1024 1028 1032... Array "Taxes" Taxes[0] Taxes[1] Taxes[15] A = 1024+(15 0) 8 = 1144 Copyright c 2002 2017 UMaine School of Computing and Information S 23 / 29

Another example Suppose arrayfoo has elements of a type that requires 6 bytes to store, that it begins at location 4096, and that its indices begin at 10. What is the address offoo[201]? Copyright c 2002 2017 UMaine School of Computing and Information S 24 / 29

Another example Suppose arrayfoo has elements of a type that requires 6 bytes to store, that it begins at location 4096, and that its indices begin at 10. What is the address offoo[201]? Answer: A = B +(I L) S = 4096+(201 10) 6 = 5242 Copyright c 2002 2017 UMaine School of Computing and Information S 24 / 29

Optimizing address computation Can write expression so static parts can be calculated at compile time and stored in a constant: A = B +(I L) S = B +(I S) (L S) = (I S)+(B L S) constant part Copyright c 2002 2017 UMaine School of Computing and Information S 25 / 29

Multidimensional arrays Can have more than 1 dimension E.g., 10 10 array is 2D, 100 entries, 10 rows, 10 columns Indices: [row,column] or [row][column] How to store dimensions? Row-major order: Rows are kept together, elements stored one row after another T: array 1..10, 1..10 of integers row 1 row 2 row 10... Column-major order: keeps columns together Row-major probably more common Copyright c 2002 2017 UMaine School of Computing and Information S 26 / 29

Address calculation ArrayTis an 2D array of temperatures taken over a 1-meter grid, 100 m on a side Assume row-major order, float data type (assume 4 bytes), base address fort=2048, indices each start at 0 (so 0..99) What is the address oft[20,15]? Copyright c 2002 2017 UMaine School of Computing and Information S 27 / 29

Address calculation ArrayTis an 2D array of temperatures taken over a 1-meter grid, 100 m on a side Assume row-major order, float data type (assume 4 bytes), base address fort=2048, indices each start at 0 (so 0..99) What is the address oft[20,15]? First: what is start of row 20? A [20,0] = B +(I L) S = 2048+(20 0) (4 100) = 10,048 Copyright c 2002 2017 UMaine School of Computing and Information S 27 / 29

Address calculation ArrayTis an 2D array of temperatures taken over a 1-meter grid, 100 m on a side Assume row-major order, float data type (assume 4 bytes), base address fort=2048, indices each start at 0 (so 0..99) What is the address oft[20,15]? First: what is start of row 20? A [20,0] = B +(I L) S = 2048+(20 0) (4 100) = 10,048 Next, find offset of element in row 20 same kind of calculation: A [20,15] = 10,048+(I L) S = 10,048+(15 0) 4 = 10,108 Copyright c 2002 2017 UMaine School of Computing and Information S 27 / 29

Heterogenous types: Records, structs contain heterogeneous related data E.g., data about employee: name (string), address (string), salary (fixed-point integer), height (float),... Most languages support this type: struct in C,defstruct in Lisp,record in Pascal, etc. Design issues: How are fields selected? How is field checked? Can we assign one structure to another? How to implement e.g., how to find element from selector? (field must have type, offset) Copyright c 2002 2017 UMaine School of Computing and Information S 28 / 29

Pointers These point to an object: contain its address Used for user-created dynamic variables (from heap) Used for indirect addressing (e.g., C) abstraction of assembly language s indirect addressing mode Can assign to and through them: int a = 3; // integer int* p; // pointer to integer p = &a; // p = a s address *p = 4; // a now = 4 Often used (in C, e.g.) to access array elements int a[100]; // 100-element array of ints int* p = a; // p = addr of a s start (no &) for (int i=0;i<100;i++) { *p = 0; p++; } Copyright c 2002 2017 UMaine School of Computing and Information S 29 / 29