Type Checking and Scope PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: Type Checking and Scope


1
Type Checkingand Scope
2
Type Checking
Type checking ensures that the operands of a
given operator are of compatible types.
A compatible type is one that is legal for the
operator, or one which can be implicitly
converted to a legal type.
This implicit conversion is called coercion.
3
Strong Typing
A strongly typed language is one in which each
name in the program has a single type associated
with it, and that type is known at compile time.
(The author further defines a strongly typed
language as one in which type errors can always
be detected.)
4
Strong Typing
Pascal - nearly strongly typed, except variant
records FORTRAN - not strongly typed,
EQUIVALENCE statement, subprogram parameters,
function return values C and C - not strongly
typed, allow function parameters not to be type
checked
5
Type Compatibility
Name type compatibility- compatible if they
occur in the same declaration or have the same
type name Structure type compatibility -
compatible if their types have identical
structures
6
Type Compatibility
Name versus structure type compatibility- Pascal
example type t1 array 1..10 of
integer t2 array 1..10 of integer var
ar1 t1 ar2 t2 ar1 and ar2 are
compatible based upon structure, but not name.
7
Type Compatibility
Name type compatibility- compatible if they
occur in the same declaration or have the
same type name easy to implement - highly
restrictive - difficulties for parameter
passing
8
Type Compatibility
Structure type compatibility - compatible if
their types have identical structures more
flexible than name type compatibility - more
difficult to implement - disallows
differentiating between types with the same
structure
9
Type Compatibility
Pascal - structure in most cases, name for formal
parameters C - structural equivalence for all
types except structures (records and
unions) C - name equivalence (typedef does not
define a new type but a new name for an existing
type) FORTRAN and COBOL - structural equivalence
(since users cant define types)
10
Scope
A variable is visible in a statement if it can be
referenced in that statement The range of
statements in which the variable is visible may
be referred to as the scope of that
variable. The scope rules of a language
determine how a particular occurrence of a name
is associated with a variable.
11
Scope
STATIC SCOPING
DYNAMIC SCOPING
12
Scope
STATIC SCOPING
  • Scope of a variable can be statically
  • determined (at compile time)
  • Generally associated with program unit
  • definitions and their physical locations in
    the
  • source file
  • If a non-local variable is referenced, the
    variable
  • is searched for in the declarations of the
  • procedure in which that subprogram is declared
  • (its static parent)

13
STATIC SCOPE
14
Scope
STATIC SCOPING
  • programmer may attempt to call subprograms
  • which are not callable (not visble)
  • more data is accessible than is necessary
  • use of global variables adversely affect the
  • readability of programs
  • getting around the restrictions of static
    scoping
  • may result in awkward code organization
  • non local variables can be resolved at compile
    time
  • access to non local variables is relatively fast

15
Scope
DYNAMIC SCOPING
  • Scope of a variable is determined at run time
  • Based on the calling sequence of the
  • subprograms, not their spatial relationship to
  • one another
  • If a non-local variable is referenced, the
    variable
  • is searched for in the declarations of the
  • procedure from which that subprogram was
  • called

16
DYNAMIC SCOPE
Calling sequence big calls sub2 sub2 calls sub1
17
Scope
DYNAMIC SCOPING
  • local variables of a subprogram are visible to
    any
  • other executing subprogram. less reliable
  • programs
  • inability to statically type check
  • less readable, calling sequence must be know to
  • identify non-local variables
  • access to non local variables is slow
  • eliminates need for most parameters

18
Referencing Environments
The referencing environment is the collection of
all names (variables, subprograms, etc) that are
visible in the statement
In a statically scoped language, the referencing
environment includes - locally declared
variables - all variables declared in its
ancestor scopes
In a dynamically scoped language, the referencing
environment includes - locally declared
variables - variables of all other subprograms
which are currently active
Write a Comment
User Comments (0)
About PowerShow.com