SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Similar documents
Chapter 5 Names, Binding, Type Checking and Scopes

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 5. Names, Bindings, and Scopes

Programming Languages, Summary CSC419; Odelia Schwartz

Imperative Programming

Type Bindings. Static Type Binding

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

NOTE: Answer ANY FOUR of the following 6 sections:

CPSC 3740 Programming Languages University of Lethbridge. Data Types

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

Chapter 5. Variables. Topics. Imperative Paradigm. Von Neumann Architecture

Semantic Analysis. Compiler Architecture

Programming Languages Third Edition. Chapter 7 Basic Semantics

Data Types In Text: Ch C apter 6 1

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

Name, Scope, and Binding. Outline [1]

CSC 533: Organization of Programming Languages. Spring 2005

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

Topic IV. Block-structured procedural languages Algol and Pascal. References:

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Binding and Variables

Names, Bindings, Scopes

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012

Fundamentals of Programming Languages

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

6. Names, Scopes, and Bindings

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Concepts of Programming Languages

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

Short Notes of CS201

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

Chapter 8 :: Composite Types

Chapter 6. Structured Data Types. Topics. Structured Data Types. Vectors and Arrays. Vectors. Vectors: subscripts

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

CS201 - Introduction to Programming Glossary By

COSC252: Programming Languages: Basic Semantics: Data Types. Jeremy Bolton, PhD Asst Teaching Professor

CST-402(T): Language Processors

The SPL Programming Language Reference Manual

Types. What is a type?

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Informatica 3 Syntax and Semantics

CS558 Programming Languages

CS 415 Midterm Exam Spring 2002

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Principles of Programming Languages. Lecture Outline

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Programming Languages

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

Programmiersprachen (Programming Languages)

Intermediate Code Generation

Lecture 4 Memory Management

Organization of Programming Languages CS3200 / 5200N. Lecture 06

CS558 Programming Languages

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

The role of semantic analysis in a compiler

Types II. Hwansoo Han

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Chapter 6. Data Types ISBN

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

CS 230 Programming Languages

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction

Names and Abstractions: What s in a Name?

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

CS558 Programming Languages

Informatica 3 Marcello Restelli

CMSC 331 Final Exam Section 0201 December 18, 2000

CS 3360 Design and Implementation of Programming Languages. Exam 1

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Run-time Environments

Names, Scope, and Bindings

COS 140: Foundations of Computer Science

Run-time Environments

LECTURE 18. Control Flow

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Memory Management and Run-Time Systems

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Chapter 3:: Names, Scopes, and Bindings

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

The PCAT Programming Language Reference Manual

Principles of Programming Languages

22c:111 Programming Language Concepts. Fall Types I

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

UNIT-4 (COMPILER DESIGN)

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Transcription:

SE352b Software Engineering Design Tools W3: Programming Paradigms Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa SE352b: Roadmap CASE Tools: Introduction System Programming Tools Programming Paradigms Database Technology Middleware and Connectivity Tools Web Development Tools Software Tools: Special Topics Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 2

Software Tools Source Procedure A Object Module A Programming Source Language Translator Procedure B Tools Systems Object Programming Linker & Module B Loader Tools Executable Binary Code Source Procedure C Object Module C Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 3 Libraries Library header Identification Objects B Calls D,printf Entry point table External Reference table Machine instructions And constants Relocation Dictionary End of module Object A Calls B,C, printf linker Library File header Identification Entry point table External Reference table Machine instructions And constants Relocation Dictionary End of module Stub Library printf Jump Table...... start on a page boundary.align 8 ; align on 8-byte boundary for variable length insns JUMP_read: jmp _read.align 8 JUMP_write: jmp _write Shared Library printf Stub for printf Entries define the exported & imported global symbols A B C D printf E C X D...... _read:...... code for read()...... _write:...... code for write() Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 4

Principles of Programming Language. Syntax 2. Semantics 3. Data Types 4. Variables Binding Type Checking Referencing Environment (Scoping) 5. Subprogramming A Modeling Tool Abstraction & Control Tool Memory Memory 2 64 Address Data CPU CPU PC Execution Unit Clock Ticks Clock 00 0 Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 5 e.g., Pascal type declarations Example: EBNF type_identifier ( identifier ) An example grammar: constant <program> ::= <stmts> <stmts> ::= <stmt> <stmt> ; <stmts> <stmt> ::= <var> = <expr> <var> ::= a b c d <expr> ::= <term> + <term> <term> - <term> <term> ::= <var> const,.. constant a = b + const <program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 6 http://cui.unige.ch/db-research/enseignement/analyseinfo/bnfweb.html

Phases of Compilation Source Code program: Character Stream Scanner (lexical analysis) Token Stream Parse Tree Abstract syntax tree or other intermediate form Modified intermediate form Assembly or machine language, or other target language Parser (syntax analysis) Semantic analysis and Intermediate code generation Machine-independent code improvement (optional) Target code generation Machine-Specific code improvement (optional) Computation Modified target language Symbol table Machine Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 7 Lexical Analysis & Parsing Lexical Analysis Remove extraneous chars and comments Group input characters into tokens Parsing Organizes tokens into a parse tree Ensures sequence of tokens conforms to the syntax defined by the context-free grammar (CFG) CFG defining the syntax of the language A formal system that describes a language by specifying how any legal text can be derived from a distinguished symbol called the axiom, or sentence symbol. Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 8

Example: Parse Tree for: Slope*x + Intercept Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 9 Example Pascal Program program GCD (input, output); var i, j : integer begin read (i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln (i) end Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 0

Parse Tree Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa Semantic Analysis Syntax tree The important nodes of the parse tree Annotations (e.g. pointers from id s to symbol table entries) Symbol table maps each identifier (id) to the information known about it Enforces rules not captured by the CFG Generates code for run time checks Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 2

Example Pascal Program program GCD (input, output); var i, j : integer begin read (i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln (i) end Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 3 Syntax tree & Symbol table id Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 4

Principles of Programming Language. Syntax A Modeling Tool 2. Semantics 3. Data Types 4. Variables Abstraction & Control Tool 2 64 Binding Type Checking Referencing Environment (Scoping) Memory Memory Address CPU CPU PC Clock Ticks Clock 5. Subprogramming Data Execution Unit 00 0 Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 5 Data Types 2 64 Specify abstract data types Type is a set of values and operations int struct foo {int bar, baz;} Memory 00 0 Advantages Improve safety and correctness Avoid adding characters to integers Improve programming efficiency Serve as documentation Specify interfaces for separate compilation Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 6

Data Type Primitive Types Integer Floating point, Decimal Boolean Character, String type Composite or Structured types Array Sets Record Pointers Integer a word; char a byte Encapsulate data representation to define operations Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 7 Data Types: Issues Implementation Storage Components of the structure Descriptor for the specification of the structure Points to data object Operations Access of components Memory 2 64 00 0 Storage Management deals with binding location and data object e.g., dangling pointers e.g., garbage collection Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 8

Data Types: Issues(Cont.) Type Checking during compilation or during execution? Static vs. Dynamic checking Strong and weak typed Relationship with respect to safety Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 9 Array Data Type Design Issues Subscripts: type and range when subscript range bound? Two kinds of array Static S-list: array (..0) of integer; Dynamic when range and storage change get (length) declare D-list : array (.. length) of integer; Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 20

Array: Implementation & Storage Management Static Array name domain type lower subscript upper subscript Descriptor Array name domain type Dynamic Data data type size of component Data lower subscript upper subscript data type size of component location Symbol Table Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 2 Dynamic Array Type type name [size] /*index from 0 to size-*/ var a:array [low.. high] of type /*index from low to high */ Random access Calculation is effective when one part is pre-computed the other part is computed at run time loc base + (I low) * w I: index; low: lower bound index w : width or storage of each element on the array base: location of the first element in the array Associative Arrays Associate a defined index with value Great for dictionaries Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 22

Dynamic Array Implementation & Storage Management Type Vector = array [.. H] of real Var A:Vector Descriptor Vector 0005 Data Base = 0005 A[I] is 0005 +(I-LB) *w subrange.. 0 A[3] 0005+(3-) *2 0009 H real 2 bytes Base Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 23 Character and Strings Specification Fixed length Variable length to a maximum bound Unbound length Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 24

Character and Strings Fixed declared length String of 4 chars If shorter than, they are padded with blanks holds the current size variable location Variable with bound declared length holds the current size holds the limit size Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 25 Record Data Type type name = record <name_> : <type_>; typedef struct {int day, month, year;} date;... struct date {int day, month, year;}; <name_n> : <type_n>; end Operations are just on the individual data var p: name; date.day = 5; p.name_ := expression type_; (&date)->year = 997; Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 26

Record : Implementation & Storage Management Group related items similar to Cartesian product descriptor stores offset and type for each field Descriptor Record name Data No. of comp Comp. name Comp. type Comp. location Comp. n location Symbol Table Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 27 Variant Records Implementation of union (in C) Use of a tag/discriminant field implemented by overlays Pascal and Modula 2: Ada: tag assignments & tags not mandatory mandatory tags & whole record assignments X: X: record; record; A: A: string; string; B: B: array array ( (..3)..3) of of char; char; C: C: Integer; Integer; case of D:.. 2 is case of D:.. 2 is when when -> -> E: E: integer; integer; F: F: string string ; ; when when 2 2 -> -> G: G: real; real; H: H: integer; integer; end end case; case; end end record; record; need the tag to access the variant record assignments to tag could cause problems runtime check to determine correct field access Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 28

Variant Record: Implementation & Storage Management descriminant Descriptor name type Data X: X: record; record; A: A: string; string; B: B: array array ( (..3)..3) of of char; char; C: C: Integer; Integer; case case of of D: D:.... 2 2 is is when when -> -> E: E: integer; integer; F: F: string string ; ; when when 2 2 -> -> G: G: real; real; H: H: integer; integer; end end case; case; end end record; record; location value type value 2 Record descriptor Record descriptor type 2 value n type n Record descriptor Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 29 Variant Record: Implementation & Storage Management No. of comp Comp. name Comp. type Comp. location Descriptor X 4 A string X.B integer 3 char X.A length X: X: record; record; A: A: string; string; B: B: array array ( (..3)..3) of of char; char; C: C: Integer; Integer; case case of of D: D:.... 2 2 is is when when -> -> E: E: integer; integer; F: F: string string ; ; when when 2 2 -> -> G: G: real; real; H: H: integer; integer; end end case; case; end end record; record; B array X.D E C integer integer 2 integer F G string real record D Variant 2 H integer record runtime check to determine correct field access Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 30

Sets Data Type Sets data type is implemented efficiently using bits i.e., operations on sets are performed using bit operations Definition var S :set of [..2] S can be any of the following: [ ], [ ], [2 ], [, 2] Operations on Sets Union as by A+ B ; { x in A or x in B} Intersection as by A * B ; { x in A and x in B} Difference as A-B ; { x in A and x is not in B} Other operation of Super set, Sub set, equal sets, not equal sets Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 3 Sets: Implementation & Storage Management Descriptor name Data Example: type S = Set..4; var X : S; X:= [] Base type Cardinality array location base value base value 2 X Integer 4 array location T F F F 2 base value n n 3 4 Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 32

Pointers Motivation and Use abstraction of machine addresses dynamic storage allocation recursive data structures Memory 2 64 00 0 Int N; Int *A; Int B[5]; Legal operations A = B; N = A [3]; N = *(A + 3); C family Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 33 Pointers Data Type: Implementation & Storage Management Pointer Type <type referenced> * <pointer name> linktype* node Legal operations p, &p, *p Memory 2 64 00 0 p = 3 3 q = &p q p r = *q 3 r *p = 25 the location content of p (3) stores 25 25 C family Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 34

Pointers Data Type Implementation & Storage Management Dynamic allocation: Problems as memory leaks from pointer assignments. p qq memory loss unable reference p := q dispose (p) leaves q dangling Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 35 Pointers: Problems & Overheads Un-initialized pointers Dangling references Pointer arithmetic Aliasing Garbage collection solves pointer problems Pointers access Only heap in Pascal, Ada, Java Any variable in C, C++, PL/I Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 36

References Reference is a safe pointer reference to an object no pointer arithmetic no unsafe casts usually no dangling references Memory 2 64 00 0 Typical example for pointers is C Typical example for references is Java Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 37 References C++ Reference Types Constant pointers that are implicitly de-referenced Used for parameters Java Safety and readability Only references No pointer arithmetic Can only point at objects (which are all on the heap) No explicit de-allocator (garbage collection is used) means there can be no dangling references De-referencing is always implicit Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 38

Type Declaration How is a type specified? Explicit declaration Var v: integer; a program statement used for declaring the types of variables Implicit declaration a default mechanism for specifying types of variables the first appearance of the variable in the program FORTRAN, PL/I, and Perl provide implicit declarations Advantage: writability Disadvantage: reliability Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 39 Type Checking Type checking ensuring that the operands of an operator are of compatible types A compatible type A one that is either A type error legal for the operator, or allowed under language rules to be implicitly converted, by compiler generated code, to a legal type. This automatic conversion is called a coercion the application of an operator to an operand of an inappropriate type A strongly typed language: if type errors are always detected Advantage allows the detection of the misuses of variables that result in type errors Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 40

Strong Typed Languages: Pascal is not: variant records C and C++ are not: parameter type checking can be avoided; unions are not type checked Ada and Java: almost Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 4 Type Checking Static checking is done at compile time. Dynamic checking is done at runtime Advantage: Flexibility Disadvantage: Cost (late error detection) and inefficiency Static checking is preferable. A strongly typed language needs dynamic checking Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 42

Principles of Programming Language. Syntax A Modeling Tool 2. Semantics 3. Data Types 4. Variables Abstraction & Control Tool 2 64 Binding Type Checking Referencing Environment (Scoping) Memory Memory Address CPU CPU PC Clock Ticks Clock 5. Subprogramming Data Execution Unit 00 0 Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 43 Variable A variable is an abstraction of a memory cell the physical cell or collection of cells associated with a variable 2 64 Attributes of variable Name Address Value Type Lifetime Scope Memory 00 0 Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 44

Variable Names Maximum length FORTRAN I: maximum 6 COBOL: maximum 30 FORTRAN 90 and ANSI C: maximum 3 C++: no limit, but implementers often impose one Are names case sensitive? C, C++, Java, and Modula-2 names are case sensitive Disadvantage: Readability Are special words reserved words or keywords? Are connector characters allowed? Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 45 Names: Alias Two names x and y are aliased if they refer to the same object (memory location) How aliases can be created? Pointers, reference variables, Pascal variant records, C and C++ unions Some of the original justifications for aliases are no longer valid; e.g. memory reuse in FORTRAN replace them with dynamic allocation Access Access 2 Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 46

2 64 Address, Value and Type Memory Address the address of the memory at which the variable is associated Value the contents of the location with which the variable is associated Type determines the range of values of variables and the set of operations that are defined for values of that type; in the case of floating point, type also determines the precision 00 0 Var v: integer Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 47 Variable Lifetime The time during which the variable is bound to a particular memory cell Two issues Allocation getting a cell from some pool of available cells De-allocation putting a cell back into the pool When does the binding take place? Static vs. Dynamic Binding time is the time at which a binding takes place Compile time Load time e.g., bind a variable to a type in C or Java e.g., bind a FORTRAN 77 variable to a memory cell (or a C static variable) Runtime e.g., bind a non-static local variable to a memory cell Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 48

Storage Bindings The lifetime of a variable Categories Static: Objects with absolute address throughout the program s execution Stack: Subprograms calls and returns Heap: Allocation and de allocation at arbitrary times Explicit heap-dynamic Implicit heap-dynamic Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 49 Static bound to memory cells before execution begins remains bound to the same memory cell throughout execution e.g. all FORTRAN 77 variables, C static variables Advantage Efficiency, based on direct addressing history-sensitive, subprogram support Disadvantage lack of flexibility, no support for recursion Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 50

Stack-dynamic Storage bindings are created for variables when their declaration statements are elaborated variables are bound to storages when execution reaches the code to which the declaration is attached. But, data types are statically bound. i.e., stack-dynamic variables are allocated from the run-time stack e.g. local variables in Pascal and C subprograms Advantage Allows recursion Conserves storage Disadvantages Overhead of allocation and de-allocation Subprograms cannot be history sensitive Inefficient references, based on indirect addressing Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 5 Stack Allocation or Stack Dynamic Sub C Temporaries SP Local Variables Sub B Sub A SP Sub B Sub C Miscellaneous Bookkeeping Return Address Temporaries Local Arguments Variables & Returns Miscellaneous Bookkeeping Return Address Arguments & Returns FP (when Sub C is running) FP (when Sub C is running) Main Program Direction of stack Growth (usually lower addresses) Sub A (Called from main program) Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 52

Implicit Heap-dynamic Allocation and de-allocation caused by assignment statements variables are bound to heap storage only when they are assigned values e.g., all variables in APL, flex arrays in Algol 68 LIST <- 7.3 Advantage flexibility Disadvantages High cost (dynamic type checking and interpretation) Type error detection by the compiler is difficult Expensive storage management algorithms for the fragmentation Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 53 Explicit Heap-dynamic Allocated and de-allocated by explicit directives, specified by the programmer, which take effect during execution Referenced only through pointers or references Advantage: e.g., dynamic objects in C++ (via new and delete) e.g., all objects in Java int * intnode;... intnode = new int;... delete intnode; Provides for dynamic storage management Disadvantage Inefficient // allocates an int cell // deallocates the cell to which intnode points Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 54

Scope The scope rules of a language determine how references to names are associated with variables The scope of a variable the range of statements over which it is visible The non-local variables of a program unit variables that are visible but not declared there Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 55 Referencing Environment (Scoping) The referencing environment of a statement is the collection of all names that are visible in the statement In a static scoped language local variables and all visible variables in all of the enclosing scopes In a dynamic scoped language local variables and all visible variables in all active subprograms A subprogram is active if its execution has begun but has not yet terminated Scope and lifetime sometimes closely related, but are different concepts! Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 56

Static Scope Based on program text To connect a name reference to a variable, programmer (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 Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 57 Dynamic Scope Based on calling sequences of program units, not their textual layout temporal versus spatial References to variables are connected to declaration by searching back through the chain of subprogram calls that forced execution to this point Advantage: convenience Disadvantage: poor readability A B C C D D B E E A Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 58

Static vs. Dynamic Scope Example: MAIN - declaration of x SUB - declaration of x... call SUB2... SUB2... - reference to x... MAIN calls SUB SUB calls SUB2 SUB2 uses x Static scoping reference to x is to MAIN's x Dynamic scoping reference to x is to SUB's x... call SUB... Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 59 Constant Name A named constant a variable that is bound to a value only when it is bound to storage Advantages: readability and modifiability The binding of values to named constants can be either static (called manifest constants) or dynamic Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 60

Variable Initialization Initialization the binding of a variable to a value at the time it is bound to storage it is often done on the declaration statement e.g., SUM : FLOAT := 0.0; Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 6 Principles of Programming Language. Syntax A Modeling Tool 2. Semantics 3. Data Types 4. Variables Abstraction & Control Tool 2 64 Binding Type Checking Referencing Environment (Scoping) Memory Memory Address CPU CPU PC Clock Ticks Clock 5. Subprogramming Data Execution Unit 00 0 Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa 62