PL Names Binding and Scopes

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/80

encourage image

There's no tags or description

Looks like no tags are added yet.

81 Terms

1
New cards

What is a variable?

An abstraction of a (computer) memory cell; characterized by six attributes: name, address, value, type, lifetime, scope

2
New cards

What are the six attributes (sextuple) of a variable?

Name, address, value, type, lifetime, scope

3
New cards

What are the two design issues for names?

Are names case sensitive? Are special words reserved words or keywords?

4
New cards

Name length limits in C99

No limit, but only the first 63 characters are significant; external names limited to a maximum of 31

5
New cards

Name length in C# and Java

No limit, and all characters are significant

6
New cards

Name length in C++

No limit, but implementers often impose one

7
New cards

Special characters in names: PHP

All variable names must begin with dollar signs

8
New cards

Special characters in names: Perl

All variable names begin with special characters, which specify the variable's type

9
New cards

Special characters in names: Ruby

Names beginning with @ are instance variables; those beginning with @@ are class variables

10
New cards

Disadvantage of case sensitivity in names

Readability: names that look alike are actually different; worse in C++, Java and C# because predefined names are mixed case (e.g., IndexOutOfBoundsException)

11
New cards

Which languages have case-sensitive names?

The C-based languages (C, C++, Java, C#)

12
New cards

Keyword (definition)

A word that is special only in certain contexts

13
New cards

Reserved word (definition)

A special word that cannot be used as a user-defined name

14
New cards

Problem with too many reserved words

Many collisions occur with user-defined names (e.g., COBOL has 300 reserved words)

15
New cards

What are aliases?

Two variable names that can be used to access the same memory location; created via pointers, reference variables, and C and C++ unions; harmful to readability

16
New cards

Address (variable attribute)

The memory address with which the variable is associated; a variable may have different addresses at different times or in different places

17
New cards

Type (variable attribute)

Determines the range of values of variables and the set of operations defined for values of that type; for floating point it also determines precision

18
New cards

Value (variable attribute)

The contents of the location with which the variable is associated

19
New cards

l-value of a variable

Its address

20
New cards

r-value of a variable

Its value

21
New cards

Abstract memory cell

The physical cell or collection of cells associated with a variable

22
New cards

Binding (definition)

An association between an entity and an attribute, such as between a variable and its type or value, or between an operation and a symbol

23
New cards

Binding time

The time at which a binding takes place

24
New cards

Static binding

A binding that first occurs before run time and remains unchanged throughout program execution

25
New cards

Dynamic binding

A binding that first occurs during execution or can change during execution of the program

26
New cards

Language design time binding

Binding operator symbols to operations

27
New cards

Language implementation time binding

Binding a floating point type to a representation

28
New cards

Compile time binding

Binding a variable to a type in C or Java

29
New cards

Load time binding

Binding a C or C++ static variable to a memory cell

30
New cards

Runtime binding

Binding a nonstatic local variable to a memory cell

31
New cards

Two key questions of type binding

How is a type specified, and when does the binding take place? If static, the type may be specified by an explicit or implicit declaration

32
New cards

Explicit declaration

A program statement used for declaring the types of variables

33
New cards

Implicit declaration

A default mechanism for specifying types of variables through default conventions, rather than declaration statements

34
New cards

Languages that provide implicit declarations

Basic, Perl, Ruby, JavaScript, and PHP

35
New cards

Advantage and disadvantage of implicit declarations

Advantage: writability (a minor convenience); Disadvantage: reliability (less trouble with Perl)

36
New cards

Type inferencing

Determining types of variables from context, e.g., C# var with an initial value sets the type; used by Visual Basic 9.0+, ML, Haskell, and F#

37
New cards

Languages with dynamic type binding

JavaScript, Python, Ruby, PHP, and C# (limited)

38
New cards

How is type specified in dynamic type binding?

Through an assignment statement, e.g., JavaScript: list = [2, 4.33, 6, 8]; list = 17.3;

39
New cards

Advantage of dynamic type binding

Flexibility (generic program units)

40
New cards

Disadvantages of dynamic type binding

High cost (dynamic type checking and interpretation) and type error detection by the compiler is difficult

41
New cards

Allocation and deallocation (storage bindings)

Allocation: getting a cell from a pool of available cells; deallocation: putting a cell back into the pool

42
New cards

Lifetime of a variable

The time during which it is bound to a particular memory cell

43
New cards

Static variables (lifetime category)

Bound to memory cells before execution begins and remain bound to the same cell throughout execution, e.g., C and C++ static variables in functions; Advantage: efficiency (direct addressing) and history-sensitive subprogram support; Disadvantage: lack of flexibility (no recursion)

44
New cards

Stack-dynamic variables (lifetime category)

Storage bindings are created when their declaration statements are elaborated; if scalar, all attributes except address are statically bound, e.g., local variables in C subprograms (not declared static) and Java methods; Advantage: allows recursion and conserves storage; Disadvantages: allocation/deallocation overhead, subprograms cannot be history sensitive, inefficient references (indirect addressing)

45
New cards

Elaboration (definition)

The process by which a declaration's executable code is executed; stack-dynamic storage bindings are created when declarations are elaborated

46
New cards

Explicit heap-dynamic variables (lifetime category)

Allocated and deallocated by explicit programmer directives during execution; referenced only through pointers or references, e.g., dynamic objects in C++ (new and delete), all objects in Java; Advantage: provides dynamic storage management; Disadvantage: inefficient and unreliable

47
New cards

Implicit heap-dynamic variables (lifetime category)

Allocation and deallocation caused by assignment statements, e.g., all variables in APL; all strings and arrays in Perl, JavaScript, and PHP; Advantage: flexibility (generic code); Disadvantages: inefficient because all attributes are dynamic, and loss of error detection

48
New cards

Scope of a variable

The range of statements over which it is visible

49
New cards

Local variables of a program unit

Variables that are declared in that unit

50
New cards

Nonlocal variables of a program unit

Variables that are visible in the unit but not declared there

51
New cards

Global variables

A special category of nonlocal variables

52
New cards

Scope rules of a language

Determine how references to names are associated with variables

53
New cards

Static scope

Scope based on program text (spatial); to connect a name reference to a variable, find its declaration by searching first locally, then in increasingly larger enclosing scopes

54
New cards

Static ancestors and static parent

Enclosing static scopes of a specific scope are its static ancestors; the nearest static ancestor is its static parent

55
New cards

Languages allowing nested subprogram definitions (nested static scopes)

Ada, JavaScript, Common Lisp, Scheme, Fortran 2003+, F#, and Python

56
New cards

How can a variable be hidden?

By having a 'closer' variable with the same name

57
New cards

Blocks

A method of creating static scopes inside program units, from ALGOL 60; legal in C and C++, but not in Java and C# (too error-prone)

58
New cards

The let construct (functional languages)

A construct with two parts: the first binds names to values, the second uses the names defined in the first part

59
New cards

let in Scheme

(LET ((name1 expression1) … (namen expressionn)) body)

60
New cards

let in ML

let val name1 = expression1 … val namen = expressionn in expression end;

61
New cards

let in F#

First part: let left_side = expression (left_side is a name or a tuple pattern); all that follows is the second part

62
New cards

Declaration order: C99, C++, Java

Variable declarations can appear anywhere a statement can appear; scope of all local variables is from the declaration to the end of the block

63
New cards

Declaration order: C#

The scope of any variable declared in a block is the whole block, regardless of the position of the declaration, but a variable still must be declared before it can be used

64
New cards

Variables declared in for statements (C++, Java, C#)

The scope of such variables is restricted to the for construct

65
New cards

Declaration vs definition in C and C++

Declarations specify just attributes; definitions specify attributes and storage; a declaration outside a function definition specifies the variable is defined in another file

66
New cards

Global scope in PHP

Variables implicitly declared in a function are local to the function; those implicitly declared outside functions extend from the declaration to the end of the program but skip over intervening functions; globals are accessed in a function via the $GLOBALS array or by declaring it global

67
New cards

Global scope in Python

A global variable can be referenced in functions, but can be assigned in a function only if it has been declared global

68
New cards

Problems with static scoping

In most cases too much access is possible; as a program evolves, the initial structure is destroyed and local variables often become global, and subprograms gravitate toward becoming global rather than nested

69
New cards

Dynamic scope

Scope based on the calling sequences of program units, not their textual layout (temporal rather than spatial); references are connected to declarations by searching back through the chain of subprogram calls

70
New cards

Scope example (big calls sub1, sub1 calls sub2, sub2 uses x)

Static scoping: the reference to x in sub2 is to big's x; dynamic scoping: the reference to x in sub2 is to sub1's x

71
New cards

Evaluation of dynamic scoping

Advantage: convenience; Disadvantages: (1) while a subprogram executes, its variables are visible to all subprograms it calls, (2) impossible to statically type check, (3) poor readability because the type of a variable cannot be statically determined

72
New cards

Scope vs lifetime

Sometimes closely related but different concepts, e.g., consider a static variable in a C or C++ function

73
New cards

Referencing environment of a statement

The collection of all names that are visible in the statement

74
New cards

Referencing environment in a static-scoped language

The local variables plus all of the visible variables in all of the enclosing scopes

75
New cards

Referencing environment in a dynamic-scoped language

The local variables plus all visible variables in all active subprograms

76
New cards

Active subprogram

A subprogram whose execution has begun but has not yet terminated

77
New cards

Named constant

A variable that is bound to a value only when it is bound to storage; advantages: readability and modifiability; used to parameterize programs

78
New cards

Manifest constant

A named constant whose value binding is static

79
New cards

Named constants in C++ and Java

Expressions of any kind are allowed, dynamically bound

80
New cards

const vs readonly in C#

const: values of named constants are bound at compile time; readonly: values are dynamically bound

81
New cards

Strong typing

Detecting all type errors