CS 363 Comparative Programming Languages - PowerPoint PPT Presentation

1 / 85
About This Presentation
Title:

CS 363 Comparative Programming Languages

Description:

Def: Name type compatibility means the two variables have compatible types if ... test. self add: 25. Transcript show: self printString; cr. CS 363 Spring 2005 ... – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 86
Provided by: tjh5
Category:

less

Transcript and Presenter's Notes

Title: CS 363 Comparative Programming Languages


1
CS 363 Comparative Programming Languages
  • Names, Type Checking, and Scopes

2
Names
  • User-defined names include variables, functions,
    classes, types
  • Design issues for names
  • Maximum length?
  • Are connector characters (_,-,) allowed?
  • Are names case sensitive?
  • Are special words reserved words or keywords?

CS 363 Spring 2005 GMU
2
3
Names
  • Length
  • If too short, they cannot be connotative
  • Language examples
  • FORTRAN I maximum 6
  • COBOL maximum 30
  • FORTRAN 90 and ANSI C maximum 31
  • Ada and Java no limit, and all are significant
  • C no limit, but implementers often impose one

CS 363 Spring 2005 GMU
3
4
Names
  • Case sensitivity
  • Disadvantage readability (names that look alike
    are different)
  • In C /Java because predefined names are mixed
    case (e.g. IndexOutOfBoundsException)
  • C, C, and Java names are case sensitive (b and
    B are different variables)
  • The names in some languages are not

CS 363 Spring 2005 GMU
4
5
Names
  • Python, Ruby
  • xname 45.67 Different from xName
  • xname hello world
  • xname 1,2,3
  • xname 1,2,3
  • Scheme Same as xName
  • (define xname 45.6)
  • (define xname hello world)
  • (define xname '((1 2) 3)

CS 363 Spring 2005 GMU
5
6
Names
  • ML
  • val xname 567.89 Different from xName
  • val xname 567.89 real
  • xname -gt val it 567.89 real
  • xname hello world
  • val it "hello world" string
  • xname 1,2,3
  • val it 3,4,5 int list
  • xname 3,4,5,6,7
  • val it 3,4,5,6,7 int list list

CS 363 Spring 2005 GMU
6
7
Names
  • Smalltalk
  • Different from xName
  • xname 45.6.
  • xname 'hello'.
  • xname (1,2,3).
  • xname ((3,4), 5).

CS 363 Spring 2005 GMU
7
8
Names
  • Prolog
  • Variables begin with a capital letter
  • is for numbers
  • Xname is 45.6.
  • assert(makelist(1,2,3,4)).
  • makelist(What).
  • What 1,2,3,4

CS 363 Spring 2005 GMU
8
9
Names
  • C/C
  • Different from xName
  • double xnum 45.6
  • char xname hello
  • int nums4 1,2,3,4
  • for(int i 0 ilt4i) cout ltlt numsi ltlt endl

CS 363 Spring 2005 GMU
9
10
Names
  • Java
  • Different from xName
  • double xnum 45.6
  • String xname hello
  • int nums new int3

CS 363 Spring 2005 GMU
10
11
Names
  • Special words keywords, reserved words
  • Ex while, for,
  • An aid to readability used to delimit or
    separate statement clauses
  • Def A keyword is a word that is special only in
    certain contexts
  • Disadvantage poor readability, compiling
  • Def A reserved word is a special word that
    cannot be used as a user-defined name

CS 363 Spring 2005 GMU
11
12
Names
  • Scheme define, if, car, cdr, remainder, null?
  • Python, Ruby def, if, elif/elsif, end (Ruby),
  • tabs/ (Python)
  • ML fun, if then else, tl, hd
  • Prolog assert, is,
  • Smalltalk/Java large library of methods/classes
  • C/C int, double, printf/cout, while, if

CS 363 Spring 2005 GMU
12
13
Variables
  • A variable is an abstraction of a memory cell(s)
  • Variables can be characterized as a sextuple of
    attributes
  • (name, address, value, type, lifetime, and scope)
  • Not all variables have names (anonymous)

CS 363 Spring 2005 GMU
13
14
Variables
  • Address - the memory address with which a
    variable is associated
  • A variable may have different addresses at
    different times during execution (variable local
    to a function)
  • A variable may have different addresses at
    different places in a program (variable name used
    in multiple scopes)
  • l-value of a variable (x )

CS 363 Spring 2005 GMU
14
15
Variables
  • If two variable names can be used to access the
    same memory location, they are called aliases
  • Aliases are harmful to readability (program
    readers must remember all of them)
  • How aliases can be created
  • Pointers, reference variables, C and C unions,
    (and through parameters - discussed in Chapter 9)
  • Some of the original justifications for aliases
    are no longer valid e.g. memory reuse in FORTRAN
  • Replace them with dynamic allocation

CS 363 Spring 2005 GMU
15
16
Variables
  • In Python
  • x 1,2,3
  • y x
  • y.append(4)
  • y -gt 1,2,3,4
  • x -gt 1,2,3,4

CS 363 Spring 2005 GMU
16
17
Variables
  • In Ruby
  • x 1,2,3
  • y x
  • y.pop()
  • y -gt 1,2
  • x -gt 1,2

CS 363 Spring 2005 GMU
17
18
Variables
  • In Smalltalk
  • xname ((3,4), 5) asOrderedCollection
  • yname xname
  • yname removeLast 5
  • xname -gt OrderedCollection ((3 , 4 ) , )
  • yname -gt OrderedCollection ((3 , 4 ) , )

CS 363 Spring 2005 GMU
18
19
Variables
  • In ML
  • - val xname 3,4,5,6,7
  • - val y xname
  • val y 3,4,5,6,7 int list list
  • - val c 1,2
  • - tl(c) val it 2 int list list
  • - y tl(c)
  • val it false bool
  • - y val it 3,4,5,6,7 int list list

CS 363 Spring 2005 GMU
19
20
Variables
  • In Java
  • String name "hello"
  • String name2 name
  • System.out.println(name)
  • System.out.println("Name2" name2)
  • name2 "world"
  • System.out.println("Name" name) hello
  • System.out.println("Name2" name2) world

CS 363 Spring 2005 GMU
20
21
Variables
  • Type - determines the size of memory location,
    range of values of variables and the set of
    operations that are defined for values of that
    type, precision (floating point)
  • Value - the contents of the location with which
    the variable is associated
  • r-value of a variable ( x )

CS 363 Spring 2005 GMU
21
22
Binding
  • A binding is an association, such as between an
    attribute and an entity, or between an operation
    and a symbol
  • Binding time is the time at which a binding takes
    place.

CS 363 Spring 2005 GMU
22
23
Binding (Python)
  • def one()
  • x "hello"
  • print x
  • def two(x)
  • print x
  • 4
  • def main()
    hello
  • x 4
    world
  • print x
    4
  • one()
  • two("world")
  • print x

CS 363 Spring 2005 GMU
23
24
Binding (Ruby)
  • def one()
  • x "hello"
  • print x, \n
  • end
  • def two(x)
  • print x, \n
  • end
    4
  • def main()
    hello
  • x 4
    world
  • print x, \n
    4
  • one()
  • two("world")
  • print x, \n end

CS 363 Spring 2005 GMU
24
25
Possible Binding Times
  • Language design time e.g., operator symbols to
    operations
  • Language implementation time e.g., bind
    floating point type to a representation
  • Compile time e.g., bind a variable to a type
  • Load time e.g., bind a FORTRAN 77 variable to a
    memory cell (or a C static variable)
  • Runtime e.g., bind a local variable to a memory
    cell
  • Different languages make different choices about
    binding times.

CS 363 Spring 2005 GMU
25
26
The Concept of Binding
  • Def A binding is static if it first occurs
    before run time and remains unchanged throughout
    program execution.
  • Def A binding is dynamic if it first occurs
    during execution or can change during execution
    of the program.

CS 363 Spring 2005 GMU
26
27
Overloading
  • More than one binding for a name in a given
    scope.
  • All languages offer limited overloading ( for
    example)
  • Subroutine names (Ada, C, Java)
    differentiated by the arguments
  • Built-in Operators (Ada, C, Fortran 90)

CS 363 Spring 2005 GMU
27
28
Type Bindings
  • How is a type specified?
  • When does the binding take place?
  • If static, the type may be specified by either an
    explicit or an implicit declaration

CS 363 Spring 2005 GMU
28
29
Types
  • Def An explicit declaration is a program
    statement used for declaring the types of
    variables
  • Def An implicit declaration is a default
    mechanism for specifying types of variables (the
    first appearance of the variable in the program)
  • FORTRAN, PL/I, BASIC, and Perl provide implicit
    declarations
  • Advantage writability
  • Disadvantage reliability (less trouble with Perl)

CS 363 Spring 2005 GMU
29
30
Types
  • Dynamic Type Binding (JavaScript and PHP)
  • Specified through an assignment statement
    e.g., JavaScript
  • list 2, 4.33, 6, 8
  • list 17.3
  • Advantage flexibility (generic program units)
  • Disadvantages
  • High cost (dynamic type checking and
    interpretation)
  • Type error detection by the compiler is difficult

CS 363 Spring 2005 GMU
30
31
Types
  • Type Inferencing (ML, Miranda, and Haskell)
  • Rather than by assignment statement, types are
    determined from the context of the reference

CS 363 Spring 2005 GMU
31
32
Type Checking
  • Generalize the concept of operands and operators
    to include subprograms and assignments
  • Def Type checking is the activity of ensuring
    that the operands of an operator are of
    compatible types
  • Def A compatible type is one that is either
    legal for the operator, or is allowed under
    language rules to be implicitly converted, by
    compiler- generated code, to a legal type. This
    automatic conversion is called a coercion.
  • Def A type error is the application of an
    operator to an operand of an inappropriate type

CS 363 Spring 2005 GMU
32
33
Type Checking
  • If all type bindings are static, nearly all type
    checking can be static
  • If type bindings are dynamic, type checking must
    be dynamic
  • Def A programming language is strongly typed if
    type errors are always detected

CS 363 Spring 2005 GMU
33
34
Strong Typing
  • Advantage of strong typing allows the detection
    of the misuses of variables that result in type
    errors
  • What languages are strongly typed?
  • FORTRAN 77 is not parameters, EQUIVALENCE
  • Pascal is not variant records
  • C and C are not parameter type checking can be
    avoided unions are not type checked
  • Ada is, almost (UNCHECKED CONVERSION is explicit
    loophole) (Java is similar)

CS 363 Spring 2005 GMU
34
35
Strong Typing
  • Coercion rules strongly affect strong
    typing--they can weaken it considerably (C
    versus Ada)
  • Although Java has just half the assignment
    coercions of C, its strong typing is still far
    less effective than that of Ada

CS 363 Spring 2005 GMU
35
36
Strong Typing - ML
  • - 3.0 4.0
  • val it 7.0 real
  • - 3 4.0
  • Error operator and operand don't agree
  • operator domain int int
  • operand int real
  • in expression 3 4.0

CS 363 Spring 2005 GMU
36
37
Typing
  • Python
  • gtgtgt 4 "hello"
  • TypeError unsupported operand type(s) for
    'int' and 'str'
  • gtgtgt "4" "hello"
  • '4hello'
  • gtgtgt print 4, "hello"
  • 4 hello

CS 363 Spring 2005 GMU
37
38
Typing
  • Smalltalk
  • x (1 2 3) asOrderedCollection
  • y x 4
  • x OrderedCollection (1 2 3 )
  • y OrderedCollection (5 6 7 )
  • x add 4
  • x OrderedCollection (1 2 3 4 )

CS 363 Spring 2005 GMU
38
39
Typing
  • Smalltalk
  • z (1 2 3)
  • z class Array
  • z 4 -gt (5 6 7 )
  • z add 4 -gt Error Message not appropriate for
    this type of object

CS 363 Spring 2005 GMU
39
40
Type Compatibility
  • Our concern is primarily for structured types
  • Def Name type compatibility means the two
    variables have compatible types if they are in
    either the same declaration or in declarations
    that use the same type name
  • Easy to implement but highly restrictive
  • Subranges of integer types are not compatible
    with integer types
  • Formal parameters must be the same type as their
    corresponding actual parameters (Pascal)

CS 363 Spring 2005 GMU
40
41
Type Compatibility
  • Def Structure type compatibility means that two
    variables have compatible types if their types
    have identical structures
  • More flexible, but harder to implement

CS 363 Spring 2005 GMU
41
42
Type Compatibility
  • Consider the problem of two structured types
  • Are two record types compatible if they are
    structurally the same but use different field
    names?
  • Are two array types compatible if they are the
    same except that the subscripts are different?
  • (e.g. 1..10 and 0..9)
  • Are two enumeration types compatible if their
    components are spelled differently?
  • With structural type compatibility, you cannot
    differentiate between types of the same structure
    (e.g. different units of speed, both float)

CS 363 Spring 2005 GMU
42
43
Type Compatibility
  • Language examples
  • Pascal usually structure, but in some cases name
    is used (formal parameters)
  • C structure, except for records
  • Ada restricted form of name
  • Derived types allow types with the same structure
    to be different
  • Anonymous types are all unique, even in
  • A, B array (1..10) of INTEGER

CS 363 Spring 2005 GMU
43
44
Variable Lifetime
  • Storage Bindings Lifetime
  • Allocation - getting a cell from some pool of
    available cells
  • Deallocation - putting a cell back into the pool
  • Def The lifetime of a variable is the time
    during which it is bound to a particular memory
    cell
  • Lifetime dictated by the type of variable
    static, stack, explicit heap, implicit heap.

CS 363 Spring 2005 GMU
44
45
Lifetime Categories
  • Static--bound to memory cells before execution
    begins and remains bound to the same memory cell
    throughout execution.
  • e.g. all FORTRAN 77 variables, C static
    variables
  • Advantages efficiency (direct addressing),
    history-sensitive subprogram support
  • Disadvantage lack of flexibility (no recursion)

CS 363 Spring 2005 GMU
45
46
Lifetime Categories
  • Stack-dynamic--Storage bindings are created for
    variables when their declaration statements are
    elaborated.
  • If scalar, all attributes except address are
    statically bound e.g. local variables in C
    subprograms and Java methods
  • Advantage allows recursion conserves storage
  • Disadvantages
  • Overhead of allocation and deallocation
  • Subprograms cannot be history sensitive
  • Inefficient references (indirect addressing)

CS 363 Spring 2005 GMU
46
47
Lifetime Categories
  • Explicit heap-dynamic--Allocated and deallocated
    by explicit directives, specified by the
    programmer, which take effect during execution
  • Referenced only through pointers or references
  • e.g. dynamic objects in C (via new and delete)
  • all objects in Java
  • Advantage provides for dynamic storage
    management
  • Disadvantage inefficient and unreliable

CS 363 Spring 2005 GMU
47
48
Lifetime Categories
  • Implicit heap-dynamic--Allocation and
    deallocation caused by assignment statements
  • e.g. all variables in APL all strings and
    arrays in Perl and JavaScript
  • Advantage flexibility
  • Disadvantages
  • Inefficient, because all attributes are dynamic
  • Loss of error detection

CS 363 Spring 2005 GMU
48
49
Scope
  • Def The scope of a variable declaration is the
    range of program statements over which it is
    visible
  • The scope rules of a language determine how
    references to names are associated with variables
  • The terms scope and name space are sometimes
    used interchangably.
  • Two approaches static and dynamic

CS 363 Spring 2005 GMU
49
50
Fortran 77 Name Space
CS 363 Spring 2005 GMU
50
51
Scheme Name Space
  • All objects (built-in and user-defined) reside in
    single global namespace
  • let expressions create nested lexical scopes

f1()
f2()
CS 363 Spring 2005 GMU
51
52
C Name Space
  • Global scope holds variables and functions
  • No function nesting
  • Block level scope introduces variables and labels
  • File level scope with static variables that are
    not visible outside the file (global otherwise)

Block scope
CS 363 Spring 2005 GMU
52
53
Java Name Space
  • Limited global name space with only public
    classes
  • Fields and methods in a public class can be
    public ? visible to classes in other packages
  • Fields and methods in a class are visible to all
    classes in the same package unless declared
    private
  • Class variables visible to all objects of the
    same class.

CS 363 Spring 2005 GMU
53
54
Smalltalk
  • ! Object methodsFor 'algorithms' !
  • main
  • "(FileStream oldFileNamed
    'selection.st') fileIn.
  • numbers (4 3 16 1 14 25 2)
    asOrderedCollection.
  • numbers selection"
  • xname
  • xname (4 3 16 1) asOrderedCollection.
  • Transcript show xname printString cr.
  • xname test.
  • Transcript show xname printStringcr.
  • !
  • test
  • self add 25.
  • Transcript show self printString cr.
  • !

CS 363 Spring 2005 GMU
54
55
Smalltalk
  • test
  • self add 25.
  • Transcript show self printString cr.
  • !
  • In Transcript window
  • OrderedCollection (4 3 16 1 )
  • OrderedCollection (4 3 16 1 25 )
  • OrderedCollection (4 3 16 1 25 )

CS 363 Spring 2005 GMU
55
56
Python
  • def one(x)
  • x.append(25)
  • print x
  • def main() 4, 10, 15
  • x 4,10, 15 4, 10, 15, 25
  • print x 4, 10, 15, 25
  • one(x)
  • print x
  • main()

CS 363 Spring 2005 GMU
56
57
Ruby
  • def one(x)
  • x.push(25)
  • print x, \n
  • end
  • def main() 4, 10, 15
  • x 4,10, 15 4, 10, 15, 25
  • print x, \n 4, 10, 15, 25
  • one(x)
  • print x, \n
  • end
  • main()

CS 363 Spring 2005 GMU
57
58
Scheme
  • (define (main) (let ((x '(4 10 15)))
  • (print x) (test x) (print x)))
  • (define (test x)
  • (set! x (cons 25 x)) (print x))
  • 4, 10, 15
  • 25, 4, 10, 15, 25
  • 4, 10, 15

CS 363 Spring 2005 GMU
58
59
Scope
  • Understanding scope rules of a given language
    allows us to answer the following
  • Where is a given variable visible?
  • What variables are visible at a given statement
    in the program?

CS 363 Spring 2005 GMU
59
60
Static Scope
  • Based on program text
  • To connect a name reference to a variable, you
    (or the compiler) must find the declaration
  • Search process search declarations, first
    locally, then in increasingly larger enclosing
    scopes, until one is found for the given name
  • A variable is local to a procedure if the
    declaration occurs in that procedure
  • A variable is nonlocal to a procedure if it is
    visible in the procedure but not declared there

CS 363 Spring 2005 GMU
60
61
Scope
  • Variables can be hidden from a unit by having a
    "closer" variable with the same name
  • C and Ada allow access to these "hidden"
    variables
  • In Ada unit.name
  • In C class_namename

CS 363 Spring 2005 GMU
61
62
Referencing Environments
  • Def The referencing environment of a statement
    is the collection of all names that are visible
    to the statement
  • In a static-scoped language, it is the local
    variables plus all of the visible variables in
    all of the enclosing scopes

CS 363 Spring 2005 GMU
62
63
Example Pascal-like language
  • Program main
  • a,b,c real
  • procedure sub1(a real)
  • d int
  • procedure sub2(c int)
  • d real
  • body of sub2
  • procedure sub3(aint)
  • body of sub3
  • body of sub1
  • body of main

CS 363 Spring 2005 GMU
63
64
Example
  • Program main
  • a,b,c real
  • procedure sub1(a real)
  • d int
  • procedure sub2(c int)
  • d real
  • body of sub2
  • procedure sub3(aint)
  • body of sub3
  • body of sub1
  • body of main

CS 363 Spring 2005 GMU
64
65
Example
  • Program main
  • a,b,c real
  • procedure sub1(a real)
  • d int
  • procedure sub2(c int)
  • d real
  • body of sub2
  • procedure sub3(aint)
  • body of sub3
  • body of sub1
  • body of main

CS 363 Spring 2005 GMU
65
66
Example
  • Program main
  • a,b,c real
  • procedure sub1(a real)
  • d int
  • procedure sub2(c int)
  • d real
  • body of sub2
  • procedure sub3(aint)
  • body of sub3
  • body of sub1
  • body of main

CS 363 Spring 2005 GMU
66
67
Example
  • Program main
  • a,b,c real
  • procedure sub1(a real)
  • d int
  • procedure sub2(c int)
  • d real
  • body of sub2
  • procedure sub3(aint)
  • body of sub3
  • body of sub1
  • body of main

CS 363 Spring 2005 GMU
67
68
Static Scope
  • Advantages
  • Readability
  • Based on program text ? can be evaluated by a
    compiler
  • Constant time implementation
  • Disadvantages
  • Encourages global variables

CS 363 Spring 2005 GMU
68
69
Dynamic Scope
  • Based on calling sequences of program units, not
    their textual layout (temporal versus spatial)
  • References to variables are connected to
    declarations by searching the chain of subprogram
    calls (runtime stack) that forced execution to
    this point

CS 363 Spring 2005 GMU
69
70
Scope Example
  • MAIN
  • - declaration of x
  • SUB1
  • - declaration of x -
  • ...
  • call SUB2
  • ...
  • SUB2
  • ...
  • - reference to x -
  • ...
  • ...
  • call SUB1

CS 363 Spring 2005 GMU
70
71
Scope Example
  • MAIN
  • - declaration of x
  • SUB1
  • - declaration of x -
  • ...
  • call SUB2
  • ...
  • SUB2
  • ...
  • - reference to x -
  • ...
  • ...
  • call SUB1

CS 363 Spring 2005 GMU
71
72
Scope Example
  • In a dynamic-scoped language, the referencing
    environment is the local variables plus all
    visible variables in all active subprograms.
  • A subprogram is active if its execution has begun
    but has not yet terminated.

CS 363 Spring 2005 GMU
72
73
Scope Example
  • MAIN
  • - declaration of x
  • SUB1
  • - declaration of x -
  • ...
  • call SUB2
  • ...
  • SUB2
  • ...
  • - reference to x -
  • ...
  • ...
  • call SUB1

CS 363 Spring 2005 GMU
73
74
Dynamic Scoping
  • Evaluation of Dynamic Scoping
  • Advantage convenience (easy to implement)
  • Disadvantage poor readability, unbounded search
    time

CS 363 Spring 2005 GMU
74
75
Scope and Lifetime
  • Scope and lifetime are closely related, but are
    different concepts
  • Consider a static variable in a C or C function
  • Lifetime entire program execution
  • Scope limited to statements in the function

CS 363 Spring 2005 GMU
75
76
Static Scope Runtime
  • Activation record keep information associated
    with each procedure call instance parameters,
    local variables, return address, return values
  • Procedure call time new activation pushed onto
    runtime stack
  • Procedure return time activation popped off
    runtime stack

CS 363 Spring 2005 GMU
76
77
Static Scope Runtime
  • At runtime, we need to be able to find the
    correct instance of a variable being used.
  • Additional field in activation record a pointer
    (static link) to the activation record for the
    closest instance of enclosing scope.
  • Pointers form a static chain back to the main.
  • Search back along these enclosing link pointers
    to find non-local variables
  • Chain never gets longer than the scope depth.

CS 363 Spring 2005 GMU
77
78
Static links
  • Program main
  • a,b,c real
  • procedure sub1(a real)
  • d int
  • procedure sub2(c int)
  • d real
  • body of sub2
  • procedure sub3(aint)
  • call sub2
  • if E call sub1 else call sub3
  • call sub1

CS 363 Spring 2005 GMU
78
79
Static links
  • Program main
  • a,b,c real
  • procedure sub1(a real)
  • d int
  • procedure sub2(c int)
  • d real
  • body of sub2
  • procedure sub3(aint)
  • call sub2
  • if E call sub1 else call sub3
  • call sub1

CS 363 Spring 2005 GMU
79
80
Static links
  • Program main
  • a,b,c real
  • procedure sub1(a real)
  • d int
  • procedure sub2(c int)
  • d real
  • body of sub2
  • procedure sub3(aint)
  • call sub2
  • if E call sub1 else call sub3
  • call sub1

CS 363 Spring 2005 GMU
80
81
Static links
  • Program main
  • a,b,c real
  • procedure sub1(a real)
  • d int
  • procedure sub2(c int)
  • d real
  • body of sub2
  • procedure sub3(aint)
  • call sub2
  • if E call sub1 else call sub3
  • call sub1

CS 363 Spring 2005 GMU
81
82
Static links
  • Program main
  • a,b,c real
  • procedure sub1(a real)
  • d int
  • procedure sub2(c int)
  • d real
  • body of sub2
  • procedure sub3(aint)
  • call sub2
  • if E call sub1 else call sub3
  • call sub1

CS 363 Spring 2005 GMU
82
83
Static links
  • Program main
  • a,b,c real
  • procedure sub1(a real)
  • d int
  • procedure sub2(c int)
  • d real
  • body of sub2
  • procedure sub3(aint)
  • call sub2
  • if E call sub1 else call sub3
  • call sub1

CS 363 Spring 2005 GMU
83
84
Static Scope Runtime
  • Static Chain.
  • Chain never gets longer than the maximum scope
    depth.
  • For a given function, the compiler can compute
  • the exact number of links to traverse to find the
    required instance and
  • The variable offset (location) in the given
    activation record

CS 363 Spring 2005 GMU
84
85
Static links
  • Program main
  • a,b,c real
  • procedure sub1(a real)
  • d int
  • procedure sub2(c int)
  • d real
  • body of sub2
  • procedure sub3(aint)
  • call sub2
  • if E call sub1 else call sub3
  • call sub1

CS 363 Spring 2005 GMU
85
Write a Comment
User Comments (0)
About PowerShow.com