block structure - PowerPoint PPT Presentation

About This Presentation
Title:

block structure

Description:

The use of x is statically bound, that of y is free ... Its scope: the function expression for zoo is excluded. A bit more terminology: ... – PowerPoint PPT presentation

Number of Views:117
Avg rating:3.0/5.0
Slides: 30
Provided by: off9
Category:
Tags: block | free | freezoo | structure | zoo

less

Transcript and Presenter's Notes

Title: block structure


1
Part II Procedural, block structured, languages
  • Procedural
  • Function() as main programming construct
  • variables to denote values
  • Block-structured
  • Blocks to control visibility of variables
  • (local definitions)
  • ()In this part, function is used for both
    function, procedure

2
  • Main requirements
  • Precisely defined, conceptually simple
    static structure for programs
  • addressed by universally accepted scope rules
  • Static structure should provide good
    understanding of dynamic behavior
  • addressed by correct use of scope in semantics
    implementation

Program structure should enable programmer
to predict its behavior
3
  • Contents
  • Semi-formal description of static structure
  • The substitution model operational semantics
    for a simple functional language
  • Introduction to type systems
  • Definitions, comparison of dynamic/static typing
  • A simple monomorphic type system
  • specification, type-checking,
    correctness
  • Extensions of the language with additional
    constructs recursion, cells
    with assignment, data structures,
  • including both semantics and types
  • The environment model an alternative
    operational semantics

4
Block structure
  • Literals, identifiers, variables
  • Variable declarations, definitions, uses
  • Blocks, regions, scope
  • Common kinds of blocks

5
Literals, identifiers variables
  • literals numbers, booleans, strings,
  • identifiers --- used by programmers to name
    entities
  • Two categories distinguished by syntax
    conventions
  • Identifier name spaces
  • Variables denote values
  • (numbers, booleans, cells, functions,
    lists, )
  • Type names (simply types)
  • Constructors (in ML)
  • . . . . .

6
  • An identifier collision
  • when same identifier used for different
    purposes in a program
  • Collisions between different name spaces often
    resolved by position
  • type bing Bing of int
  • let bing Bing(5) ( a value of type bing )
  • But, within a space, rules must be used.
  • We deal with variable space

7
Variable declarations, definitions, uses
  • Variable occurrence a variable in a specific
    position
  • Occurrences divided into
  • declaration -- introduces a variable
  • often carries additional information
    type, lifetime,
  • use an occurrence that is not a declaration
  • definition occurrence in expression whose
    elaboration at run-time associates the
    variable with a value
  • may be a use, or a declaration (most
    often the latter)

8
  • Programming languages offer a variety of
  • declaration constructs
  • definition constructs
  • These often group several declarations/definitions
    together

9
Examples in C int x a declaration
(implicit) definition (why?)
real foo(real x), int
z return xyz declares defines foo,
declares x, declares defines z, uses x, y,
z int goo(int x) (a function prototype)
declares goo, x int goo(int x)return x1
given the declaration, a definition a use of
goo
10
  • Scheme, OCAML
  • (define foo (lambda (x) ( x y)))
  • declares defines foo, declares x, uses x, y
  • 2. (let ((x 3) (y ( z 1))) ( x z)) (a
    definition construct)
  • declares defines x, y uses x, z
  • let x 5
  • declares defines x
  • 4. let foo function x ? x1
  • declares defines foo, declares x
  • 5. let x 4 and y z1 in xz
  • declares defines x, y uses x, z

11
Block, region , scope
  • Block any program construct that restricts
    visibility of variables declared in it
  • variable declared at most once in a
    block
  • Functions/procedures are abstractions ? restrict
    visibility of formal parameters ? are blocks
  • Other common blocks let, let rec, let
    (scheme), program, function body, compound
    statement (in some pls)

12
  • A program may contain several declarations of
    variables with same name (in different blocks)
    these are different variables
  • Blocks may be nested
  • Q To which variable (declaration) does a give
    use refer (is statically bound to)?

13
Examples int x
real y 0.0 real foo(real x)int
z return xyz (define foo (lambda (x) ( x
y))) The use of x is statically bound, that
of y is free
14
  • Q Why is it important to associate uses with
    declarations?
  • A This defines their meaning, their run-time
    values
  • At run-time, declared variables are dynamically
    bound to values
  • Each binding is associated with a
    declaration
  • Many bindings for a given identifier may exist
  • Static binding of a use to a declaration allows
    to select for it only the dynamic binding(s)
    associated with that declaration

Static structure determines dynamic behavior
15
  • Example
  • int n 43
  • int x 3
  • int zoo(int n)if n 0 then return 1 else
    xzoo(n-1)

16
  • Terminology
  • d(x) a declaration of x, u(x) a use
    of x
  • decl(u(x)) the declaration to which u(x) is
    statically bound
  • The rules that determine decl(u(x))
  • Block specific rules
  • A universal rule

17
  • Block specific
  • Each variable declared in a block has an
    associated
  • region (of visibility) in
    the block
  • ( a component of the block)
  • Each block-kind has specific rules that
    determine the region for each variable
    declared in it

18
  • Examples
  • 1. let x E1 in E2
  • the region of d(x) here is E2
  • 2. let rec f E1 in E2
  • the region of d(f) here is both E1, E2
  • 3. int x 5
  • int y x5
  • the region of a declared variable in a C block
    from after the declaration to end of block

19
  • Blocks and regions may be nested
  • Partial overlap not allowed ? hierarchies
  • The important one the region hierarchy
  • The universal rule
  • The scope of a declaration of x
  • its region, excluding all nested regions of
    other declarations of x

20
  • Region --- associated with block kinds
  • Scope --- determines actual visibility in given
    program, based on actual nesting
  • Region ????? ,
    scope ????
  • The discipline is called
  • static/lexical) scoping ????? ????
  • Static--- determined by program structure

21
  • Example
  • int n 43
  • int x 3
  • int zoo(int n)if n 0 then return 1 else
    xzoo(n-1)
  • The region of 1st declaration of n
  • down to end of program
  • Its scope the function expression for zoo is
    excluded

22
  • A bit more terminology
  • A declaration d(x) binds each u(x) in its
    scope
  • use bound to decl. --- a binary, functional
    relation
  • A use u(x) is bound/free in a program phrase E
    decl(u(x)) is/is not defined in E
    (two unary predicates)
  • ( local, global have similar meanings)
  • Variable x is free/bound in E, if it has a
    free/bound use in E (can have both)
  • For all add statically-

23
Common kinds of blocks
  • Function
  • Non-recursive, (possibly) parallel (collateral)
  • Regions do not contain defining expressions
  • independent definitions
  • Sequential definitions elaborated one by one,
  • each may use previously defined variables
  • regions do not contain defining expressions
  • Recursive the regions do contain the defining
    expressions
  • Comment
  • In all kinds except function, each declaration
    may be viewed as a definition (possibly to null)

24
  • Graphic illustration
  • P
  • Non-recursive, parallel block
  • let x 3, y x 5 in x y
  • Sequential block
  • let x 3, y x 5 in x y (Scheme
    let)

(pseudo-code)
(pseudo-code)
25
  • A recursive block
  • let rec factorial function n if n0 then
    1

  • else nfactorial (n-1)
  • in factorial 3
  • Another recursive block (mutual recursion)
  • let rec even function n ? if n 0 then true

  • else odd (n-1)
  • and odd function n ? if n0 then false

  • else even (n-1)
  • in even 3

26
  • Comment on recursion in C
  • Recursion of a single function by default (in
    C)
  • a recursive block
  • For mutual recursion use a sequential block,
    separate declaration from definition
  • int foo(int x) // function declaration
  • int goo(int y)foo(y-1)
  • int foo(int x).goo(x1) // function
    definition

27
  • Every block is (up syntactic variation) one of
    the four kinds
  • Example
  • for (int i 1, i, i
  • This loop construct is a block
  • Could separate the definition from the uses
  • int t (implicit definition)
  • for (i1
  • It is convenient to make blocks of various
    constructs (here a loop)
  • But, they typically fall into one of the kinds
    above

28
  • A sequential block may interleave defintions and
    expressions
  • A sequential block can be converted to nested
    parallel blocks
  • (let ((x 3) (y ( x 4))
  • ( x y))
  • ? (let ((x 3))
  • (let ((Y ( x 4))
  • ( x y)))
  • ? It can be ignored in general semantic
    discussion

29
  • Conclusions
  • Languages are very similar in terms of their
    block structure
  • It suffices to define semantics for a toy
    language with the three main kinds of blocks
Write a Comment
User Comments (0)
About PowerShow.com