Principles of Programming Languages
P. S. Suryateja
Asst. Professor, CSE Dept
Vishnu Institute of Technology
Vishnu Institute of technology – Website: www.vishnu.edu.in
UNIT – 2��Names, Bindings and Scopes
Vishnu Institute of technology – Website: www.vishnu.edu.in
Names (Identifiers)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Names – Design Issues
Vishnu Institute of technology – Website: www.vishnu.edu.in
Names – Name Forms
Vishnu Institute of technology – Website: www.vishnu.edu.in
Names – Name Forms
Vishnu Institute of technology – Website: www.vishnu.edu.in
Names – Special Words
Integer Apple //Here Integer is treated as keyword
Integer = 4 //Here Integer is treated as a variable
Vishnu Institute of technology – Website: www.vishnu.edu.in
Names – Special Words (cont...)
Integer Real
Real Integer
is valid and may confuse programmers and users.
Vishnu Institute of technology – Website: www.vishnu.edu.in
Variables
Vishnu Institute of technology – Website: www.vishnu.edu.in
Variables (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Variables (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Binding
Vishnu Institute of technology – Website: www.vishnu.edu.in
Binding (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Binding (cont...)
Example (Java):
count = count + 5;
Vishnu Institute of technology – Website: www.vishnu.edu.in
Binding - Type
Vishnu Institute of technology – Website: www.vishnu.edu.in
Binding – Type (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Binding – Type (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Binding – Type (cont...)
dynamic variable-name;
Vishnu Institute of technology – Website: www.vishnu.edu.in
Storage Bindings and Lifetime
Vishnu Institute of technology – Website: www.vishnu.edu.in
Storage Bindings and Lifetime – Static Variables
Vishnu Institute of technology – Website: www.vishnu.edu.in
Storage Bindings and Lifetime – Static Variables (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Storage Bindings and Lifetime – Stack-Dynamic Variables
Vishnu Institute of technology – Website: www.vishnu.edu.in
Storage Bindings and Lifetime – Stack-Dynamic Variables (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Storage Bindings and Lifetime – Explicit Heap-Dynamic Variables
Vishnu Institute of technology – Website: www.vishnu.edu.in
Storage Bindings and Lifetime – Explicit Heap-Dynamic Variables (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Storage Bindings and Lifetime – Implicit Heap-Dynamic Variables
heights = [74, 43, 56, 76];
Vishnu Institute of technology – Website: www.vishnu.edu.in
Scope
Vishnu Institute of technology – Website: www.vishnu.edu.in
Scope – Static Scope
Vishnu Institute of technology – Website: www.vishnu.edu.in
Scope – Static Scope (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
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
Scope – Blocks
Vishnu Institute of technology – Website: www.vishnu.edu.in
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
Scope – Global Scope
Vishnu Institute of technology – Website: www.vishnu.edu.in
Scope – Global Scope (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Scope – Global Scope (cont...)
PHP example:
Vishnu Institute of technology – Website: www.vishnu.edu.in
Scope – Global Scope (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Scope – Global Scope (cont...)
Python example:
Vishnu Institute of technology – Website: www.vishnu.edu.in
Scope – Dynamic Scope
Vishnu Institute of technology – Website: www.vishnu.edu.in
Scope – Dynamic Scope (cont...)
Example:
Vishnu Institute of technology – Website: www.vishnu.edu.in
Scope – Dynamic Scope (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Referencing Environments
Vishnu Institute of technology – Website: www.vishnu.edu.in
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
Referencing Environments (cont...)
Python example:
Vishnu Institute of technology – Website: www.vishnu.edu.in
Referencing Environments (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Referencing Environments (cont...)
C example:
void sub1( )
{
int a, b;
.... <---------- 1
}
void sub2( )
{
int b, c;
.... <---------- 2
sub1( );
}
void main( )
{
int c, d;
.... <----------- 3
sub2( );
}
Vishnu Institute of technology – Website: www.vishnu.edu.in
Referencing Environments (cont...)
C example:
Sequence of calls is: main( ), sub2( ) and sub1( ).
Vishnu Institute of technology – Website: www.vishnu.edu.in
Named Constants
Vishnu Institute of technology – Website: www.vishnu.edu.in
Named Constants (cont...)
Example:
Before using named constant:
After using named constant len:
Vishnu Institute of technology – Website: www.vishnu.edu.in
Named Constants (cont...)
const int result = 2 * width + 1;
Vishnu Institute of technology – Website: www.vishnu.edu.in
UNIT – 2��Data Types
Vishnu Institute of technology – Website: www.vishnu.edu.in
Introduction
Vishnu Institute of technology – Website: www.vishnu.edu.in
Introduction (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Primitive Data Types
Vishnu Institute of technology – Website: www.vishnu.edu.in
Primitive Data Types – Numeric Types
Vishnu Institute of technology – Website: www.vishnu.edu.in
Primitive Data Types – Floating-Point
Vishnu Institute of technology – Website: www.vishnu.edu.in
Primitive Data Types – Floating-Point
Vishnu Institute of technology – Website: www.vishnu.edu.in
Primitive Data Types – Floating-Point
Adopted from “Concepts of Programming Languages – Sebesta”
Vishnu Institute of technology – Website: www.vishnu.edu.in
Primitive Data Types – Complex and Decimal
Vishnu Institute of technology – Website: www.vishnu.edu.in
Primitive Data Types – Boolean Types
Vishnu Institute of technology – Website: www.vishnu.edu.in
Primitive Data Types – Character Types
Vishnu Institute of technology – Website: www.vishnu.edu.in
Character String Types
Vishnu Institute of technology – Website: www.vishnu.edu.in
Character String Types – String Operations
Vishnu Institute of technology – Website: www.vishnu.edu.in
Character String Types – String Operations (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Character String Types – String Operations (cont...)
Example:
/[A-Za-z][A-Za-z\d]+/
Vishnu Institute of technology – Website: www.vishnu.edu.in
Character String Types – String Length
Vishnu Institute of technology – Website: www.vishnu.edu.in
Character String Types – String Length (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Character String Types – Implementation
Vishnu Institute of technology – Website: www.vishnu.edu.in
User-Defined Ordinal Types – Enumeration Types
Vishnu Institute of technology – Website: www.vishnu.edu.in
User-Defined Ordinal Types – Enumeration Types (cont...)
enum days {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
Vishnu Institute of technology – Website: www.vishnu.edu.in
User-Defined Ordinal Types – Subrange Types
type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
subtype Weekdays is Days range Mon..Fri;
subtype Index is Integer range 1..100;
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types – Arrays and Indices
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types – Arrays and Indices (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types – Subscript Bindings and Array Categories
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types – Subscript Bindings and Array Categories (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types – Subscript Bindings and Array Categories (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types – Subscript Bindings and Array Categories (cont...)
Examples:
Array Type | Example Languages |
Static arrays | Static arrays in functions in C and C++ |
Fixed stack-dynamic arrays | Non-static arrays in functions in C and C++ |
Stack-dynamic arrays | Arrays in Ada |
Fixed heap-dynamic arrays | C and C++ (using malloc and free), Java and C# |
Heap-dynamic arrays | C#, Java (generic arrays) |
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types - Initialization
Ex:
int list[] = {1, 2, 3, 4};
char name[] = “ramesh”;
char *names[] = {“dinesh”, “ramesh”}
List : array (1..5) of Integer := (1, 3, 5, 7, 9);
Bunch : array (1..5) of Integer := (1 => 17, 3 => 34,
others => 0);
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types- Operations
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types – Operations (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types – Rectangular and Jagged Arrays
Example: myArray[3][5]
Example: myArray[3,5]
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types – Slices
vector = [2, 4, 6, 8, 10, 12, 14, 16]
mat = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
vector[3:6] gives 8, 10, 12
mat[1] gives 4,5,6
mat[0][0:2] gives 1,2
vector[0:7:2] gives 2,6,10,14
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types – Slices (cont...)
@list[1..5] = @list2[3, 5, 7, 9, 13];
(elements with indices 3, 5, 7, 9 and 13 from list2 are stored in list1)
list = [2, 4, 6, 8, 10]
list.slice(3) gives 8
list.slice(2,2) gives 6,8
list.slice(1..3) gives 4,6,8
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types – Implementation of Array Types
address(list[k]) = address(list[0]) + k * element_size
Adopted from “Concepts of Programming Languages – Sebesta”
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types – Implementation of Array Types (cont...)
n is number of elements in the row.
Vishnu Institute of technology – Website: www.vishnu.edu.in
Array Types – Implementation of Array Types (cont...)
Adopted from “Concepts of Programming Languages – Sebesta”
Vishnu Institute of technology – Website: www.vishnu.edu.in
Associative Arrays
Vishnu Institute of technology – Website: www.vishnu.edu.in
Associative Arrays – Structure and Operations
%salaries = ("Gary" => 75000, "Perry" => 57000, "Mary" => 55750, "Cedric" => 47850);
$salaries{"Perry"} = 58850;
delete $salaries{"Gary"};
@salaries = ();
Vishnu Institute of technology – Website: www.vishnu.edu.in
Associative Arrays – Structure and Operations (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Record Types
Vishnu Institute of technology – Website: www.vishnu.edu.in
Record Types (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Record Types – Definitions of Records
Vishnu Institute of technology – Website: www.vishnu.edu.in
Record Types – Definitions of Records (cont...)
Records in COBOL:
- Employee-Record and Employee-name are records and FIRST, MIDDLE, LAST, HOURLY-RATE are fields.
Adopted from “Concepts of Programming Languages – Sebesta”
Vishnu Institute of technology – Website: www.vishnu.edu.in
Record Types – Definitions of Records (cont...)
Records in Ada:
Adopted from “Concepts of Programming Languages – Sebesta”
Vishnu Institute of technology – Website: www.vishnu.edu.in
Record Types – References to Record Fields
In COBOL:
Syntax:
field_name OF record_name_1 OF . . . OF record_name_n
Example:
MIDDLE OF EMPLOYEE-NAME OF EMPLOYEE-RECORD
In Ada:
Employee_Record.Employee_Name.Middle
Vishnu Institute of technology – Website: www.vishnu.edu.in
Record Types – References to Record Fields (cont...)
1. MIDDLE OF EMPLOYEE-NAME OF EMPLOYEE-RECORD
2. Employee_Record.Employee_Name.Middle
Vishnu Institute of technology – Website: www.vishnu.edu.in
Record Types – Implementation
Adopted from “Concepts of Programming Languages – Sebesta”
Vishnu Institute of technology – Website: www.vishnu.edu.in
Tuple Types
Ex: myTuple = (3, 5.8, 'apple')
myTuple[1] gives 3 as first index of a tuple is 1
#1 (myTuple) gives 3
Vishnu Institute of technology – Website: www.vishnu.edu.in
Tuple Types (cont...)
Ex:
let tup = (3, 5, 7);;
let a, b, c = tup;;
In the above statement, 3 is assigned to a, 5 is assigned to b and 7 is assigned to c.
Vishnu Institute of technology – Website: www.vishnu.edu.in
List Types
(A B C D)
Nested List:
(A (B C) D)
Vishnu Institute of technology – Website: www.vishnu.edu.in
List Types (cont...)
Ex: (CAR ‘(A B C)) returns A
The quote tells the interpreter that (A B C) is not a call to the function A.
CDR function returns the rest of the list elements except the first element.
Ex: (CDR ‘(A B C)) gives (B, C)
Vishnu Institute of technology – Website: www.vishnu.edu.in
List Types (cont...)
Ex: (CONS 'A '(B C)) gives (A B C)
Ex: (LIST 'A 'B '(C D)) returns (A B (C D))
Vishnu Institute of technology – Website: www.vishnu.edu.in
List Types (cont...)
Ex: [5, 7, 9]
Ex: 3 :: [5, 7, 9] gives [3, 5, 7, 9]
hd [5, 7, 9] gives 5
tl [5, 7, 9] gives [7, 9]
[5; 7; 9]
Vishnu Institute of technology – Website: www.vishnu.edu.in
List Types (cont...)
Ex: myList = [3, 5.8, "grape"]
myList[1] gives 5.8 as list index starts with 0 in Python.
del myList[1] removes the second element in the list.
Vishnu Institute of technology – Website: www.vishnu.edu.in
List Types (cont...)
[x * x for x in range(12) if x % 3 == 0]
range(12) creates an array with values 0 to 11 and square all the numbers which are exactly divisible by 3. The result list is:
[0, 9, 36, 81]
Vishnu Institute of technology – Website: www.vishnu.edu.in
List Types (cont...)
[n * n | n <- [1..10]]
let myArray = [|for i in 1 .. 5 -> (i * i) |];;
Vishnu Institute of technology – Website: www.vishnu.edu.in
Union Types
Vishnu Institute of technology – Website: www.vishnu.edu.in
Union Types – Ada Union Types
Vishnu Institute of technology – Website: www.vishnu.edu.in
Union Types – Ada Union Types (cont...)
Example:
Form is tag or
discriminant
Vishnu Institute of technology – Website: www.vishnu.edu.in
Union Types – Ada Union Types (cont...)
Consider the following varaibles:
Figure_1 is unconstrained variable
Figure_2 is constraint variant variable
Type of Figure_1 can change based on the assignment shown above.
Vishnu Institute of technology – Website: www.vishnu.edu.in
Union Types – Ada Union Types (cont...)
Adopted from “Concepts of Programming Languages – Sebesta”
Vishnu Institute of technology – Website: www.vishnu.edu.in
Union Types – F# Union Types
Example:
intReal is the union type. IntValue and RealValue are constructors. Values of type intReal can be created using the constructors as if they were a function as show below:
Vishnu Institute of technology – Website: www.vishnu.edu.in
Union Types – F# Union Types (cont...)
To display the type of the intReal union, the following function could be used:
The following lines show calls to this function and the output:
Vishnu Institute of technology – Website: www.vishnu.edu.in
Union Types – Implementation
Vishnu Institute of technology – Website: www.vishnu.edu.in
Union Types – Implementation (cont...)
Compile-time Descriptor:
Adopted from “Concepts of Programming Languages – Sebesta”
Vishnu Institute of technology – Website: www.vishnu.edu.in
Pointer and Reference Types
Vishnu Institute of technology – Website: www.vishnu.edu.in
Pointer and Reference Types (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Pointer and Reference Types – Pointer Operations
Vishnu Institute of technology – Website: www.vishnu.edu.in
Pointer and Reference Types – Pointer Problems
Vishnu Institute of technology – Website: www.vishnu.edu.in
Pointer and Reference Types – Pointer Problems (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Pointers in Ada
Vishnu Institute of technology – Website: www.vishnu.edu.in
Pointers in C and C++
Vishnu Institute of technology – Website: www.vishnu.edu.in
Reference Types
Vishnu Institute of technology – Website: www.vishnu.edu.in
Type Checking
Vishnu Institute of technology – Website: www.vishnu.edu.in