programming language concepts/binding and scope
TRANSCRIPT
Programming Language Concepts/Binding and Scope
Programming Language Concepts/Binding and
Scope
Onur Tolga Sehitoglu
Bilgisayar Muhendisligi
11 Mart 2008
Programming Language Concepts/Binding and Scope
Outline
1 Binding2 Environment3 Block Structure
Monolithic block structureFlat block structureNested block structure
4 Hiding5 Static vs Dynamic Scope/Binding
Static bindingDynamic binding
6 DeclarationsDefinitions and DeclarationsSequential DeclarationsCollateral DeclarationsRecursive declarationsRecursive Collateral DeclarationsBlock ExpressionsBlock CommandsBlock Declarations
7 Summary
Programming Language Concepts/Binding and Scope
Binding
Binding
Most important feature of high level languages: programmersable to give names to program entities (variable, constant,function, type, ...). These names are called identifiers.
definition of an identifier ⇆ used position of an identifier.Formally: binding occurrence ⇆ applied occurrence.
Identifiers are declared once, used n times.
Language should map which corresponds to which.
Binding: Finding the corresponding binding occurrence(definition/declaration) for an applied occurrence (usage) ofan identifier.
Programming Language Concepts/Binding and Scope
Binding
for binding:
1 Scope of identifiers should be known. What is the blockstructure? Which blocks the identifier is available.
2 What will happen if we use same identifier name again“C forbids reuse of same identifier name in the same scope.Same name can be used in different nested blocks. Theidentifier inside hides the outside identifier”.
double f ,y;int f () { × error!
...
}
double y; × error!
double y;int f () {
double f ;√
OK
int y ;√
OK.
}
Programming Language Concepts/Binding and Scope
Environment
Environment
Environment: The set of binding occurrences that areaccessible at a point in the program.
Example:
struct Person { ... } x;int f (int a) {
double y;int x;... ©1
}
int main() {
double a;... ©2
}
O(©1)={struct People 7→ type, x 7→int,f 7→ func, a 7→ int, y 7→ double}
O(©2)={struct People 7→ type,x 7→ struct People, f 7→ func, a 7→double,main 7→ func}
Programming Language Concepts/Binding and Scope
Block Structure
Block Structure
Program blocks define the scope of the identifiers declaredinside. (boundary of the definition validity) For variables, theyalso define the lifetime.
Languages may have different block structures:
C function definitions and command blocks ({ ... }) define localscopes. Also each source code define a block.
Java Class definitions, class member function definitions, blockcommands define local scopes. Nested function definitions andnamespaces possible.
Haskell ‘let definitions in expression ’ defines a blockexpression. Also ‘ expression where definitions ’defines a block expression. (the definitions have a local scopeand not accessible outside of the expression)
Block structure of the language is defined by the organizationof the blocks.
Programming Language Concepts/Binding and Scope
Block Structure
Monolithic block structure
Monolithic block structure
Whole program is a block. All identifiers have global scopestarting from the definition.
Cobol is a monolithic block structure language.int x;
int y;
....
....
In a long program with many identifiers, they share the samescope and they need to be distinct.
Programming Language Concepts/Binding and Scope
Block Structure
Flat block structure
Flat block structure
Program contains the global scope and only a single level localscope of function definitions. No further nesting is possible.
Fortran and partially C has flat block structure.int x;
int y;
int f()
{ int a;
double b;
...
}int g()
{ int a;
double b;
...
}....
Programming Language Concepts/Binding and Scope
Block Structure
Nested block structure
Nested block structure
Multiple blocks with nested local scopes can be defined.
Pascal and Java have nested block structure.int x;
int f()
{ int a;
double g()
{ int x;
...
}...
}int g()
{ int h()
{ int x;
...
}...
}....
C block commands can be nested.
GCC extensions to C allow nested function definitions.
Programming Language Concepts/Binding and Scope
Hiding
Hiding
Identifiers defined in the inner local block hides the outerblock identifiers with the same name during their scope. Theycannot be accessed within the inner block.
int x,y;int f (double x) {
... // parameter x hides global x in f()
}
int g(double a) {
int y; // local y hides global y in g()
double f ; // local f hides global f() in g()
...
}
int main() {
int y; // local y hides global y in main ()
}
Programming Language Concepts/Binding and Scope
Static vs Dynamic Scope/Binding
Static vs Dynamic Scope/Binding
The binding and scope resolution is done at compile time or runtime? Two options:
1 Static binding, static scope
2 Dynamic binding, dynamic scope
First defines scope and binding based on the lexical structureof the program and binding is done at compile time.
Second activates the definitions in a block during theexecution of the block. The environment changes dynamicallyat run time as functions are called and returned.
Programming Language Concepts/Binding and Scope
Static vs Dynamic Scope/Binding
Static binding
Static binding
Programs shape is significant. Environment is based on theposition in the source (lexical scope)Most languages apply static binding (C, Haskell, Pascal, Java,...)
int x=1,y=2;
int f (int y) {
y=x+y; /* x global , y local */
return x+y;
}
int g(int a) {
int x=3; /* x local , y global */
y=x+x+a; x=x+y; y= f (x);return x;
}
int main() {
int y=0; int a=10; /* x global y local */
x=a+y; y=x+a; a= f (a); a=g(a);return 0;
}
Programming Language Concepts/Binding and Scope
Static vs Dynamic Scope/Binding
Dynamic binding
Dynamic binding
Functions called update their declarations on the environmentat run-time. Delete them on return. Current stack ofactivated blocks is significant in binding.
Lisp and some script languages apply dynamic binding.
1 int x=1,y=2;2 int f (int y) {
3 y=x+y;4 return x+y;5 }
6 int g(int a) {
7 int x=3;8 y=x+x+a; x=x+y;9 y= f (x);
10 return x;11 }
12 int main() {
13 int y=0; int a=10;14 x=a+y; y=x+a;15 a= f (a); a=g(a);16 return 0;
17 }
Trace Environmentinitial {x:gl, y:gl}
12 call main {x:gl, y:main, a:main, main()}
15 call f(10) {x:gl, y:f , a:main, main(), f()}
4 return f : 30 back to environment before f15 in main {x:gl, y:main, a:main, main()}
15 call g(30) {x:g, y:main, a:g, main(), g() }
9 call f(39) {x:g, y:f, a:g, main(), g(), f() }
4 return f : 117 back to environment before f
9 in g {x:g, y:main, a:g, main(), g() }
10 return g : 39 back to environment before g15 in main {x:gl, y:main, a:main, main()}16 return main x:gl=10, y:gl=2, y:main=117, a:main=39
Programming Language Concepts/Binding and Scope
Declarations
Declarations
Definitions vs Declarations
Sequential declarations
Collateral declarations
Recursive declarations
Collateral recursive declarations
Block commands
Block expressions
Programming Language Concepts/Binding and Scope
Declarations
Definitions and Declarations
Definitions and Declarations
Definition: Creating a new name for an existing binding.
Declaration: Creating a completely new binding.
in C: struct Person is a declaration. typedef struct
Person persontype is a definition.
in C++: double x is a declaration. double &y=x; is adefinition.
creating a new entity or not. Usually the distinction is notclear and used interchangeably.
Programming Language Concepts/Binding and Scope
Declarations
Sequential Declarations
Sequential Declarations
D1 ; D2 ; ... ; Dn
Each declaration is available starting with the next line. D1
can be used in D2 an afterwards, D2 can be used in D3 andafterwards,...
Declared identifier is not available in preceding declarations.
Most programming languages provide only such declarations.
Programming Language Concepts/Binding and Scope
Declarations
Collateral Declarations
Collateral Declarations
Start; D1 and D2 and ... and Tn ; End
Each declaration is evaluated in the environment precedingthe declaration group. Declared identifiers are available onlyafter all finishes. D1,... Dn uses in the environment of Start.They are in the available in the environment of End.
ML allows collateral declarations additionally.
Programming Language Concepts/Binding and Scope
Declarations
Recursive declarations
Recursive declarations
Declaration:Name = Body
The body of the declaration can access the declared identifier.Declaration is available in the body of itself.
C functions and type declarations are recursive. Variabledefinitions are usually not recursive. ML allows programmer tochoose among recursive and non-recursive function definitions.
Programming Language Concepts/Binding and Scope
Declarations
Recursive Collateral Declarations
Recursive Collateral Declarations
All declarations can access the others regardless of their order.
All Haskell declarations are recursive collateral (includingvariables)
All declarations are mutually recursive.
ML allows programmer to do such definitions.
C++ class members are like this.
in C a similar functionality can be access by prototypedefinitions.
Programming Language Concepts/Binding and Scope
Declarations
Block Expressions
Block Expressions
Allows an expression to be evaluated in a special localenvironment. Declarations done in the block is not availableoutside.
in Haskell: let D1; D2; ... ; Dn in Expression orExpression where D1; D2; ... ; Dn
x=5t =let x squa r e =x*x
f a c t o r i a l n = if n<2 then 1 else n* f a c t o r i a l (n-1)x f a c t = f a c t o r i a l x
in ( x squa r e +1)* x f a c t /( x f a c t * x squa r e +2)
Programming Language Concepts/Binding and Scope
Declarations
Block Expressions
Hiding works in block expressions as expected:
x=5 ; y=6 ; z = 3
t =let x=1in let y=2
in x+y+ z{-- t is 1+2+3 here. local x and y hides the ones above --}
Programming Language Concepts/Binding and Scope
Declarations
Block Commands
Block Commands
Similar to block expressions, declarations done inside a blockcommand is available only during the block. Statementsinside work in this environment. The declarations lost outsideof the block.
int x=3, i =2;x+= i ;while (x> i ) {
int i =0;...
i ++;}
/* i is 2 again */
Programming Language Concepts/Binding and Scope
Declarations
Block Declarations
Block Declarations
A declaration is made in a local environment of declarations.Local declarations are not made available to the outerenvironment.
in Haskell: Dexp where D1; D2; ... ; Dn
Only Dexp is added to environment. Body of Dexp has all localdeclarations available in its environment.
f i f t h p ow e r x = ( f o r t hpowe r x ) * x where
squa r ex = x*xf o r t hpowe r x = squa r ex * squa r ex
Programming Language Concepts/Binding and Scope
Summary
Summary
Binding, scope, environment
Block structure
Hiding
Static vs Dynamic binding
Declarations
Sequential, recursive, collateral
Expression, command and declaration blocks