Names, Scopes, and Bindings Flashcards

Covers Names, Scopes, and Bindings

1
Q

Define Names

A

Mnemonic string used to represent something else; Sort of like tokens. Allows us to refer to variables, types, operations, etc… rather than low-level concepts like addresses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define Binding

A

Association between 2 things; a name and the thing it names.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Binding Time?

A

The point at which binding is created. Or, more generally, the point at which any implementation decision is made.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a regular expression?

A

Formal notation for the lexical structure to ensure clear rules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the scope of the Binding?

A

The part of the program (textually) in which the binding is active.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Some things are defined during language design time. Name a few.

A
  • Program structure
  • Primitive types
  • Constructors
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Name some items that can be defined during the language implementation time.

A
  • I/O
  • Number of bits in primitive types
  • How overflow is handled
  • Organization of maximum size of stack and heap
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are some pros/cons defining more at language design time than implementation time.

A
  • Portability
  • Disallows architecture specific optimization
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are some pros/cons of binding in early stages VS later stages

A
  • Greater efficiency
  • Less flexibility
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

(T/F) The compiler is written during the programming language design time.

A

False. The compiler is written during the implementation phase.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

At what point can programmers modify the programming language?

A

During the language implementation time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What things are determined during program writing time?

A
  • Algorithms
  • Names
  • Data Structures
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What things are determined during link time?

A
  • Layout of the whole program in memory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is determined during load time?

A
  • Choice of physical addresses
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What things are determined during compile time?

A

The layout of statically defined data in memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What things are determined during run time?

A
  • Variable values
  • Sizes of strings
  • Start-up time
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is Static Binding?

A

Binding that happens before run time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is Dynamic Binding?

A

Binding that occurs during run time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Compilers VS Interpreters: Which has early binding time?

A

Compilers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Name the 7 language times or phases that binding depends on.

A
  • Design time
  • Implementation time
  • Program writing time
  • Compile time
  • Link time
  • Load time
  • Run time
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Define Bindings Lifetime

A

The period of time between the creation and the destruction of a name-to-object binding.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Define Object Lifetime

A

The time between the creation and destruction of an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Name key events during an objects lifetime

A
  • Creation of object
  • Creation of bindings
  • How objects are referenced using the bindings
  • Temporary deactivation of binding
  • Reactivation
  • Destruction of both bindings and objects.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

When does a dangling reference occur?

A

When a binding outlives the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is the scope of a binding?

A

The region of the program in which the binding is active.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

When does an object become garbage?

A

This happens when an object outlives its binding.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Depending on the lifetime of an object, what are the 3 ways to store them?

A
  • Static
  • Stack
  • Heap
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What happens to objects during Static storage allocation?

A

Objects are given an address that is maintained through the life of the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What happens to memory during Stack storage allocation.

A

Memory allocated in last-in, first-our order, usually in conjunction with subroutine calls and returns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Describe Heap storage allocation

A

A region of storage in which sub-blocks can be allocated and deallocated at arbitrary times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What are some examples of statically allocated memory?

A
  • Global defines
  • Code Language translation to machine
  • Static variables
  • Explicit constants
  • Tables used for debugging
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Give an example of when static allocation can be used for the whole program.

A

Static allocation can be used when a language does not support recursion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

List the objects and information commonly found in a stack frame.

A
  • Arguments
  • Return address
  • Misc bookkeeping
  • Local variables
  • Temporaries
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What is stack allocation typically used for?

A
  • Parameters
  • Local Variables
  • Temporaries (intermediate values of expressions)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Why use a stack? Give three reasons.

A
  • Allocate space for recursive routines
  • Reuse space
  • Space for active routines is less than space for all routines (like static allocation)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Stacks use two pointers to keep track of subroutine calls. What are they?

A
  • SP - Stack Pointer
  • FP - Frame Pointer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

In a stack, what does the stack pointer point to?

A

First unused location of the stack. Top of the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

In a stack, what does the frame pointer point to?

A

Close to the return address of the subroutine.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

In heap allocation, what is internal fragmentation?

A

This occurs when storage management allocates a block that is larger than required to hold a given object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

In heap allocation, what is external fragmentation?

A

Memory is available, but in small non-contiguous pieces.

41
Q

What are heap stuctures’ two main principal concerns?

A
  • Speed
  • Space
42
Q

What is a disadvantage to more intricated heap management?

A

Time consumption

43
Q

What is the calling sequence?

A

The code executed by the caller immediately before and after the call.

44
Q

What is garbage collection?

A

Deallocates memory when objects are no longer used.

45
Q

What are the advantages of explicit garbage collection?

A
  • Simpler compiler implementation
  • Better execution speed
46
Q

What are the disadvantages to explicit garbage collection?

A
  • Subject to dangling references
  • Programmer errors causing memory leaks
47
Q

What are the advantages of implicit garbage collection?

A
  • Simplifies programming task
  • No dangling references
  • Fewer memory leaks
48
Q

What are the disadvantages of implicit garbage collection?

A
  • Adds complexity to the implementation of a language
  • Reduces execution speed
49
Q

What is a more precise definition for scope?

A

A program section of maximal size in which no bindings change.

50
Q

What is a referencing environment?

A

The set of active bindings at any given point in a program’s execution.

51
Q

What do scope rules do?

A

Define referencing environment.

52
Q

Describe elaboration.

A

Refers to the process by which declarations become active when control first enters a scope.

53
Q

What happens to the binding of global variables that are redeclared?

A

These bindings are deactivated temporarily untill the duplicate variable goes out of scope.

54
Q

Describe static scoping.

A

This happens when the scope of the binding is determined at compile time by examining the text of the program.

55
Q

Describe dynamic scoping.

A

This happens when the scope of a binding is determined at runtime (and depends on flow of execution).

56
Q

Describe the most closely nested rule.

A

An identifier is known in the scope in which it is declared and in each enclosed scope, unless it is redeclared in an enclosed scope.

57
Q

(T/F) Outer enclosing routines are not visible to nested subroutines.

A

FALSE

58
Q

How are static links used?

A

Each frame points to the frame of the (correct instance of) the routine inside which it was declared.

59
Q

In Pascal, how can pointer point to a node that includes the pointer type?

Example:

ListPtr = POINTER TO ListNode


ListNode = RECORD 


data: ... 
  
next: ListPtr;


END

A

A pointer is an exception because it can reference any type and still have the same size.

60
Q

What does information hiding allow the system to do?

A

It helps make details invisible to parts of the system that don’t need them.

61
Q

What are some of the benefits of information hiding?

A
  • Reduces cognitive load on programmers
  • Reduces the risk of name conflicts
  • Can limit access to internals of another abstraction (undefined symbol errors)
62
Q

How can variables keep their values after leaving their local subroutine invocation?

A
  • Static in C
  • Save in Fortran
63
Q

What is a module? Describe it’s visibility with respect to the whole system.

A

A module is a collection of objects that are visible to other objects inside the module unless exported.

64
Q

How are global modules in Modula-2 divided?

Describe their contents.

A
  • Definition module
    • Defines the interface
    • Contains all info needed to compile a module that uses it (exported names, types, etc)
  • Implementation module
    • Defines internals
65
Q

How many instances can Modula-2 have?

How can instances be created?

A

One instance.

Each instance is a separate module. The code has to be duplicated for a separate instance.

66
Q

What is a technique in Modula-2 that allows multiple instances of a module?

A

The module can be used as a manager of a type rather than the type itself.

67
Q

Do classes dynamically bind procedures to a type?

A

No. They statically bind procedures to a type.

68
Q

Programming in the large:

What are the pros/cons of independent compilation?

A

Can compile different parts of a program independently; however, there is no type checking accross the compilation units.

69
Q

Programming in the large:

What are some of the benefits of separate compilation?

A

Can compile different parts of a program independently with type checking across the compilation units.

70
Q

During separate compilation, the definition module is written and compiled to get a symbol file. With the symbol files, clients can compile their code to get what kind of file?

A

Object file.

71
Q

What is Dynamic Scope?

A

This scope applies to the most recent binding encountered in the program execution.

APL, Lisp, Perl use dynamic scope.

72
Q

What are some disadvantages of dynamic scoping?

A
  • While subroutines are executing, its local variables are exposed to subprograms.
  • Cannot statically type check references.
  • Difficult to understand (need to know the path of the calling sequence).
73
Q

When implementing a static scope in a compiler, what kind of table gets created to store object names and attributes?

A

Symbol table.

74
Q

The symbol table attributes includes various things that the compiler needs to know about the name. Name some of these attributes.

A
  • Name type
  • Address of offset
  • Value of constant, etc.
75
Q

What are some basic operations provided by a symbol table?

A
  • Create an empty symbol table
  • Insert an entry
  • Lookup an entry
  • Delete an entry
76
Q

What kind of table can be used to create a symbol table with a monolithic block?

A

A hash table can be used.

  • Insert entries into the table when variables are declared.
  • Lookup entries when variables are declared.
77
Q

In a flat block structure, a symbol table will have variable values for two scope levels. Which are these two basic scope levels?

A
  • Global
  • Local
78
Q

What kind of storage structure is used on a flat block symbol table?

A

A stack is used. This allows entries to be added in a LIFO way.

79
Q

On the symbol table, what scope level are global variables in?

A

Level 1

80
Q

Symbol tables keep track of variable scopes with levels. If a variable has multiple entries in the symbol table, which scope level is chosen at a given instance?

A

The variable entry with the highest scope level is selected.

81
Q

Using a stack to organize a symbol table is simple. Inserting and removing entries is done quickly. On the other hand, what are some drawbacks when using a stack?

A
  • Slow lookup E.g., searching for globals located at the end of a stack.
  • For debuggers, symbol table info is usefull. It should not be removed when not used.
82
Q

As opposed to using a stack data structure for a symbol table, the Leblanc-Cook has improvements that help read items from the table a lot quicker. What technique does this table use?

A
  • Each scope is assigned a unique serial #.
  • Stores identifiers in a hash table.
  • Table chains entries with the same identifier.
  • A separate scope stack records the scopes that belong to the current referencing env.
83
Q

When a Leblanc-Cook symbol table item goes out of scope, which data structure gets updated, the hash table or the scope stack?

A

The scope stack. Items get poped off the stack. The updated stack ensure the correct version of the item gets used.

84
Q

The Leblanc-Cook symbol table works great for static scope during compilation. What are two common symbol tables used for dynamic scope (runtime)?

A
  • Association List
  • Central reference table
85
Q

In an Association list symbol table, entries are placed in what kind of a data structure?

A

The items are placed on a stack during runtime (dynamic scope)

86
Q

What kind of data structure gets used in a Central reference symbol table?

A

Hash table

87
Q

On a Central Reference Table, new entries are added at the fron of a chain and lookup retreives the first entry in the chain. When an item is no longer in scope, is the item kept or deleted?

A

The item is deleted.

88
Q

Deep Binding Vs Shallow Binding

This type of binding occurs when a routine is called.

A

Shallow Binding

89
Q

Deep Binding Vs Shallow Binding

This type of binding occurs when a routine is referenced.

A

Deep Binding

90
Q

In deep binding, it is important to keep track of the reference location and the binding environment of the subroutine. What is the name of this event?

A

Closure

91
Q

Define First Class Objects, and give examples.

A

These objects can be passed as parameters, returned, and assigned to a variable.

  • Simple types
  • Functions in functional languages
92
Q

Define Second Class Objects, and give an example.

A

Objects that are passed but not returned or assigned.

  • Procedures in many imperative languages.
93
Q

Define Third Class Objects, and give an example.

A

These objects cannot be passed, returned, or assigned.

  • Labels in most languages.
94
Q

In Perl, the ‘local’ keyword is used to make variables local in a block. When the program leaves the block, the saved value is restored to the symbol table.

This approach gives (Dynamic | Static) binding of names, and (Shallow | Deep) binding of reference environments.

Choose the best answers.

A

Dynamic

Shallow

95
Q

In Perl, a variable can be marked as a lexical variable in a block using the ‘my’ keyword. The variable is newly allocated in the block, and deallocated when the program leaves the block. It is stored in a stack-based structure separate from the symbol table.

This technique gives (Dynamic | Static) binding for names, and (Shallow | Deep) binding of the reference environment for that variable.

Choose the best answers.

A

Static

Deep

96
Q

What is an alias?

A

An alias is a set of multiple names that refer to the same object in a scope.

97
Q

When does overloading occur?

A

This occurs when multiple objects are bound to the same name in a scope.

If + is overloaded,

2.3 + 3.4 = 5.7

and

2 + 3 = 5

98
Q

When does polymorphism occur?

A

This occurs when methods work with multiple type parameters.

99
Q

When does coercion occur?

A

This occurs when a compiler automatically converts a value from one type to another when a second type is required by context.

If + is only defined for int, then

2.3 + 3.4 = 5