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.

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

Types II. Hwansoo Han

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

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

TYPES, VALUES AND DECLARATIONS

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

CS 3360 Design and Implementation of Programming Languages. Exam 1

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

Chapter 5. Names, Bindings, and Scopes

Chapter 5 Names, Binding, Type Checking and Scopes

MIDTERM EXAMINATION - CS130 - Spring 2003

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

Lecture 12: Data Types (and Some Leftover ML)

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

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

CSCI312 Principles of Programming Languages!

CS558 Programming Languages

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

CS 314 Principles of Programming Languages

Programming Languages, Summary CSC419; Odelia Schwartz

Chapter 8 :: Composite Types

CS558 Programming Languages

Array. Prepared By - Rifat Shahriyar

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

Programming Methodology

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

Names, Bindings, Scopes

Language Reference Manual simplicity

Lecture 13: Complex Types and Garbage Collection

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

Programmiersprachen (Programming Languages)

Program Fundamentals

COS 140: Foundations of Computer Science

CSC 533: Organization of Programming Languages. Spring 2005

G Programming Languages - Fall 2012

Programming Languages

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

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

Values and Variables 1 / 30

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

Type Bindings. Static Type Binding

CS 3360 Design and Implementation of Programming Languages. Exam 1

Week 3 Lecture 2. Types Constants and Variables

Concepts of Programming Languages

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

Programming Languages: Lecture 11

Lecture Notes on Programming Languages

MIDTERM EXAMINATION - CS130 - Spring 2005

CS558 Programming Languages. Winter 2013 Lecture 3

Final-Term Papers Solved MCQS with Reference

BM214E Object Oriented Programming Lecture 4

CSE 307: Principles of Programming Languages

Chapter 6. Data Types ISBN

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

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Derived and abstract data types. TDT4205 Lecture 15

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

a data type is Types

How to declare an array in C?

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

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

Full file at

LECTURE 18. Control Flow

HANDLING NONLOCAL REFERENCES

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Programming Lecture 3

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

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

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

VARIABLES AND TYPES CITS1001

The Warhol Language Reference Manual

Topic 9: Type Checking

Topic 9: Type Checking

Chapter 6. Data Types ISBN

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

Continuations provide a novel way to suspend and reexecute

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

Example: Haskell algebraic data types (1)

6. Names, Scopes, and Bindings

CS112 Lecture: Primitive Types, Operators, Strings

COMP2121: Microprocessors and Interfacing. Number Systems

Zheng-Liang Lu Java Programming 45 / 79

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

Transcription:

COS 140: Foundations of Computer Science Variables and Primitive Data Types Fall 2017 Introduction 3 What is a variable?......................................................... 3 Variable attributes.......................................................... 4 Binding................................................................. 5 Variable names 6 Variable names............................................................ 6 Name length.............................................................. 9 Special names............................................................. 10 Assignment 11 Data types 13 Primitive data types 14 Numeric types............................................................ 15 Booleans................................................................ 17 Characters............................................................... 18 Arrays.................................................................. 20 Records................................................................. 28 Pointers................................................................. 29 1

Homework Reading: Chapter 16 Homework: Exercises at end of chapter Homework due 10/23 Copyright c 2002 2017 UMaine Computer Science Department 2 / 29 Introduction 3 / 29 What is a variable? 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 Computer Science Department 3 / 29 2

Variable attributes 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 Computer Science Department 4 / 29 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 Computer Science Department 5 / 29 3

Variable names 6 / 29 Variable 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 Computer Science Department 6 / 29 Variable 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 Computer Science Department 7 / 29 4

Variable 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 Computer Science Department 8 / 29 Length of variable 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 Computer Science Department 9 / 29 5

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 Computer Science Department 10 / 29 Assignment 11 / 29 Giving variables values Assignment construct binds a value to a variable Often, a construct such as: x = a+b x s value is bound to result of a+b Not algebra: can have: x = x+1 Copyright c 2002 2017 UMaine Computer Science Department 11 / 29 6

Assignment construct examples C, Java, Python... x = x + 1; Pascal x := x + 1 Lisp (setf x (1+ x)) R x <<- x + 1 or x + 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 Computer Science Department 12 / 29 Data types 13 / 29 Data types 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 Computer Science Department 13 / 29 7

Primitive data types 14 / 29 What are primitive data types? 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 Computer Science Department 14 / 29 Integers Integer type represents subset of the integers Z (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 computer instead i...j, where i 0 j For two s complement, with n bits, can represent 2 n 1...2 n 1 1 E.g., 8 bits can represent 10000000 2 (= 128 10 ) to 01111111 2 (= 127 10 ). Some languages: unsigned integers (C) Copyright c 2002 2017 UMaine Computer Science Department 15 / 29 8

Floating point numbers Represent subset of real numbers R Usually two sizes Two parts: fraction and exponent E.g.: 0.1101 2, exponent 101 2 Fractional part = 1 2 + 1 4 + 0 8 + 1 16 = 0.8125 10 Exponent = 5, so multiply fraction by 2 5 So number is 0.8125 32 = 26.0 10 Can t represent all numbers accurately e.g., π, 1 3 Copyright c 2002 2017 UMaine Computer Science Department 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 Computer Science Department 17 / 29 9

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 Computer Science Department 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 Computer Science Department 19 / 29 10

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 Computer Science Department 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 Computer Science Department 21 / 29 11

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 Computer Science Department 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 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 element Taxes[15] 1024 1028 1032... Array "Taxes" Taxes[0] Taxes[1] Taxes[15] A = 1024+(15 0) 8 = 1144 Copyright c 2002 2017 UMaine Computer Science Department 23 / 29 12

Another example Suppose array Foo 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 of Foo[201]? Answer: A = B +(I L) S = 5242 = 4096+(201 10) 6 Copyright c 2002 2017 UMaine Computer Science Department 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 Computer Science Department 25 / 29 13

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 Computer Science Department 26 / 29 Address calculation Array T is 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 for T = 2048, indices each start at 0 (so 0..99) What is the address of T[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 Computer Science Department 27 / 29 14

Heterogenous types: Records, structs Records 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 Computer Science Department 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 Computer Science Department 29 / 29 15