Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 9 Lecturer: Dr. Emad Nabil 1-1.

Slides:



Advertisements
Similar presentations
Names, Bindings, Type Checking, and Scopes
Advertisements

Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case.
Names and Bindings.
Chapter 5 Names, Bindings, and Scopes
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, Type Checking, and Scopes
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.
Names, Bindings, Type Checking, and Scopes
ISBN Lecture 05 Variable Attributes.
The Concept of Variables
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
Scope.
Names, Bindings, and Scopes
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.
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?
1 CS Programming Languages Class 07 September 14, 2000.
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.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Topics Scope Scope and Lifetime Referencing Environments.
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.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
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.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Variables, Names, Scope and Lifetime ICOM 4036 Lecture 9.
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.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
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
Names, Bindings, Type Checking, and Scopes
Advanced Programming in C
Type Checking, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, and Scopes
Names, Bindings, and Scopes
Names.
Concepts of programming languages Names, Bindings, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Abstract Data Types and Encapsulation Concepts
Names, Bindings, Type Checking, and Scopes
Scope, Visibility, and Lifetime
Names, Bindings, Type Checking, and Scopes
Names, Bindings, and Scopes
Names, Bindings, Type Checking, and scopes
Types and Related Issues
Identifiers & Lifetimes
Presentation transcript:

Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 9 Lecturer: Dr. Emad Nabil 1-1

x = 0 def outer(): x = 1 def inner(): x = 2 print("inner:", x) inner() print("outer:", x) outer() print("global:", x) # inner: 2 # outer: 1 # global: 0

x = 0 def outer(): x = 1 def inner(): nonlocal x x = 2 print("inner:", x) inner() print("outer:", x) outer() print("global:", x) # inner: 2 # outer: 2 # global: 0

x = 0 def outer(): x = 1 def inner(): global x x = 2 print("inner:", x) inner() print("outer:", x) outer() print("global:", x) # inner: 2 # outer: 1 # global: 2

void sub1() { int a, b;... 1 } /* end of sub1 */ void sub2() { int b, c; sub1(); } /* end of sub2 */ void main() { int c, d;... 3 sub2(); } /* end of main */ Point Referencing Environment 1) a and b of sub1, c of sub2, d of main, ( c of main and b of sub2 are hidden) 2) b and c of sub2, d of main, ( c of main is hidden) 3) c and d of main Point Referencing Environment 1) a and b of sub1, c of sub2, d of main, ( c of main and b of sub2 are hidden) 2) b and c of sub2, d of main, ( c of main is hidden) 3) c and d of main Assume dynamic scope

Copyright © 2012 Addison-Wesley. All rights reserved. 1-7 Chapter 5 Topics IntroductionNamesVariables The Concept of Binding Scope Scope and Lifetime Referencing Environments Named Constants

Copyright © 2012 Addison-Wesley. All rights reserved. 1-8 Named Constants A named constant is a variable that is bound to a value only when it is bound to storage Advantages: readability and modifiability The binding of values to named constants can be either static (called manifest constants) or dynamic

Copyright © 2012 Addison-Wesley. All rights reserved. 1-9 Named Constants –Ada, C++, and Java: expressions of any kind, dynamically bound binding of values Dynamic bindingStatic binding Value bound at run timeValue bound at compile time Ada, C++, and Java, C#(readonly ) C#(const)

Named constants in java

100 is written only one time and can’t be changed Named constants in java Final variables must be intlized in the same line of declaration

int y; cin >> y; const int x = 2 * y; x++; cout << x; Syntax Error OK: Value Dynamically bound C++ example

const int x; X=5; cout << x; Error, need initialization C++ example

In C# language const int x; x = 3; Error int a=4, b=5; const int y=a+b; Error const int y=5 OK Must be initialized when declared

class Program { public readonly int x =5; public readonly int y; Program() { int a = int.Parse(Console.ReadLine()); y = a + 1; y++; } public void f() { int a = int.Parse(Console.ReadLine()); y = a + 1; y++ } static void Main(string[] args) { } ok error OK C# readonly variables can be changed only inside constructor error

Copyright © 2012 Addison-Wesley. All rights reserved Summary Case sensitivity and the relationship of names to special words represent design issues of names Variables are characterized by the sextuples: name, address, value, type, lifetime, scope Binding is the association of attributes with program entities Scalar variables are categorized as: static, stack dynamic, explicit heap dynamic, implicit heap dynamic Strong typing means detecting all type errors