CIS217 Concepts of Programming Languages - Chapter 5 PDF

Document Details

AccomplishedUniverse8955

Uploaded by AccomplishedUniverse8955

Tags

programming languages computer science programming concepts computer programming

Summary

This document is Chapter 5 of a course on programming languages, focusing on names, bindings, and scopes within programming languages. It covers imperative language concepts like memory, variables' attributes (name, address, type, value, lifetime, scope), types of binding (design, implementation, compile, load, runtime), static and dynamic bindings, variable initialization, introduction and overview of named constants.

Full Transcript

CIS217 CONCEPTS OF PROGRAMMING LANGUAGES CHAPTER-5 Names, Bindings, and Scopes Outline Introduction Names Variables The Concept of Binding Scope Scope and Lifetime Named Constants Summary Introduction Imperative languages are abstractions of von Neumann architecture...

CIS217 CONCEPTS OF PROGRAMMING LANGUAGES CHAPTER-5 Names, Bindings, and Scopes Outline Introduction Names Variables The Concept of Binding Scope Scope and Lifetime Named Constants Summary Introduction Imperative languages are abstractions of von Neumann architecture – Memory: stores both instructions and data – Processor: provides operations for modifying the contents of memory Variables are characterized by a collection of properties or attributes – The most important of which is type, a fundamental concept in programming languages – To design a type, must consider scope, lifetime, type checking, initialization, and type compatibility Names Design issues – The following are the primary design issues for names Maximum length? Are names case sensitive? Are special words reserved words or keywords? Name Forms – A name is a string of characters used to identify some entity in a program. – Length If too short, they cannot be connotative – Language examples: FORTRAN I: maximum 6 COBOL: maximum 30 C# and Java: no limit, and all characters are significant C++: no limit, but implementers impose a length limitation due to performance reasons Names Name Forms (Cont.) – Names in most programming languages have the same form: a letter followed by a string consisting of letters, digits, and (_). – Prior to Fortran 90, the following two names are equivalent: Sum Of Salaries // names could have embedded spaces SumOfSalaries // which were ignored – Special characters PHP: all variable names must begin with dollar signs $ (e.g. $var) Perl: all variable names begin with special characters $, @, or %, which specify the variable’s type if a name begins with $ is a scalar, if a name begins with @ it is an array, if it begins with %, it is a hash structure Ruby: variable names that begin with @ are instance variables; those that begin with @@ are class variables Names Case sensitivity – Disadvantage: readability (names that look alike are different) – Names in the C-based languages are case sensitive C, C++, and Java names are case sensitive ➔ rose, Rose, ROSE are distinct names 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. – Ex: Fortran Real Apple // Real is a data type followed with a name, therefore Real is a keyword Real = 3.4 // Real is a variable name – Disadvantage: poor readability. Compilers and users must recognize the difference. – A reserved word is a special word that cannot be used as a user-defined name. Variables A variable is an abstraction of a memory cell. Variables can be characterized as a sextuple of attributes: – Name Not all variables have names: Anonymous, heap-dynamic variables – Address The memory address with which it is associated A variable may have different addresses at different times during execution. – Type Determines the range of values of variables and the set of operations that are defined for values of that type; – Value The value of a variable is the contents of the memory cell or cells associated with the variable. – Lifetime – Scope The Concept of Binding A binding is an association, such as between an attribute and an entity, or between an operation and a symbol. Binding time is the time at which a binding takes place. Possible binding times: – Language design time: bind operator symbols to operations For example, the asterisk symbol (*) is bound to the multiplication operation. – Language implementation time: A data type such as int in C is bound to a range of possible values. – Compile time: bind a variable to a particular data type at compile time. – Load time: bind a variable to a memory cell (ex. C static variables) – Runtime: bind a non-static local variable to a memory cell. Type Bindings Static Type Bindings – If static, the type may be specified by either an explicit or an implicit declaration. – An explicit declaration is a program statement used for declaring the types of variables. – An implicit declaration is a default mechanism for specifying types of variables (the first appearance of the variable in the program.) – Both explicit and implicit declarations create static bindings to types. – Type Inference: Some languages use type inferencing to determine types of variables (context) C# - a variable can be declared with var and an initial value. The initial value sets the type var sum = 0; // sum is int var total = 0.0; // total is float var name = “Fred”; // name is string Visual Basic, ML, Haskell, and F# also use type inferencing. The context of the appearance of Type Bindings Dynamic Type Bindings – With dynamic type binding, the type of a variable is not specified by a declaration statement, nor can it be determined by the spelling of its name. Instead, the variable is bound to a type when it is assigned a value in an assignment statement. – In Python, Ruby, JavaScript, and PHP, type binding is dynamic – Specified through an assignment statement – Ex, JavaScript list = [2, 4.33, 6, 8]; ➔ single-dimensioned array list = 47; ➔ scalar variable Storage Bindings and Lifetime 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. So the lifetime of a var begins when it is bound to a specific cell and ends when it is unbound from that cell. Categories of variables by lifetimes: – Static – stack-dynamic – explicit heap-dynamic – implicit heap-dynamic Scope The scope of a variable is the range of statements in which the variable is visible. – A variable is visible in a statement if it can be referenced in that statement. – Local variable is local in a program unit or block if it is declared there. – Non-local variable of a program unit or block are those that are visible within the program unit or block but are not declared there. Static Scope – Static scoping is named because the scope of a variable can be statically determined – that is prior to execution. – This permits a human program reader (and a compiler) to determine the type of every variable in the program simply by examining its source code. – There are two categories of static scoped languages: Nested Subprograms. Subprograms that cannot be nested. Blocks introduced in ALGOL 60, allows a section of code to have its own local variables whose scope is minimized. – Such variables are stack dynamic, so they have their storage allocated when the section is entered and deallocated when the section is exited. – The C-based languages allow any compound statement (a statement sequence surrounded by matched braces) to have declarations and thereby defined a new scope. – Ex: Skeletal C function: void sub() { int count;... while (...) { int count; count ++;... }... } 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 {int x; {...... {int x; // Illegal {int x; // Illegal...... } }... int x; } } Because the scope of a declaration is the whole block, the above nested declaration of x is also illegal: Declaration Order In C++, Java, and C#, variables can be declared in for statements – The scope of such variables is restricted to the for construct void fun() {... for (int count = 0; count < 10; count++) {... }... } – The scope of count is from the for statement to the end of for its body (the right brace) 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 For example, C and C++ have both declarations and definitions of global data – A declaration outside a function definition specifies that it is defined in another file – A global variable in C is implicitly visible in all subsequent functions in the file. – A global variable that is defined after a function can be made visible in the function by declaring it to be external, as the in the following: extern int sum; Global Scope PHP – Any variable that is implicitly declared outside any function is a global variable – variables implicitly declared in functions are local variables. – The scope of global variables extends from their declarations to the end of the program but skips over any subsequent function definitions. – Global variables are not implicitly visible in any function. Global variables can be made visible in functions in their scope in two ways: If the function includes a local variable with the same name as a global, that global can be accessed through the $GLOBALS array, using the name of the global as a string literal subscript. if there is no local variable in the function with the same name as the global, the global can be made visible by including it in a global declaration statement. Global Scope PHP $day = "Monday"; $month = "January"; function calendar() { $day = "Tuesday"; global $month; print "local day is $day "; $gday = $GLOBALS['day']; print "global day is $gday "; print "global month is $month "; } calendar(); Interpretation of this code produces the following: local day is Tuesday global day is Monday global month is January Evaluation of Static Scoping Works well in many situations Problems: – In most cases, it allows more access to both variables and subprograms that is necessary – As a program evolves, the initial structure is destroyed and local variables often become global; subprograms also gravitate toward become global, rather than nested An alternative to the use of static scoping to control access to variables and subprograms is an encapsulation construct. Dynamic Scope The scope of variables in APL, SNOBOL4, and the early versions of LISP is dynamic. Perl and Common Lisp also allow variables to be declared to have dynamic scope, although the default scoping mechanism is these languages is static. Dynamic Scoping is based on calling sequences of program units, not their textual layout (temporal versus spatial) and thus the scope is determined only at run time. function big() { Consider the two different call sequences for sub2: function sub1() { var x = 7; – big calls sub2 and sub2 use x... The dynamic parent of sub2 is big. The reference is to the x } in big. function sub2() { var y = x; – big calls sub1, sub1 calls sub2, and sub2 use x... The search proceeds from the local procedure, sub2, to its } caller, sub1, where a declaration of x is found. var x = 3;... Note that if static scoping was used, in either calling sequence } the reference to x in sub2 is to big’s x. Scope and Lifetime Scope and lifetime are sometimes closely related, but are different concepts For example, In a Java method – The scope of such a variable is from its declaration to the end of the method – The lifetime of that variable is the period of time beginning when the method is entered and ending when execution of the method terminates Consider a static variable in a C or C++ function – Statically bound to the scope of that function and is also statically bound to storage – Its scope is static and local to the function, but its lifetime extends over the entire execution of the program of which it is a part Ex: C++ functions void printheader() {... The scope of sum in contained within compute function } The lifetime of sum extends over the time during which void compute() { printheader executes. int sum;... Whatever storage location sum is bound to before the call to printheader(); printheader, that binding will continue during and after the } execution of printheader. Named Constants It is a variable that is bound to a value only at the time it is bound to storage; its value cannot be change by assignment or by an input statement. Ex, Java final int LEN = 100; Advantages: readability and modifiability Variable Initialization – The binding of a variable to a value at the time it is bound to storage is called initialization. – Initialization is often done on the declaration statement. – Ex, C++ int sum = 0; int* ptrSum = &sum; char name[] = “George Washington Carver”;

Use Quizgecko on...
Browser
Browser