Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.

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

Programming Languages and Paradigms
Names and Bindings.
Chapter 5 Names, Bindings, and Scopes
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
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.
ISBN Lecture 05 Variable Attributes.
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
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
Software II: Principles of Programming Languages Lecture 5 – Names, Bindings, and Scopes.
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.
Names, Bindings, Type Checking, and Scopes
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
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 Chapter 5 Names Bindings Type Checking Scope. 2 High-Level Programming Languages Two main goals:Two main goals: –Machine independence –Ease of programming.
1 CS Programming Languages Class 07 September 14, 2000.
Names Variables Type Checking Strong Typing Type Compatibility 1.
Names, Bindings, Type Checking, and Scopes
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)
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.
Basic Semantics Associating meaning with language entities.
ISBN Chapter 5 Names, Bindings, and Scopes.
Programming Languages and Paradigms Imperative Programming.
CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann.
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
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.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
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.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Data Types In Text: Chapter 6.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
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, Bindings, Type Checking, and Scopes
Names and Binding In Text: Chapter 5.
Names, Bindings, and Scopes
Types and Related Issues
Presentation transcript:

Variables reference, coding, visibility

Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special words: keywords or reserved  predefined names  association with data and processes What features influence readability and writability?

Variables – data representation  high level version of memory reference  more general and useful model of memory  six attributes: name and address, type and value, scope and lifetime What features influence readability and writability? name address type value scope lifetime

Attributes of variables nameaddress typevalue lifetime scope name address type value scope lifetime

D Goforth COSC Address - memory location  Actual location of data, low level  name-address relations may be one to one one to many: e.g., same name used for different variables in different methods many to one (alias): e.g.: formal parameter in method call refers to same object as actual parameter name address type value scope lifetime

D Goforth COSC Type: code and range  Simple (primitive) types: bit code for storage  Composite types: subtypes and organization Type determines:  range of possible values  operations on values name address type value scope lifetime

D Goforth COSC Values  Actual sequence of bits as interpreted by codes and organization  Abstract memory cell - memory space for a value without considering inside structure e.g., double ‘cell’ is 8 bytes in java e.g., object manipulation name address type value scope lifetime

D Goforth COSC Values  Implementation - inside view  Application - abstract view some languages allow inside view of simple data types e.g.: C has bit level operations name address type value scope lifetime

D Goforth COSC Binding - setting attributes of entities  E.g. a variable (entity) has six attributes that must be bound to it. x = 6.0; // binds value to variable with name x  All entities of a programming language have attributes: data types, operators, keywords, programs, etc. name address type value scope lifetime

D Goforth COSC Binding Time  Time when binding of attributes occurs is a major distinguishing feature of languages Binding Time Line: Language Design Language Implementation Program Coding Program Compile Program Execution name address type value scope lifetime

D Goforth COSC Binding to variables  Static - before execution  Dynamic - (before and) during execution java e.g.: value int mVal = 0; final double yearLength = ; mVal = 1000;  What about other attributes? name address type value scope lifetime

D Goforth COSC Binding type to a variable when how ExplicitImplicit Static Dynamic X name address type value scope lifetime

D Goforth COSC User-defined types Simple types  enumerated e.g. in pascal: type suittype = (spade, heart, diamond, club); var trump suittype; trump := diamond;  subrange e.g. in Ada: subtype DAY_OF_MONTH is INTEGER range 1..31; START_DATE: DAY_OF_MONTH ; name address type value scope lifetime

D Goforth COSC User-defined types Compound types  anonymous types  named types e.g. in Ada: type YEAR_LIST is array (1..366) of INTEGER; A, B: YEAR_LIST; C: array (1..366) of INTEGER; D: array (1..366) of INTEGER; name address type value scope lifetime

D Goforth COSC User-defined types in java Compound types  anonymous types  named types e.g. in Ada: type YEAR_LIST is array (1..366) of INTEGER; A, B: YEAR_LIST; C: array (1..366) of INTEGER; D: array (1..366) of INTEGER; name address type value scope lifetime

Dynamic typing  variable gets type from value assigned to it  type can change during execution  expensive in performance – tracking type  e.g. APL, LISP name address type value scope lifetime

Storage (address) binding  static allocation: variable is associated with one address for entire program – efficient but inflexible (e.g., no recursion, waste space)  e.g., java class variables  dynamic allocation: memory allocated when declaration executed

Stack-dynamic memory  allocations for a called procedure are ‘stacked’ on allocations for calling procedure  lifetime: proc execution allocation for main:x,y,z allocation for a: w,t,x allocation for b: x,e,d procedure main { int x=0,y=1,z=2 a(x) } procedure a(int w) { int t=3,x=4 b(t) } procedure b(int d) { int e,x=5 e = d + x + t + y }

Heap-dynamic storage  explicit: heap of memory is allocated on specific request: pointers (c) and references (java) lifetime: till deallocation(c) or gc(java)  implicit: with dynamic typing lifetime: till variable retyped

Type checking-compatibility of types in an operation e.g.: assignment, expressions, procedure calls: Type error: expected type and actual type don’t match Coercion: legitimate change of actual variable to expected type e.g.: 4 * 5.6double y = 100; String s = “max: ” + y;int x = 3.2;

Strong typing  all type errors are detected - reliability Question: java is strongly typed: can type checking be done at compile time??

Strong typing Question: java is strongly typed: can type checking be done at compile time?? NO: some variables can contain different types – must check at run time e.g. Object obj; // can reference object from any class

Type compatibility For user defined simple types: type date = 1..31; var birthday: date; count: integer;... count := birthday; Pascal: type error - name type compatibility (OK with structure type compatibility) C?

Scope  Range of accessibility of variable  scope in object-oriented languages is distinct and sometimes tricky (chapter 12)  scope in imperative (procedural) languages depends mostly on procedures

Scope in imperative languages  Static scoping - visibility depends on structure of source code file determined at compile time  Dynamic scoping - visibility depends on procedure calls determined at execution time

Scope definitions – imperative languages  block – bounded part of a program – e.g., procedure  scope – range of visibility of an identifier, e.g., variable name  local scope – block where identifier is delcared  non-local scope – other blocks where identifer is visible

Typical imperative program program A type f,g,h var x,y,z procedure B var v,x begin... end begin... end A B C D E F

Static Scoping  scopes nested according to file structure – determined at compile  what identifiers are visible from a scope?  which variable for multiply-defined identifier is visible?

Static example: pascal  sample of code sample of code  visible variables at each block  visible procedures at each block  partial scope and forward reference

Static scope impedes information hiding  unintended access to variables and procedures is hard to prevent – nesting scopes partial solution: flat procedure hierarchy (c)

Dynamic scope – stack dynamic variables  Non-local scopes based on procedure calls  procedure may have different scopes each time it is executed

Dynamic example: “pseudopascal”  sample of code sample of code  visible variables at each block  visible procedures at each block  partial scope and forward reference

Dynamic scope  all local variables visible during execution  difficult to read programs – must know calling order partial solution: use static scoping and get ‘dynamic’ power by parameter passing