Download Programming Languages: Variables, Types, and Data Structures - Prof. Abdelghani Bellaachia and more Study notes Computer Science in PDF only on Docsity!
Basics of Programming Languages
- DEFINITIONS......................................................................
- BUILT-IN TYPES AND PRIMITIVE TYPES..................
- DATA AGGREGATES AND TYPE CONSTRUCTORS
- CONSTRUCTORS
- TYPE COMPATIBILITY
- TYPE CONVERSION........................................................
- TYPE AND SUBTYPES
- GENERIC TYPES..............................................................
- MONOMORPHIC VERSUS POLYMORPHIC
- SCOPE.............................................................................
1. Definitions
- Attributes : o It is a set of properties used to describe a program entity, e.g., variable and function. o Example: Array: Name, Element type, Index type, Index lower bound, Index upper bound, and address. Variable: Name, Type, and Value. Variables - Most conventional programming languages can be viewed as abstraction of an underlying Von Neumann architecture: - Memory cell with and address and a value. - Five Semantic Attributes of Variables In general, the semantics of variables in programming languages is often described in terms of four attributes. o Name : It is the name used to refer to the variable. o Type : A description of the set of permissible values for a variable. o Scope : The region of program text over which a variable is known. o l-value (or location): A location in memory associated with the variable. o r-value : Typically, an indirect attribute of a variable; the value stored in the memory location associated with a variable.
2. Built-in Types and Primitive Types
- What is a type? Values Operations
- A primitive type mimics hardware units.
- Example : Character is a primitive type in C, but String is not.Boolean is not a primitive type in C while it is in Java.
- Examples: Booleans Integers Reals Characters
- Benefits: Hides underlying representation Can do type checking at compile time Can resolve overloaded functions at compile time Programmer can specify the accuracy required
3. Data Aggregates and Type Constructors
- Definition: An aggregate data type, also called compound object , consists of one or more data type objects. Example: o Arrays: consists of one or more elements of a single data type placed in contiguous locations from first to last.
- Constructors:
4. Constructors
- Cartesian Product: A constructor used to create records and structures Definition: Cartesian product of n sets A1, A2, A3, …, An is a set whose elements are ordered n -tuples (a1, a2, a3, …, an) , where each ak
belongs to Ak for 1 ≤ k ≤ n.
Example : o Structure in C o Records in Pascal and Ada. o A C example: #include <stdio.h>
typedef struct { char name[30]; int age; char ssn[10]; } person;
main (){
person myName = {"Bell", 20, "222222222"};
printf("Person Info: \n"); printf("\t\t%s\n\t\t%d\n\t\t%s\n", myName.name,myName.age, myName.ssn);
}
- Mapping Definition: o A mapping is a function from a set of values (domain) to a set of values (range):
F: integer -> real
Array Constructor: is a mapping from a set of integers (index) to a set of values (content of the array).
Example: o Int myArray[5] ={-2,0,-3,4,300};
MyArray: 0 → - MyArray: 1 → 0 MyArray: 2 → - MyArray: 3 → 4 MyArray: 4 → 300
Another Example:
enum Colors {red, white, black, green, blue};
The compiler assigns an integer number to each name in the enumerated type starting from zero.
- Union and Discriminated Union
Union is a constructor that allows objects to be specified by a disjunctive of fields, e.g., field 1 or field 2 or ... or field (^) n.
Example : sequential files, Vector, List in Java.
- Recursion Recursion is a constructor that allows the size of an object to grow dynamically (add or delete elements). Example: Create the structure of a Linked List in C” typedef strcut { DoulyLinkedList * previous; Char name[30]; DoulyLinkedList * next;
} doublyLinkedList; doublyLinkedList *head;
5. Type Compatibility
- Definitions: Type checking is the activity of ensuring that the operands of an operator are of compatible types Casting or Coercion : A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler-generated code, to a legal type. A type error is the application of an operator to an operand of an inappropriate type. Static type checking: If all type bindings are static. Dynamic type checking: If all type bindings are dynamic.
- Strong Typing & Type Checking A type system is said to be strong if it guarantees not to generate type errors. Strongly typed languages : Languages that have strong type systems. Weakly typed languages : languages that are not strongly typed.
- Coercion rules weakens the value of strong typing
- Static Type System: The type of each expression must be known at compile time. Requirements of a static type system include: o Only built-in types can be used o All variables are declared with an associate type. o All operations are specified by stating the types of the required
- Examples:
- Two records or structure types compatible if they have same structure but different field names?
- Two single-dimensioned array types in a Pascal or Ada program are compatible if they have the same element type but have different subscript ranges?
- C uses structural compatibility except for structures.
6. Type Conversion
- It is needed when the types of an expression are not compatible.
- Some programming languages allow automatic conversions (Also called coercion) : What would the following C program output?
#include <stdio.h>
main(){
char c1 = 'a'; int i = 10;
i += c1; printf(" The value of i is:%d\n",i); }
- Formally, the conversion of a type T 1 to another type R 1 is defined as follows:
func: T 1 → T 2
For example: T 1 x 1 ; T 2 x 2 ;
x 2 = func(x 1 );
- How to use func to convert from another type T 2 to another type R 2?
Requirements: Two new functions t 21 and r 12 :
t 21 : T 2 → T 1
7. Type and Subtypes
- A type is defined by: A set of values A set of operations
- A subtype ST of a basic type T (also called parent type or supertype) can be defined as A subset of the values of T Assume that operations of T are inherited by ST
- A language supporting subtypes must provide: A way to define subsets of a given type, and Compatibility rules between a subtype and its supertype Example: Ada subtype
SUBTYPE Day_Range IS Integer RANGE 0..31; SUBTYPE Year_Range IS Integer RANGE 1901..2099; SUBTYPE natural IS integer range 0..integer'last;
SUBTYPE natural IS integer RANGE 0..integer’last; SUBTYPE positive IS integer RANGE 1..integer’last;
8. Generic Types
- Generic routines allow the same code to be used for multiple data types: ADT Stack, Sorting, searching, etc.
- Called templates in C++
- How type-checking is done to guarantee type safety?
Bind generic types to concrete types at compile-time instantiation.
push: stack(T)xT → stack(T) pop: stack(T) → stack(T)xT length: stack(T) → int
- Polymorphism classification:
polymorphism
Parametric (Templates)
Inclusion (subtypes)
overloading
coercion
universal
Ad hoc
- Ad hoc : functions work on a finite small set of types and may behave differently for each: Overloading Coercion
- Universal : work uniformly for an infinite set of types, all of which have common structure Parametric Inclusion
10. Scope
- Definitions : The scope of a variable is the range of statements over which it is visible The scope rules of a language determine how references to names are associated with variables.
- Example :
#include <stdio.h> main( ) { int x, y; printf("Please enter a value for x: "); scanf( "%d", &x ); printf("Please enter a value for y: "); scanf( "%d", &y ); { // This block used to swap x and y int temp; temp = x; x = y; y = temp; } printf( "x:%d and y:%d\n", x, y ); // printf( "temp:%d\n", temp ); }
If you try to execute the last statement in Visual C++, you will get the following error:
“error C2065: 'temp' : undeclared identifier”
- Non-local variables : The non-local variables of a program unit are those that are visible but not declared in the program.