How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.

Slides:



Advertisements
Similar presentations
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case.
Advertisements

Types in Ruby and other languages….  Classes and objects (vs prototypes)  Instance variables/encapsulation  Object creation  Object equality/comparison.
Names and Bindings.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 5 Names, Bindings, and Scopes
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
ISBN Chapter 7 Expressions and Assignment Statements.
And other languages….  The Ruby Programming Language, Flanagan & Matsumoto (creator of Ruby)
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
CS 330 Programming Languages 10 / 18 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
Names, Bindings, and Scopes
Software II: Principles of Programming Languages Lecture 5 – Names, Bindings, and Scopes.
ISBN Chapter 7 Expressions and Assignment Statements.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 5 © 2002 by Addison Wesley Longman, Inc Names - We discuss all user-defined names here - Design issues for names: - Maximum length? - Are.
COMP4730/2003/lec5/H.Melikian Names, Bindings,Type Checking and Scopes (Chapter 5) - Design issues: - Maximum length? - Are connector characters allowed?
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 CS Programming Languages Class 07 September 14, 2000.
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Programming Language C++ Xulong Peng CSC415 Programming Languages.
Chapter 5 Names, Bindings, and Scopes. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 5 Topics Introduction Names Variables The Concept.
ISBN Chapter 5 Names, Bindings, and Scopes.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
ISBN Chapter 5 Names, Bindings, and Scopes.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Arithmetic Expressions
Expressions and Assignment Statements
CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann.
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names and Binding In Text: Chapter 4.
ISBN Variables, Names, Scope and Lifetime ICOM 4036 Lecture 9.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Scope, and Bindings Programming Languages and Paradigms.
Names, Bindings, Type Checking and Scopes. Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence.
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Object Lifetime and Pointers
What do you need to know about a new language?
Expressions and Assignment Statements
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Chapter 6: Data Types Lectures # 10.
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, Type Checking, and Scopes
Names and Binding In Text: Chapter 5.
Names, Bindings, and Scopes
PRESENTED BY ADNAN M. UZAIR NOMAN
Presentation transcript:

How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references – type conversions and equality Expressions – Operators, overloading, booleans, short-circuiting, conditional expression – Referential transparency – Statements vs Expressions Control flow – conditionals – loops Functions Classes Exception handling Other features Threads Reflection Libraries Functional Language – other aspects, covered later QUICK EX: With a partner how do you learn a new programming language? What types of programs do you write?

 A binding is an association, such as: bind type of variable bind operation to symbol (e.g., meaning of *) bind function to its definition  Binding time is the time at which a binding takes place.  Type binding may be static or dynamic explicit or implicit

 Language design time -- bind operator symbols to operations : sum = sum + count  Language implementation time-- bind type to a representation : int => number of bits, etc.  Compile time -- bind a variable to a type: int count;  Link time – bind library subprogram to code: cout << x;  Load time -- bind a FORTRAN 77 variable to a memory cell (or a C static variable)  Runtime -- bind a nonstatic local variable to a memory cell

 A binding is static if it first occurs before run time and remains unchanged throughout program execution.  A binding is dynamic if it first occurs during execution or can change during execution of the program  NOTE: doesn't consider paging etc. which is at the hardware level

This distinction may apply to:  variable typing  variable lifetime  variable scope  polymorphism overloaded operators vs late binding Static is also used to identify class vs instance variables – not really a static vs dynamic example

 Type not specified by declaration, not determined by name (JavaScript, PHP, Ruby)  Specified through an assignment statement list = [2, 4.33, 6, 8]; list = 17.3; Advantage: flexibility (generic program units) Disadvantages:  High cost (dynamic type checking requires run-time descriptors, normally interpreted… upcoming discussion)  Type error detection by the compiler is difficult How are generic program units done in C++? Java?

 How would dynamic types be implemented? What data structure(s) would you use? How does this impact your code – consider efficiency, reliability.  Now think about challenges with + total = message = “hello” + “ world” something = “count “ other = 3 + “count”

i = x; // desired, x is scalar i = y; // typed accidentally, y is array

 Definitions not precise  In general, a strongly typed language will generate a compiler error if the value used (e.g., passed to a function, assigned to a variable) does not match the expected type  May also be considered strongly typed if type errors are prevented at runtime due to dynamic typing (so type safety is more important for reliability)  A language may be considered weakly typed if it includes features that allow types to be used interchangeably

 Implicit type conversions A language with more implicit conversions is considered less strongly typed C supports more implicit conversions than Java, for example  Pointers*  Untagged unions* * covered later

 Widening Conversions: can include at least approximations to all of the values of the original type. Examples (Java/C++) byte to short, int, long, float or double short to int, long, float or double char to int, long, float or double int to long, float or double long to float or double float to double  Narrowing conversions: cannot include all of the values of the original type short to byte or char char to byte or short int to byte, short, or char long to byte, short, char or int float to byte, short, char, int or long double to byte, short, char, int, long or float

 Even widening conversions may lose accuracy. Example: integers stored in 32 bits, 9 digits of precision. Floating point values also stored in 32 bits, only about 7 digits of precision (because of space used for exponent). Conversions should be used with care! Warnings should not just be ignored… Strongly typed language minimizes type conversions

 Type safety is the extent to which a programming language discourages or prevents type errors  A type error is erroneous or undesirable program behavior caused by a discrepancy between differing data types (e.g., trying to perform an operation that is not valid for that type)  Type enforcement can be static (compile time) or dynamic (run-time)

 Explicit – stated by programmer  Implicit – determined by language  Can be applied to: type declaration variable lifetime

 An explicit declaration is a program statement used for declaring the types of variables: int count;  An implicit declaration is a default mechanism for specifying types of variables (the first appearance of the variable in the program)  Both create static bindings to types (i.e., type doesn’t change during execution of program)  FORTRAN, PL/I, BASIC, and Perl provide implicit declarations Advantage: writability Disadvantage: reliability  is array, % is hash, $ is scalar  Fortran: I-N integer, others single precision, can override

 many words have special meaning (e.g. if, true, def, etc.)  Keyword: has special meaning in particular context, but can be used as variable name Algol, PL/I, Fortran  Reserved: can’t be used as variable COBOL has ~400, Java has ~50 Ruby has reserved words (evolving, no fixed number) Advantage: may avoid confusion Disadvantage: may need to be aware of language parts you aren’t even using Compare to Java/C++

 In Fortran, this is potentially valid: if if then then else else  In Java, goto is a reserved word (you can’t use) but not a keyword (language doesn’t use)  Functions in libraries are not keywords OR reserved words

function bark() {}; bark.prototype.alert = "woof"; bark.prototype.alertOwner = function(){return this.alert}; var myDog = new bark(); alert(myDog.alert); // displays dialog saying woof

 Read this:   Why are goto statements harmful??