1 / 29

Part II Procedural, block structured, languages

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.

ulric-perez
Download Presentation

Part II Procedural, block structured, languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Part IIProcedural, 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 block structure

  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 block structure

  3. Contents: • Semi-formal description of static structure • The substitution model: operational semantics for asimple functional language • Introduction to type systems • Definitions, comparison of dynamic/static typing • A simple monomorphictype 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 block structure

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

  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: denotevalues (numbers, booleans, cells, functions, lists, …) • Type names(simply: types) • Constructors (in ML) • . . . . . block structure

  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 block structure

  7. Variable declarations, definitions, uses Variableoccurrence: 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) block structure

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

  9. Examples in C: int x; a declaration & (implicit) definition (why?) real foo(real x){, int z; … return x+y+z;} 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 x+1;} given the declaration, a definition & a use of goo block structure

  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  x+1; declares & defines foo, declares x 5. let x = 4 and y = z+1 in x+z;; declares & defines x, y; uses x, z block structure

  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 pl’s) block structure

  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)? block structure

  13. Examples: int x; real y = 0.0; real foo(real x){int z; … return x+y+z;} (define foo (lambda (x) (+ x y))) The use of x is statically bound, that of y is free block structure

  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 block structure

  15. zoo 5: zoo 5: zoo 5: Example: int n = 43; ………… int x = 3; int zoo(int n){if n =0 then return 1 else x+zoo(n-1) block structure

  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 block structure

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

  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 = x+5; the region of a declared variable in a C block : from after the declaration to end of block block structure

  19. Blocks and regions may be nested Partial overlap not allowed  hierarchies The important one: theregion hierarchy The universal rule: The scopeof a declaration of x: its region, excluding allnested regions of other declarations of x block structure

  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 block structure

  21. Example: int n = 43; ………… int x = 3; int zoo(int n){if n =0 then return 1 else x+zoo(n-1) The region of 1st declaration of n: down to end of program Its scope: the function expression for zoo is excluded block structure

  22. A bit more terminology: A declaration d(x) binds each u(x) in its scope ‘use bound todecl.’ --- 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, globalhave 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- block structure

  23. Common kinds of blocks • Function • Non-recursive, (possibly) parallel(collateral) • Regions do not contain defining expressions • independent definitions • Sequential – definitions elaboratedone 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) block structure

  24. region region dec dec (pseudo-code) (pseudo-code) 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*) block structure

  25. A recursive block: let rec factorial = function n => if n=0 then 1 else n*factorial (n-1) in factorial 3 Another recursive block(mutual recursion) letrec even = function n  if n = 0 then true else odd (n-1) and odd = function n  if n=0 then false else even (n-1) in even 3;; block structure

  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(x+1)…} // function definition block structure

  27. Every block is (up syntactic variation) one of the four kinds Example: for (int i = 1, i++, i<k){ …} This loop construct is a block Could separate the definition from the uses: {int t; (implicit definition) for (i=1…… } • It is convenient to make blocks of various constructs (here a loop) • But, they typically fall into one of the kinds above block structure

  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 block structure

  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 block structure

More Related