Names, Bindings, and Scopes in Programming Languages

principles of programming languages n.w
1 / 125
Embed
Share

Explore the essential concepts of names, bindings, and scopes in programming languages, covering identifiers, design issues, name forms, special words, and more. Gain insights into how names are defined, used, and managed within the context of programming.

  • Programming Languages
  • Names
  • Bindings
  • Scopes
  • Identifiers

Uploaded on | 3 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Principles of Programming Languages P. S. Suryateja Asst. Professor, CSE Dept Vishnu Institute of Technology Vishnu Institute of technology Website: www.vishnu.edu.in

  2. UNIT 2 Names, Bindings and Scopes Vishnu Institute of technology Website: www.vishnu.edu.in

  3. Names (Identifiers) Attributes of variables: Name Type Value Address Scope Lifetime Vishnu Institute of technology Website: www.vishnu.edu.in

  4. Names Design Issues Design issues for names are: Are names case sensitive? Are the special words of the language reserved words or keywords? Vishnu Institute of technology Website: www.vishnu.edu.in

  5. Names Name Forms A name is a string of characters used to identify some entity in a program. Language restrictions: Fortran 95+ allows 31 characters C99 has no limitation on length of internal names and 31 characters limitation on external names (names used outside all functions) C++, Java, C# and Ada have no limitation on size of names. Vishnu Institute of technology Website: www.vishnu.edu.in

  6. Names Name Forms In most of the programming languages, names have the some form, begins with a letter, followed by any no. of letters or digits or underscores. In C, underscores are replaced with camel notation. (Ex: myStack) In PHP, variable names must begin with $. In Perl, $, @ or % at the beginning of a variable name specifies different types. In Ruby, @ or @@ at the beginning of a variable name refer to instance variable and class variable respectively. Vishnu Institute of technology Website: www.vishnu.edu.in

  7. Names Special Words Special words are names which have pre-defined special actions allotted to them. A keyword is a word of a programming language, which is special only in some contexts. In Fortran, Integer can be treated differently: Integer Apple //Here Integer is treated as keyword Integer = 4 //Here Integer is treated as a variable A reserved word is a special word which cannot be used as a name. Vishnu Institute of technology Website: www.vishnu.edu.in

  8. Names Special Words (cont...) Reserved words are better than keywords in terms of readability. For example in Fortran: Integer Real Real Integer is valid and may confuse programmers and users. Too many reserved words might effect writability. For example, COBOL includes 300 reserved words. Vishnu Institute of technology Website: www.vishnu.edu.in

  9. Variables A variable is an abstraction of a computer memory cell or collection of cells. A variable can be characterized as a collection of six attributes: name, value, address, type, scope and lifetime. Most variables have names and some do not. Vishnu Institute of technology Website: www.vishnu.edu.in

  10. Variables (cont...) The address of a variable is the machine memory address with which it is associated. The address of a variable is called its l-value in an assignment statement. When more than one variable name can be used to access the same memory location, such variables are called as aliases. Aliases can be created in different ways. In C and C++ aliases are created using pointers or unions. Vishnu Institute of technology Website: www.vishnu.edu.in

  11. Variables (cont...) The type of a variable determines the range of values that a variable can hold and the operations allowed on it. The value of a variable is the contents of the memory cell or cells associated with the variable. The variable s value is sometimes called its r- value as it is required when the name of a variable appears in the right hand side of an assignment statement. Vishnu Institute of technology Website: www.vishnu.edu.in

  12. Binding A binding is an association between an entity and its attribute. Binding between a variable and its type or value Binding between an operator and its operation The time at which binding takes place is known as binding time. Binding can take place at language design time, language implementation time, compile time, run time, link time or load time. Binding is said to be static if it occurs before run time and remains unchanged throughout program execution. If the binding occurs during run time, or it can change during program execution, such binding is said to be dynamic. Vishnu Institute of technology Website: www.vishnu.edu.in

  13. Binding (cont...) Some examples of binding times: The asterisk symbol (*) is bound to the multiplication operation at language design time. A data type like int is bound to its range at language implementation time. A variable is bound to its type in Java at compile time. A variable may be bounded to a storage cell at load time. A call to a library subprogram is bound to the subprogram code at link time. Vishnu Institute of technology Website: www.vishnu.edu.in

  14. Binding (cont...) Example (Java): count = count + 5; Type of count is bound at compile time. Range of values for count is bound at compiler design time. Meaning of operator + is bound at compile time, when the types of its operands have been determined. Internal representation of literal 5 is bound at compiler design time. Value of count is bound at execution time. Vishnu Institute of technology Website: www.vishnu.edu.in

  15. Binding - Type Type binding that takes place during compile time is known as static type binding. Type can be specified through explicit declaration or through implicit declaration. An explicit declaration declares the variables along with their types. An implicit declaration associates variables with types through default conventions, rather through declarations. Vishnu Institute of technology Website: www.vishnu.edu.in

  16. Binding Type (cont...) Implicit variable type binding is done by the language processor, either a compiler or interpreter. Implicit declaration examples: In Fortran, if the identifier begins with I, J, K, L, M, or N, or their lowercase versions, it is implicitly declared to be of Integer type. Otherwise, Real type. In Perl, a name that begins with $ is a scalar, which can store a string or a numeric value. If the name begins with @, it is an array. If the name begins with % , it is a hash structure. Implicit declarations can also be based on context. This is sometimes called as type inference. In C#, var sum = 0 declares sum as of type int. Here, the context is the value being assigned to the variable. Visual Baisc 9.0+, Go, and functional languages like ML, Haskell, OCaml and F# use type inferencing. Vishnu Institute of technology Website: www.vishnu.edu.in

  17. Binding Type (cont...) Type binding that takes place at run time is known as dynamic type binding. In dynamic type binding, a variable is bound to a type when it is assigned a value. The type of a variable is temporary in dynamic type binding. Dynamic binding provides more programming flexibility. Program to process numeric data (int, float etc...) Python, Ruby, JavaScript and PHP support dynamic type binding. Vishnu Institute of technology Website: www.vishnu.edu.in

  18. Binding Type (cont...) Option of dynamic type binding was introduced in C# (2010). A variable can be declared to use dynamic typing as follows: dynamic variable-name; In Ruby, all variables are references and do not have any type. A variable can refer any object. Disadvantages of dynamic type binding: Less readability as the error-detection capability of a compiler is diminished. Cost is high. Type checking must be done at run time. In general, languages supporting dynamic type binding are processed by an interpreter. Vishnu Institute of technology Website: www.vishnu.edu.in

  19. Storage Bindings and Lifetime Process of binding a variable to a memory cell is called as allocation and unbinding a variable from its memory cell is called as deallocation. The lifetime of a variable is the time during which the variable is bound to a specific memory location. Vishnu Institute of technology Website: www.vishnu.edu.in

  20. Storage Bindings and Lifetime Static Variables Static variables are those that are bound to a memory cell before program execution and remain bound to the same memory cell until the program terminates. Global variables are static variables. Subprograms can have local static variables which are history sensitive. Advantage of static variables is efficiency. They can be addressed directly. No run time overhead for allocation and deallocation. Vishnu Institute of technology Website: www.vishnu.edu.in

  21. Storage Bindings and Lifetime Static Variables (cont...) One disadvantage of static variables is reduced flexibility. Static variables cannot be used for recursive subprograms. Another disadvantage is memory cannot be shared. C and C++ allows static specifier for variables inside functions. In C++, Java, and C#, static implies that the variable is a class variable. Class variables are created before the class is instantiated. Vishnu Institute of technology Website: www.vishnu.edu.in

  22. Storage Bindings and Lifetime Stack-Dynamic Variables Stack-dynamic variables are those whose storage bindings are created when their declaration statements are elaborated, but whose types are statically bound. Elaboration refers to the storage allocation and binding process indicated by the declaration and occurs at run time. Variable declarations in a Java method are elaborated when the method is called and is done at run time. The variables are allocated on run time stack. Vishnu Institute of technology Website: www.vishnu.edu.in

  23. Storage Bindings and Lifetime Stack-Dynamic Variables (cont...) Advantage of stack-dynamic variables is, they are suitable for recursive sub programs. Disadvantages: Runtime overhead of allocation and deallocation. Slower access due to indirect addressing. Subprograms cannot be history sensitive. In C++, Java, and C#, by default all the variables in a method are stack- dynamic. In Ada, all non-heap variables defined in sub programs are stack-dynamic. Vishnu Institute of technology Website: www.vishnu.edu.in

  24. Storage Bindings and Lifetime Explicit Heap-Dynamic Variables Explicit heap-dynamic variables are nameless (abstract) memory cells that are allocated and deallocated by explicit run time instructions. These variables available in the heap are accessed through pointers or references. In C++, the allocation operator, new is used to create a dynamic variable which is allocated on the heap. Java objects are explicitly heap dynamic and are accessed through references. Pointers are included in C# to allow C# components to interoperate with C and C++ components. Vishnu Institute of technology Website: www.vishnu.edu.in

  25. Storage Bindings and Lifetime Explicit Heap-Dynamic Variables (cont...) Explicit heap-dynamic variables are often used to create dynamic data structures like linked lists and trees. Disadvantages: Difficulty of using pointers and references correctly. Aliasing issues. Complexity of storage management implementation. Vishnu Institute of technology Website: www.vishnu.edu.in

  26. Storage Bindings and Lifetime Implicit Heap-Dynamic Variables Implicit heap-dynamic variables are bound to heap storage only when they are assigned values. Consider the following example in JavaScript: heights = [74, 43, 56, 76]; Advantage of implicit heap-dynamic variables is that they have the highest degree of flexibility in writing generic programs. Disadvantages: Run time overhead for maintaining all dynamic attributes. Loss of error detection by the compiler. Vishnu Institute of technology Website: www.vishnu.edu.in

  27. 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. The scope rules of a language determine how a particular occurrence of a name is associated with a variable or an expression. A variable is local in a program unit or block if it is declared in it. Vishnu Institute of technology Website: www.vishnu.edu.in

  28. Scope Static Scope ALGOL 60 introduced the concept of binding names to non-local variables called static scoping. In static scoping the scope of a variable can be determined statically (before execution). There are two categories of static-scoped languages: those in which sub programs can be nested and those in which sub programs cannot be nested. Ada, JavaScript, Common LISP, Scheme, Fortran 2003+, F# and Python allow nested sub programs. C-based languages do not allow nested sub programs. Vishnu Institute of technology Website: www.vishnu.edu.in

  29. Scope Static Scope (cont...) In a static-scoped language, when the reader of a program finds a reference to a variable, the attributes of a variable can be found through the following process: If a reference for variable x is found in sub program sub1, the correct declaration for variable x is searched in the definition of sub1. If declaration for x is not found in sub1, the search continues in the parent sub program for sub1. This continues until the declaration for x is found or all the static ancestors have been exhausted. Vishnu Institute of technology Website: www.vishnu.edu.in

  30. Scope Static Scope (cont...) JavaScript example: The reference of x in sub2 is to the x declared in big. The search for x begins within sub2 and as it is not there, it is searched in its ancestor, big. Vishnu Institute of technology Website: www.vishnu.edu.in

  31. Scope Blocks ALGOL 60 introduced the concept of blocks. A block is a compound statement which contains a set of statements enclosed by matching braces. A block can have its own local variables. Such variables are typically stack dynamic. In case of nested bocks, the scope rules are same as for nested sub programs. Vishnu Institute of technology Website: www.vishnu.edu.in

  32. Scope Blocks (cont...) C example: In the above code, count of while loop is different from count of sub function. The count variable inside while loop hides the count variable in the enclosing scope. Above code is legal in C and C++ and illegal in Java and C#. Vishnu Institute of technology Website: www.vishnu.edu.in

  33. Scope Global Scope Variables defined outside functions are known as global variables. C, C++, PHP, Python, and JavaScript support global variables. Declaration of a variable specifies type and other attributes but not storage. Storage is allocated when the variable is defined. A global variable in C is implicitly visible in all subsequent functions. A global variable that is defined after a function can be made visible in the function by declaring it to be external. Vishnu Institute of technology Website: www.vishnu.edu.in

  34. Scope Global Scope (cont...) In C++, a global variable that is hidden by a local variable with same name can be accessed using the scope operator (::). In PHP, variables declared implicitly outside functions are global variables. Global variables are not implicitly visible to functions. Global variables in PHP can be made visible in functions using two ways: If the function includes a local variable with the same name as the global, it can be accessed using the $GLOBALS array. If there is no local variable, the global can be accessed by making it a part of the global declaration statement. Vishnu Institute of technology Website: www.vishnu.edu.in

  35. Scope Global Scope (cont...) PHP example: Vishnu Institute of technology Website: www.vishnu.edu.in

  36. Scope Global Scope (cont...) Global variables in JavaScript are same as in PHP, except there is no way to access a global if there is a local with the same name. In Python, global variables can be accessed inside functions but cannot be assigned new values. New values can be assigned only when it is declared as global in the function. Vishnu Institute of technology Website: www.vishnu.edu.in

  37. Scope Global Scope (cont...) Python example: Vishnu Institute of technology Website: www.vishnu.edu.in

  38. Scope Dynamic Scope Dynamic scoping is based on the calling sequence of sub programs. APL, SNOBOL, LISP, Perl and Common LISP support dynamic scoping. Scope is determined at run time. Vishnu Institute of technology Website: www.vishnu.edu.in

  39. Scope Dynamic Scope (cont...) Example: -If the sequence of calls to sub programs is big, sub1 and sub2, x in sub2 refers to x in sub1. -If the sequence of calls to sub programs is big and sub2, x in sub2 refers x in big. Vishnu Institute of technology Website: www.vishnu.edu.in

  40. Scope Dynamic Scope (cont...) Problems with dynamic scoping: Local variables of a sub program are visible to all other sub programs. Inability to type check references to non-locals statically. Makes programs less readable. Access to non-local variables in dynamic scoped languages takes more time than in static scoped languages. Vishnu Institute of technology Website: www.vishnu.edu.in

  41. Referencing Environments The referencing environment of a statement is the set of all variables that are visible in the statement. The referencing environment of a statement in a static- scoped language is the variables declared in its local scope plus the set of variables of its ancestor scope that are visible. In Python, the referencing environment of a statement includes all the local variables and all the variables declared in the functions in which the statement is nested. Vishnu Institute of technology Website: www.vishnu.edu.in

  42. Referencing Environments (cont...) Python example: g = 3; #A global def sub1( ): a = 5; b = 7; ... <------ 1 def sub2( ): global g; #Global g is accessible here c = 9; ... <-------- 2 def sub3( ): nonlocal c; #Makes non-local c visible here g = 11; ... <-------- 3 Vishnu Institute of technology Website: www.vishnu.edu.in

  43. Referencing Environments (cont...) Python example: Vishnu Institute of technology Website: www.vishnu.edu.in

  44. Referencing Environments (cont...) The referencing environment of a statement in dynamically scoped language is the local variables, plus the variables of all other active sub programs. Recent sub program activations can have variables that are hidden (which have the same name as local variables). Vishnu Institute of technology Website: www.vishnu.edu.in

  45. Referencing Environments (cont...) C example: void sub1( ) { } void sub2( ) { } void main( ) { } int a, b; .... <---------- 1 int b, c; .... <---------- 2 sub1( ); int c, d; .... <----------- 3 sub2( ); Vishnu Institute of technology Website: www.vishnu.edu.in

  46. Referencing Environments (cont...) C example: Sequence of calls is: main( ), sub2( ) and sub1( ). Vishnu Institute of technology Website: www.vishnu.edu.in

  47. Named Constants A named constant is a variable that is bound to a value only once. Named constants aids readability: The name pi can be used instead of the constant value 3.14159265. Can be used to parameterize the programs (ex: processing fixed number of data values) Vishnu Institute of technology Website: www.vishnu.edu.in

  48. Named Constants (cont...) Example: Before using named constant: After using named constant len: Vishnu Institute of technology Website: www.vishnu.edu.in

  49. Named Constants (cont...) Ada and C++ allow dynamic binding of values to named constants i.e., expressions can be specified as values as shown below: const int result = 2 * width + 1; Java also supports dynamic binding for named constants through final keyword. C# supports two kinds of named constants: const: These named constants are statically bound to values. readonly: These named constants are dynamically bound to values. Vishnu Institute of technology Website: www.vishnu.edu.in

  50. UNIT 2 Data Types Vishnu Institute of technology Website: www.vishnu.edu.in

Related


More Related Content