Docsity

Programming Languages: Variables, Types, and Data Structures - Prof. Abdelghani Bellaachia, Study notes of Computer Science

Toggle sidebar
bg1
Basics of Programming Languages
1. DEFINITIONS...................................................................... 2
2. BUILT-IN TYPES AND PRIMITIVE TYPES.................. 5
3. DATA AGGREGATES AND TYPE CONSTRUCTORS 5
4. CONSTRUCTORS............................................................... 7
5. TYPE COMPATIBILITY .................................................11
6. TYPE CONVERSION........................................................ 14
7. TYPE AND SUBTYPES ....................................................16
8. GENERIC TYPES.............................................................. 17
9. MONOMORPHIC VERSUS POLYMORPHIC ............. 18
10. SCOPE............................................................................. 20
A. Bellaachia Page: 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

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

    1. DEFINITIONS......................................................................
    1. BUILT-IN TYPES AND PRIMITIVE TYPES..................
    1. DATA AGGREGATES AND TYPE CONSTRUCTORS
    1. CONSTRUCTORS
    1. TYPE COMPATIBILITY
    1. TYPE CONVERSION........................................................
    1. TYPE AND SUBTYPES
    1. GENERIC TYPES..............................................................
    1. MONOMORPHIC VERSUS POLYMORPHIC
    1. 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:
    1. Two records or structure types compatible if they have same structure but different field names?
    2. 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?
    3. 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.

  • Example:

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.