Type Checking and Scope Type Checking Type checking
- Slides: 18
Type Checking and Scope
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.
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. )
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
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
Type Compatibility Name versus structure type compatibility. Pascal example: type t 1 = array [1. . 10] of integer; t 2 = array [1. . 10] of integer; var ar 1 : t 1; ar 2 : t 2; ar 1 and ar 2 are compatible based upon structure, but not name.
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 **
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 **
Type Compatibility Pascal - structure in most cases, name 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 can’t define types)
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.
Scope STATIC SCOPING DYNAMIC SCOPING
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)
STATIC SCOPE This reference to variable x. . . Procedure big; var x : integer; procedure sub 1; begin {sub 1} …x… end; {sub 1} procedure sub 2; var x : integer; begin {sub 2} …x… end; {sub 2} begin {big}. . . end; {big} …is resolved by this declaration
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
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
DYNAMIC SCOPE This reference to variable x. . . Procedure big; var x : integer; procedure sub 1; begin {sub 1} …x… end; {sub 1} procedure sub 2; var x : integer; begin {sub 2} …x… end; {sub 2} begin {big}. . . end; {big} Calling sequence: big calls sub 2 calls sub 1 …is resolved by this declaration
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
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
- Type checker in compiler design
- Scope management pmp
- Use case diagram
- What is type checking in compiler design
- Type checking in compiler design
- Sdt for type checking
- Type checking in compiler design
- Verification type
- Discuss the nature of educational psychology
- Multiple look and feel standards
- Checking account and debit card simulation
- 2-1 checking accounts answers
- Checking account and debit card simulation answers
- Understanding checking and debit accounts assignment
- Checking and corrective action
- Checking and corrective action
- Chapter 9 checking accounts and banking services
- 3 1 checking accounts
- Checking accounts 101