Part 5: Names, Bindings, Type Checking, and Scopes .

Uploaded on:
Category: Animals / Pets
5-2. Variables. A variable is a reflection of a memory cellVariables can be described by the accompanying six attributes:NameAddressValueTypeLifetimeScope. 5-3. Property 1: Names. LengthIf too short, they can't be connotativeLanguage examples:FORTRAN I: greatest 6FORTRAN 90 and ANSI C: most extreme 31Ada and Java: no restriction, and all are significantC : no restriction, however implementers frequently force oneCase
Slide 1

Section 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime Named Constants

Slide 2

Variables A variable is a reflection of a memory cell Variables can be described by the accompanying six traits: Name Address Value Type Lifetime Scope

Slide 3

Attribute 1: Names Length If too short, they can\'t be demonstrative Language cases: FORTRAN I: most extreme 6 FORTRAN 90 and ANSI C: greatest 31 Ada and Java: no restriction, and all are huge C++: no restriction, however implementers frequently force one Case affectability Disadvantage: intelligibility (names that carbon copy are distinctive) C, C++, and Java names are case delicate The names in different dialects are not

Slide 4

Names (proceeded with) Connectors Pascal, Modula-2, and FORTRAN 77 don\'t permit Others do Special words A guide to meaningfulness; used to delimit or isolate proclamation provisos A catchphrase is a word that is unique just in specific settings, e.g., in Fortran Real VarName ( Real is an information sort took after with a name, thusly Real is a watchword) Real = 3.4 ( Real is a variable) A saved word is an exceptional word that can\'t be utilized as a client characterized name Note: not all factors have names (see Ch 6).

Slide 5

Attribute 2: Address: the memory address with which it is related A variable may have diverse locations at various circumstances amid execution, for instance, nearby factors. (see Binding). In the event that two variable names can be utilized to get to a similar memory area, they are called false names Aliases are made by means of pointers, reference factors, C and C++ unions (see Ch 6). Nom de plumes are hurtful to meaningfulness (program perusers must recall every one of them) The l-estimation of a variable is its address (left)

Slide 6

Attributes 3 & 4: Value, Type Value: the substance of the area with which the variable is related Abstract memory cell : the physical cell or gathering of cells connected with a variable The r-estimation of a variable is its esteem (right) Type : decides the scope of estimations of factors and the arrangement of operations that are characterized for estimations of that sort; on account of gliding point, sort additionally decides the accuracy (see Ch 6)

Slide 7

The Concept of Binding A coupling is an affiliation, for example, between a trait and an element, or between an operation and an image Binding time is the time at which a coupling happens. Dialect configuration time - tie administrator images to operations Language usage time - tie skimming guide sort toward a representation Compile time - tie a variable to a sort in C or Java Load time - tie a FORTRAN 77 variable to a memory cell (or a C static variable) Runtime - tie a nonstatic neighborhood variable to a memory cell

Slide 8

Static and Dynamic Binding A coupling is static on the off chance that it first happens before run time and stays unaltered all through program execution. A coupling is alert on the off chance that it first happens amid execution or can change amid execution of the program

Slide 9

Type Binding Type restricting can be static or element. On the off chance that static, the sort might be indicated by either an unequivocal or a verifiable affirmation An express revelation is a program proclamation utilized for proclaiming the sorts of factors An understood assertion is a default instrument for determining sorts of factors (the main appearance of the variable in the program) FORTRAN, PL/I, BASIC, and Perl give certain announcements Advantage: writability Disadvantage: unwavering quality (less issue with Perl, which utilizes the principal image, as $, @ to mean the sort)

Slide 10

Dynamic Type Binding Dynamic Type Binding (JavaScript and PHP) Specified through a task explanation e.g., JavaScript list = [2, 4.33, 6, 8]; list = 17.3; Advantage: adaptability Disadvantages: High cost (dynamic sort checking and elucidation) Type mistake location by the compiler is troublesome Type Inferencing (ML) (see Ch 15) Rather than by task articulation, sorts are resolved from the setting of the reference

Slide 11

Attribute 5: Lifetime The lifetime of a variable is the time amid which it is bound to a specific memory cell. Capacity Bindings Allocation - getting a phone from some pool of accessible cells Deallocation - returning a phone to the pool Four classifications: Static, stack-alert, unequivocal pile alterable, verifiable pile dynamic

Slide 12

Categories of Variables by Lifetimes Static : bound to memory cells before execution starts and stays bound to a similar memory cell all through execution, e.g., C static factors Advantages : proficiency (coordinate tending to), history-touchy subprogram bolster Disadvantage : absence of adaptability (no recursion) stack-dynamic: stockpiling ties are made for factors when execution achieves the code of revelation proclamations, e.g., neighborhood factors in C subprograms and Java techniques Advantage: permits recursion; rations stockpiling Disadvantages: Overhead of designation and deallocation Subprograms can\'t be history delicate Inefficient references (circuitous tending to)

Slide 13

Categories of Variables by Lifetimes (cont) Explicit load dynamic: Allocated and deallocated by express mandates, indicated by the software engineer, which produce results amid execution Referenced just through pointers or references, e.g. dynamic questions in C++ (through new and erase), all items in Java Advantage : accommodates dynamic stockpiling administration Disadvantage : wasteful and inconsistent Implicit load dynamic: distribution and deallocation brought about by task explanations all factors in APL; all strings and clusters in Perl and JavaScript Advantage : adaptability Disadvantages : Inefficient, on the grounds that all properties are progressive; loss of mistake discovery

Slide 14

Type Checking Type checking is the movement of guaranteeing that the operands of an administrator are of perfect sorts Generalize the idea of operands and administrators to incorporate subprograms and assignments A good sort is one that is either lawful for the administrator, or is permitted under dialect standards to be verifiably changed over to a legitimate sort This programmed transformation is known as a pressure. A sort blunder is the utilization of an administrator to an operand of an improper sort If all sort ties are static, almost all sort checking can be static If sort ties are progressive, sort checking must be rapid

Slide 15

Strong Typing A programming dialect is specifically if sort mistakes are constantly identified Advantage of solid writing : permits the location of the abuses of factors that outcome in sort mistakes Language illustrations: C and C++ are not: parameter sort checking can be maintained a strategic distance from; unions are not sort checked (see Ch6) Ada is about solid writing,

Slide 16

Type Equivalence Two ways to deal with characterize sort proportionality: name sort equality and structure sort comparability Name sort similarity implies the two factors have good sorts on the off chance that they are in a similar revelation, e.g., x, y: int; in affirmations that utilization a similar sort name Easy to execute yet very prohibitive: Subranges of number sorts are not perfect with whole number sorts Example : factors tally and record are not sort equal type indextype is 1..100; count: number; Index: indextype; Formal parameters must be an indistinguishable sort from their relating real parameters. The organized sort must be characterized just once universally (Pascal).

Slide 17

Structure Type Compatibility Structure sort similarity implies that two factors have perfect sorts if their sorts have indistinguishable structures More adaptable, however harder to execute Consider the issue of two organized sorts: Are two record sorts good in the event that they are fundamentally the same yet utilize diverse field names? Are two cluster sorts good in the event that they are the same aside from that the subscripts are distinctive? (e.g. [1..10] and [0..9]) Are two count sorts perfect if their segments are spelled in an unexpected way?

Slide 18

Attribute 6: Scope The extent of a variable is the scope of proclamations over which it is obvious The nonlocal factors of a program unit are those that are noticeable however not proclaimed there The degree principles of a dialect decide how references to names are connected with factors

Slide 19

Static Scope Based on program content To associate a name reference to a variable, you (or the compiler) must discover the statement Search handle : seek affirmations, first locally, then in progressively bigger encasing degrees, until one is found for the given name Blocks: a strategy for making static extensions inside program units- - from ALGOL 60 Examples: C and C++: for (...) { int file; ... }

Slide 21

Static Scope (proceeded with) Variables can be avoided a unit by having a "closer" variable with a similar name The most-nearly settled run: an event of a variable is in the extent of the deepest encasing piece of the variable. C++ and Ada permit access to these "hidden" factors In Ada: In C++: class_name::name

Slide 22

Dynamic Scope Based on calling arrangements of program units, not their literary format (fleeting versus spatial) References to factors are associated with revelations via looking back through the chain of subprogram calls that constrained execution to this point Evaluation of Dynamic Scoping: Advantage : accommodation Disadvantage : poor lucidness

Slide 23

Scope Example MAIN - presentation of x SUB1 - assertion of x - ... call SUB2 ... SUB2 ... - reference to x - ... ... call SUB1 … MAIN calls SU

View more...