Exam 2 Flashcards
scope
visibility of names in source program
storage
memory space associated with names
lifetime
the time interval a variable is allocated with memory
static (lexical) scope
scopes can be determined by the compiler, all bindings for identifiers can be resolved by examining the program
let
scope of a variable declared in expression is the body
let*
scope of a variable declared in expression is body and other expressions following
letrec
variable does not need to be defined first for it to be in scope (allows for recursive definitions)
symbol table
table of names and their bindings
single scope
a dictionary or hash map
nested scope
a tree of symbol tables; each scope has one symbol table, each symbol table may have a parent
Name collision in symbol table
To find a binding:
- start from table of current scope
- Go up until name is found
- Fail if no table contains the name
Static Scoping
bindings determined by lexical structure, local renaming principle
Dynamic scoping
bindings depend on flow of control
always use most recent, active binding, names important, meaning of a variable can change
scope vs. lifetime
scope = visibility of a binding/name lifetime = creation/destruction of storage
global variable
static area, visible in entire program
local variable
stack, visible in a function
when are scope and lifetime not the same?
heap allocated memory, functions returned as values
shallow binding
subroutine is passed as a parameter, binding happens when subroutine is called
deep binding
when a subroutine is passed as a parameter, when the reference is created
function closure
for deep binding; contains a pointer to function and current symbol table
What type of binding is implemented for dynamic scoping
both shallow and deep; shallow has a higher cost at run time
What type of binding is implemented for static scoping?
Deep binding; shallow binding has never been implemented
alias
two variables point to the same memory location
overloading
uses the number or type of parameters to distinguish functions
coercion
the process by which a compiler automatically converts a value of one type to value of another type
coercion vs. overloading
coercion converts parameters to fit function def; overloading allows compiler to pick function implementation that fits
polymorphism
function with multiple forms/uses
parametric polymorphism
function with aset of types
subtype polymorphism
function with one type, and it’s refinements
Hows does the compiled code locate a variable used in a function at run time?
Start address = base addr (of scope) + offset (in the scope)
code section
read only, stores source code