Types and Values.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Functional Design and Programming Lecture 1: Functional modeling, design and programming.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
TES3111 October 2001 Artificial Intelligence LISP.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
JavaScript, Third Edition
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
Introduction to C Programming
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.
1 Structured Programming in C Welcome to CPSC 206.
Basic Elements of C++ Chapter 2.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
A First Book of ANSI C Fourth Edition
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
CPS120: Introduction to Computer Science
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
Ajmer Singh PGT(IP) Programming Fundamentals. Ajmer Singh PGT(IP) Java Character Set Character set is a set of valid characters that a language can recognize.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
FG Group -Afrilia BP -Liana F.B.I -Maulidatun Nisa -Riza Amini F.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Definition of the Programming Language CPRL
Chapter Topics The Basics of a C++ Program Data Types
A Simple Syntax-Directed Translator
BASIC ELEMENTS OF A COMPUTER PROGRAM
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Overview of Compilation The Compiler Front End
Overview of Compilation The Compiler Front End
Basic Elements of C++.
Multiple variables can be created in one declaration
Introduction to C++.
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Chapter 10 Programming Fundamentals with JavaScript
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Review: Compiler Phases:
An aggregation mechanism
CS111 Computer Programming
C Operators, Operands, Expressions & Statements
Chapter 2: Java Fundamentals
Declarative Computation Model Single assignment store (VRH 2
The Data Element.
Comments Any string of symbols placed between the delimiters /* and */. Can span multiple lines Can’t be nested! Be careful. /* /* /* Hi */ is an example.
The Data Element.
Lexical Elements & Operators
Chapter 2, Part III Arithmetic Operators and Decision Making
Python fundamental.
Faculty of Computer Science and Information System
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Types and Values

a set of elements having common computational characteristics type –

Types int string Various types corresponding to tuples

Integer Values and Arithmetic Operations Note that in SML unary minus is denoted ~ arithmetic operations = { +, -, *, div, mod }

Integer Expressions and their Evaluation 5 - this expression cannot be simplified any further and is therefore said to be in normal form 5 + 6  11 5 + 6 + 7  11 + 7  18 5 + 6 * 7  5 + 42  47 (5 + 6) * 7  11 * 7  77 8 * 2 div 4  16 div 4  4 5 – 7  ~2 The arrow symbol above denotes a computational step which is also referred to as a reduction. We say “5 + 6 reduces to 11”.

Arithmetic Operations Operator Expression Normal Form + 5 + 4 9 - 4 - 5 ~1 * 4 * 5 20 div 17 div 5 3 mod 17 mod 5 2

Expressions and Values and Types – Oh my! Result 5 + 4 9 : int 4 - 5 ~1 : int 4 * 5 20 : int 17 div 5 3 : int 17 mod 5 2 : int

SML Interpreter

String values and Operations string values = { “”, “a”, …, some large string } string operation = { ^ }

String Expressions and their Evaluation “hello” ^ “ world”  “hello world” “see” ^ “ spot” ^ “ run”  “see spot” ^ “ run”  “see spot run”

String Operations Operator Expression Normal Form ^ “hello” ^ “ world”

Expressions and values and types Result “hello” ^ “ world” “hello world” : string

Tuple Expressions and their Evaluation a tuple value is a comma-separated sequence of two or more values enclosed in parenthesis. a tuple is a comma-separated sequence of two or more expressions enclosed in parenthesis. a tuple is reduced to a tuple value by reducing its expressions from left to right (e.g., the left most expression is reduce first, and so on). An expression e enclosed in parenthesis reduces to e. (e)  e An expression consisting of parenthesis that enclose nothing denotes a value that is called unit.

 (“hi world”, 2 * 3)  (“hi world”, 6) Evaluation of Tuples Expression Reduction Sequence Normal Form (5 + 4, 1 + 2)  (9, 1 + 2)  (9,3) (9,3) (“hi” ^ “ world”, 2 * 3)  (“hi world”, 2 * 3)  (“hi world”, 6) (“hi world”, 6) (4,5) (4, 2 + 3)  (4,5) (1+1, 3, 5 + 3)  (2, 3, 5 + 3)  (2, 3, 8) (2,3,8)

Tuple Types Special Cases Expression Normal Form Type (5 + 4, 1 + 2) (9,3) int * int (“hi” ^ “ world”, 2 * 3) (“hi world”, 6) string * int (4,5) (1+1, 3, 5 + 3) (2,3,8) int * int * int Remark: In the context of types, the * symbol denotes the Cartesian product. Special Cases Expression Normal Form Type () unit (2 * 3) (6) int

(12, (2, 5), “hello”) : int * (int * int) * string Nested Tuples A tuple is a kind of expression. A tuple consists of a comma separated sequence of expressions. Therefore, a tuple can contain a tuple – which can contain another tuple and so on! (6 * 2, (1+1, 5), “hello”)  (12, (1+1, 5), “hello”)  (12, (2, 5), “hello”) (12, (2, 5), “hello”) : int * (int * int) * string

SML Interpreter

Variables, Names, and Identifiers

Variables In SML, a variable is a language construct which can be associated with (i.e., bound to) a value and a type.  A variable is bound to the result of a computation (i.e., its normal form). In books on programming languages, the terms variable, name, and identifier (id) are often used loosely in an interchangeable manner. Conceptually, a variable represents a place where values (and types) can be stored. An identifier is how the value in a place can be referenced within a program.

Variables Variable Name/Id Value Type x 5 int y “hello” string

Function Names In Bricklayer Level 2, we are interested in declaring functions. Part of a function declaration involves giving a name to the function. The understanding of a function can be significantly improved by naming it appropriately. In order to do this, it helps to have an understanding of the rules constraining the choice of names (aka identifiers).

Identifiers We will define an identifier as a sequence of characters beginning with an alphabetic character (e.g., a-z or A-Z) followed by zero or more alphanumeric characters (e.g., a-z, A-Z, or 0-9) or underscores. Remark: this definition of identifier is a subset of the set of legal SML identifiers.

Tokens and Spaces

The Structure of a Book A sequence of characters (including blank characters). A sequence of words and punctuation symbols. A sequence of sentences. A sequence of paragraphs. A sequence of chapters. Note that a chapter consists of a sequence of paragraphs, and a paragraph consists of a sequence of sentences.

SML programs also have a Structure An SML program consists of a sequence of characters. An SML program contains values, identifiers, and reserved symbols. An SML program contains expressions (which define computations to be performed). A Bricklayer function call is a kind of expression. An SML program contains declarations. open Level_1 is an example of a declaration. For now, we consider a Bricklayer program to consist of a sequence of declarations followed by a sequence of expressions.

Lexical Analysis A sequence of characters can be converted into a sequence of tokens through a process called lexical analysis. Tokens correspond to values, identifiers, and reserved symbols. During lexical analysis white space is discarded.

put2D_4x2_BLUE(10, 1 + 1); Token Classification Comment put2D_2x4_BLUE Identifier Bricklayer function name ( Reserved symbol Delimiter - left parenthesis 10 Integer value , Delimiter - comma 1 + primitive arithmetic operation ) Delimiter – right parenthesis ; Sequential composition – semi-colon

Type Systems

Type Systems In a strongly typed programming language, a type system classifies expressions according to their type (e.g., int, string). Most (but not all) of this analysis can be done prior to executing a program (i.e., at runtime). Analysis that is done prior to program execution is called static analysis. Analysis (or checks) that are performed during runtime is called dynamic analysis.

int 8 div 4 “hi” ^ “world” string 5+4 7 “hello”

undefined 8 div 0 “hello” ^ 7 “hi” * 5 In this context, an undefined computation is also called an error.

error 8 div 0 ? 8 div x int

Limits of Static Analysis In general, determining the value of an expression lies beyond the limits of static analysis. Values are determined during program execution (i.e., at runtime). To be complete, some checks in a type system must be performed at runtime. This slows down program execution.