Imperative Language Design Issues - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Imperative Language Design Issues

Description:

Imperative Language Design Issues Imperative languages are abstractions of von Neumann architecture Memory Processor Variables characterized by attributes – PowerPoint PPT presentation

Number of Views:138
Avg rating:3.0/5.0
Slides: 39
Provided by: Sherr195
Category:

less

Transcript and Presenter's Notes

Title: Imperative Language Design Issues


1
Imperative Language Design Issues
  • Imperative languages are abstractions of von
    Neumann architecture
  • Memory
  • Processor
  • Variables characterized by attributes
  • To design a type, must consider scope, lifetime,
    type checking, initialization, and type
    compatibility

2
Names
  • Names
  • Naming conventions
  • Hungarian Notation
  • Case sensitive
  • Maximum length
  • Special words
  • Keywords
  • Reserved words
  • Pre-defined identifiers (true, false)

3
program confusing const true false begin
if (a lt b) true then f(a) else
g(a) end.
4
Names (continued)
  • Length
  • If too short, they cannot be connotative
  • Language examples
  • FORTRAN I maximum 6
  • COBOL maximum 30
  • FORTRAN 90 and C89 maximum 31
  • C99 maximum 63
  • C, Ada, and Java no limit, and all are
    significant
  • C no limit, but implementers often impose one
  • Recent versions of MSVC will allow up to 247
    significant characters for C identifiers and 2048
    characters for C identifiers.
  • Some of the longest Windows API names
  • GetVolumePathNamesForVolumeName, 31 characters
    long. GetVolumeNameForVolumeMountPoint, 32
    characters long. CoMarshalInterThreadInterfaceInS
    tream, 37 characters long.

5
Names (continued)
  • Special characters
  • PHP all variable names must begin with dollar
    signs
  • Perl all variable names begin with special
    characters, which specify the variables type
  • Ruby variable names that begin with _at_ are
    instance variables those that begin with _at__at_ are
    class variables
  • Objective-C
  • class
  • - instance

6
Names (continued)
  • Case sensitivity
  • Disadvantage readability (names that look alike
    are different)
  • Names in the C-based languages are case sensitive
  • Names in others are not
  • Worse in C, Java, and C because predefined
    names are mixed case (e.g. IndexOutOfBoundsExcept
    ion)

7
Names (continued)
  • Special words
  • An aid to readability used to delimit or
    separate statement clauses
  • A keyword is a word that is special only in
    certain contexts, e.g., in Fortran
  • Real VarName (Real is a data type followed with
    a name, therefore Real is a keyword)
  • Real 3.4 (Real is a variable)
  • A reserved word is a special word that cannot be
    used as a user-defined name
  • Potential problem with reserved words If there
    are too many, many collisions occur (e.g., COBOL
    has 300 reserved words!)

8
Variables
  • Variables
  • Name
  • Address (l-value)
  • Aliases (references in C)
  • Type
  • Value
  • Scope
  • When does a name go out of scope?
  • Lifetime
  • When does a name become unavailable?
  • When does a data item gets destroyed?

9
Binding
  • Binding - association of an operation and a
    symbol.
  • Binding time - when does the association take
    place?
  • Design time
  • Compile time
  • Link time
  • Runtime

10
Binding time
  • Example
  • int count count count 5
  • Type of count is bound at compile time.
  • Value of count bound at execution time.
  • Meaning of bound at compile time.
  • Possible types for count, internal representation
    of 5 and set of possible meanings for bound at
    compiler design time.

11
Binding
  • Static
  • It occurs before runtime and remains unchanged
    throughout program execution.
  • Dynamic
  • It occurs at runtime and can change in the course
    of program execution.

12
Type Binding
  • Before a variable can be referenced in a program,
    it must be bound to a data type.
  • Two important questions to ask
  • 1. How is the type specified?
  • Explicit Declaration
  • Implicit Declaration
  • All variable names that start with the letters
    i - r are integer, real otherwise
  • _at_name is an array, something is a hash structure
  • Determined by context and value

13
Type Binding
  • 2. When does the binding take place
  • Explicit declaration (static)
  • Implicit declaration (static)
  • Determined by context and value (dynamic)
  • Dynamic Type Binding
  • When a variable gets a value, the type of the
    variable is determined right there and then.

14
Dynamic Type Binding
  • Specified through an assignment statement
  • (set x (1 2 3)) lt x becomes a list
  • (set x a) lt x becomes a single
    element
  • Advantage
  • flexibility (generic program units)
  • Disadvantages
  • 1. High cost (dynamic type checking and
    interpretation)
  • 2. Type error detection by the compiler is
    difficult

15
Type Inference
  • Rather than by assignment statement, types are
    determined from the context of the reference.
  • Some languages dont support assignment
    statements
  • Example
  • (define someFunction (m n) .... )
  • (someFunction a b) lt m becomes a single
    element
  • (someFunction (a b c) (1 2 3)) lt m becomes a
    list

16
Storage Bindings
  • Allocation
  • getting a cell from some pool of available cells
  • Deallocation
  • putting a cell back into the pool
  • The lifetime of a variable is the time during
    which it is bound to a particular memory cell.

17
Variable lifetime
  • Static
  • bound to memory cells before execution begins and
    remains bound to the same memory cell throughout
    execution.
  • Cs static variables
  • Advantage
  • efficiency (direct addressing),
  • Disadvantage
  • lack of flexibility (no recursion)

18
Variable lifetime
  • 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 Pascal and C subprograms
  • Advantage
  • allows recursion conserves storage
  • Disadvantages
  • Overhead of allocation and deallocation

19
Variable Lifetime
  • 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

20
Variable lifetime
  • Implicit heap-dynamic
  • Allocation and deallocation caused by assignment
    statements
  • e.g. Lisp variables
  • Advantage
  • flexibility
  • Disadvantages
  • Inefficient, because all attributes are dynamic
  • Loss of error detection

21
Scope
  • The scope of a variable is the range of
    statements over which it is visible.
  • The nonlocal variables of a program unit are
    those that are visible but not declared there.
  • The scope rules of a language determine how
    references to names are associated with variables.

22
Declaration Order
  • C99, C, Java, and C allow variable
    declarations to appear anywhere a statement can
    appear
  • In C99, C, and Java, the scope of all local
    variables is from the declaration to the end of
    the block
  • In C, the scope of any variable declared in a
    block is the whole block, regardless of the
    position of the declaration in the block
  • However, a variable still must be declared before
    it can be used

23
Declaration Order (continued)
  • In C, Java, and C, variables can be declared
    in for statements
  • The scope of such variables is restricted to the
    for construct

24
Global Scope
  • C, C, PHP, and Python support a program
    structure that consists of a sequence of function
    definitions in a file
  • These languages allow variable declarations to
    appear outside function definitions
  • C and Chave both declarations (just attributes)
    and definitions (attributes and storage)
  • A declaration outside a function definition
    specifies that it is defined in another file

25
Global Scope (continued)
  • PHP
  • Programs are embedded in XHTML markup documents,
    in any number of fragments, some statements and
    some function definitions
  • The scope of a variable (implicitly) declared in
    a function is local to the function
  • The scope of a variable implicitly declared
    outside functions is from the declaration to the
    end of the program, but skips over any
    intervening functions
  • Global variables can be accessed in a function
    through the GLOBALS array or by declaring it
    global

26
Global Scope (continued)
  • Python
  • A global variable can be referenced in functions,
    but can be assigned in a function only if it has
    been declared to be global in the function

27
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.

28
Evaluation of Static Scoping
  • Works well in many situations
  • Problems
  • In most cases, too much access is possible
  • As a program evolves, the initial structure is
    destroyed and local variables often become
    global subprograms also gravitate toward become
    global, rather than nested

29
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 back through the chain
    of subprogram calls that forced execution to this
    point.
  • Evaluation of Dynamic Scoping
  • Advantage convenience
  • Disadvantage hard to debug, hard to understand
    the code

30
Static Scope
  • Blocks - a method of creating static scopes
    inside program units--from ALGOL 60
  • C and C for (...)
  • int index
  • ...
  • Ada declare LCL FLOAT
  • begin
  • ...
  • end

31
Static Scope Procedures
Evaluation of Static Scoping (for nested
procedures) Consider the example MAIN
Define Sub A Define Sub C
Define Sub D Define Sub B
Define Sub E
MAIN
A
C
D
B
E
32
Evaluation of Static Scoping
  • Assume MAIN calls A and B
  • A calls C and D
  • B calls A and E

33
Static Scope Example
MAIN
MAIN
A
B
A
B
C
D
E
C
E
D
34
Static Scope
  • Suppose the spec is changed so that D must now
    access some data in B
  • Solutions
  • 1. Put D in B (but then C can no longer call it
    and D cannot access A's variables)
  • 2. Move the data from B that D needs to MAIN (but
    then all procedures can access them)
  • Same problem for procedure access!
  • Overall static scoping often encourages many
    globals

35
Static Scope Variables
  • program main
  • var x integer
  • procedure sub1
  • begin
  • print(x)
  • end sub1
  • procedure sub2
  • var x integer
  • begin
  • x 10
  • sub1
  • end
  • begin
  • x 5
  • sub2
  • end.

Output 10 or 5?
36
Dynamic Scope Variables
  • program main
  • var x integer
  • procedure sub1
  • begin
  • print(x)
  • end sub1
  • procedure sub2
  • var x integer
  • begin
  • x 10
  • sub1
  • end
  • begin
  • x 5
  • sub2
  • end.

Output 10 or 5?
37
Scope vs. Lifetime
  • Scope and lifetime are sometimes closely related,
    but are different concepts!!
  • Consider a static variable in a C or C function
  • void someFunction()
  • static int x
  • ...

38
Referencing Environments
  • The referencing environment of a statement is the
    collection of all names that are visible in the
    statement
  • In a static scoped language, that is the local
    variables plus all of the visible variables in
    all of the enclosing scopes
  • A subprogram is active if its execution has begun
    but has not yet terminated
  • In a dynamic-scoped language, the referencing
    environment is the local variables plus all
    visible variables in all active subprograms
Write a Comment
User Comments (0)
About PowerShow.com