Classification and distinctive features of programming languages - PowerPoint PPT Presentation

1 / 88
About This Presentation
Title:

Classification and distinctive features of programming languages

Description:

In an imperative language, an expression is evaluated and the result is stored ... Langauges that do allow such expressions, called mixed-mode expressions, must ... – PowerPoint PPT presentation

Number of Views:306
Avg rating:3.0/5.0
Slides: 89
Provided by: jessi69
Category:

less

Transcript and Presenter's Notes

Title: Classification and distinctive features of programming languages


1
Classification and distinctive features of
programming languages
  • procedural
  • declarative
  • object-oriented
  • visual
  • query

2
Imperative language
  • In an imperative language, an expression is
    evaluated and the result is stored in a memory
    location, which is represented as a variable in a
    program.

3
Functional language
  • It provides a set of primitive functions, a set
    of functional forms to construct complex
    functions from those primitive functions, a
    function application operation, and some
    structure or structures for storing data.
  • The objective of the design of a functional
    programming language is to mimic mathematical
    functions to the greatest extent possible.

4
Purely functional language
  • A purely functional programming language does not
    use variables or assignment statements. This free
    the programmer from concerns about the memory
    cells of the computer on which the program is
    executed.
  • Programs are function definitions and function
    application specifications, and executions
    consist of evaluating the function applications.

5
Procedural
  • Programming in both imperative and functional
    languages is primarily procedural, which means
    that the programmer knows what is to be
    accomplished by a program and instructs the
    computer on exactly how the computation is to be
    done.

6
Declarative Language
  • Programming that uses a form of symbolic logic as
    a programming language is often called logic
    programming.
  • Languages based on symbolic logic are called
    logic programming languages or declarative
    languages.
  • e.g. Prolog

7
Object-oriented programming (1)
  • Data-oriented programming focuses on abstract
    data types.
  • An abstract data type is defined as a data
    structure in which the data and its operations
    are defined together in a single syntactic unit.

8
Object-oriented programming (2)
  • Focusing on data is the opposite from the
    traditional procedural programming approach that
    focuses on processes and their implementation in
    subprograms.

9
Object-oriented programming (3)
  • One restriction of abstract data types is that,
    once defined, they cannot be conveniently
    modified for slightly different applications.
  • There is no way to collect the common
    characteristics of families of closely related
    types.

10
Object-oriented programming (4)
  • Objects have local memory, inherent processing
    ability, the capability to communicate with other
    objects, and the ability to inherit
    characteristics from ancestors.
  • Objects do not have names they are anonymous and
    can be referenced only by pointers.

11
Object-oriented programming (5)
  • Objects communicate with other objects by sending
    messages.
  • A message is used by an object to request an
    operation that another object (or itself)
    provides.

12
Object-oriented programming (6)
  • The operation definitions in objects are called
    methods.
  • A method specifies the reaction of the object
    when it receives a message that is directed to
    that method.
  • The binding of a message to a method is dynamic.

13
Object-oriented programming (7)
  • The common characteristics of a category of
    objects is captured by a class definition.
  • Instances of the class can be created and are the
    objects of the program.
  • Each object has its own local data and represents
    a different instance of its class.
  • The only difference between two objects of the
    same class is the state of their local variables.

14
Procedural languages
  • (C, Pascal)

15
Data Types
  • Nearly all programming languages provide a set of
    primitive data types. Some of the primitive types
    are merely reflections of the hardware for
    example, integer types two's complement
    floating-point types format of IEEE single or
    double precisions. Before a variable can be
    referenced in a program, it must be bound to a
    data type, for example, a 2-bytes integer types
    bind a variable to a range between -32768 and
    32767.

16
Simple Data Type (1)
  • Nearly all programming languages provide a set of
    simple data type.
  • Some of these data types are merely reflections
    of the hardware (e.g. integer type).
  • Others require only a little support for their
    implementation. (e.g. IEEE floating point format).

17
Simple Data Type (2)
  • single items (numbers, characters etc) that are
    associated with single identifiers on a
    one-to-one basis.
  • examples are integer, character, boolean,
    floating-point numbers.

18
User-Defined Data Type
  • An ordinal type is one in which the range of
    possible values can be easily associated with a
    set of positive integers.
  • In many languages, users can define two kinds of
    ordinal types enumeration and subrange.

19
Enumeration Types
  • An enumeration type is one in which all of the
    possible values, which are symbolic constants,
    are enumerated in the definition.
  • Enumeration types provide greater readability in
    a very direct way.
  • Named values are easily recognized, whereas coded
    values are not.
  • Coding are meaningless to everyone except the
    program author.

20
Pascal Example
  • type colortype (red, blue, green, yellow)
  • var color colortype
  • .
  • color blue
  • if (color gt red)
  • ...

21
In Pascal,
  • a literal constant is not allowed to be used in
    more than one enumeration type definition in a
    given referencing environment.
  • Enumeration type variables can be used as array
    subscripts, for loop variables, and case selector
    expressions, but can be neither input nor output.
  • Two enumeration type variables and / or literal
    can be compared with the relational operators,
    with their relative positions in the declaration
    determining the result.

22
Subrange Types (1)
  • A subrange type is a contiguous sub-sequence of
    an ordinal type.
  • e.g. 12..18 is a subrange of integer type.
  • Pascal Example
  • type
  • uppercase A..Z
  • index 1..100

23
Subrange Types (2)
  • Subrange types enhance readability by making it
    clear to readers that variables of subtypes can
    store only certain ranges of values.
  • Reliability is increased with subrange types,
    because assigning a value to a subrange variable
    that is outside the specified range is detected
    as an error by the run-time system.

24
Structured Data Type (1)
  • multiple data items are related to one another in
    some specified manner.
  • each group of data items is associated with a
    particular identifier.
  • individual data items within each group can also
    be associated with a particular identifier. e.g.
    index using in array field name using in record.

25
Structured Data Type (2)
  • Array Types
  • Record Types
  • Union Types
  • Set Types
  • Pointer Types

26
Array Types (1)
  • An array is a homogeneous aggregate of data
    elements in which an individual element is
    identified by its position in the aggregate,
    relative to the first element.
  • A majority of computer programs need to model
    collections of objects, in which the objects are
    of the same type and must be processed in the
    same way.

27
Array Types (2)
  • Design issues specific to arrays are the
    following
  • What types are legal for subscripts ?
  • When are subscript ranges bound ?
  • When does array allocation take place ?
  • How many subscripts are allowed ?
  • Can arrays be initialized when they have their
    storage allocated ?

28
Array Type (3)
  • C allows initialization of its static arrays.
  • e.g. int list 4 , 5 , 7 , 83
  • The compiler sets the length of the array.
  • Character strings in C are implemented as arrays
    of char.
  • e.g. char name freddie

29
Record Types
  • A record is a possibly heterogeneous aggregate of
    data elements in which the individual elements
    are identified by names.
  • The record elements (fields) are named with
    identifiers, and references are made using these
    identifiers.

30
Union Types
  • A union is a type that is allowed to store
    different type values at different times during
    program execution.
  • The same location (a table field) could store a
    value of different types.
  • If type checking is required, it must be dynamic.

31
Pascal Union Types (1)
  • In Pascal, it is called record variant.
  • e.g.
  • type shape (circle, triangle, rectangle)
  • object record
  • case form shape of
  • circle (diameter real)
  • triangle (leftside
    integer rigthside integer angle real)
  • rectangle (side1
    integer side2 integer)
  • end
  • var thing object

32
Pascal Union Types (2)
  • case thing.form of
  • circle writeln(It is a circle its
    diameter , thing.diameter)
  • triangle begin
  • writeln(It is a
    triangle)
  • writeln(its sides are,
    thing.leftside, thing.rightside)
  • writeln(angle between the
    sides is, thing.angle)
  • end
  • rectangle begin
  • writeln(It is a
    rectangle)
  • writeln(its sides are
    , thing.side1, thing.side2)
  • end
  • end

33
Pascal Union Types (3)
34
Set Types (1)
  • A set type is one whose variables can store
    unordered collections of distinct values from
    some ordinal type called its base type.
  • Set types are often used to model mathematical
    sets.
  • e.g. text analysis often requires that small sets
    of characters, such as punctuation characters or
    vowels, be stored and conveniently searched.

35
Set Types (2)
  • A variable of a set type can store any subset of
    the range of the base type.
  • The set of all subsets of a given base set is
    often called the powerset of that base set.

36
Sets in Pascal (1)
  • The maximum size of Pascal base sets is
    implementation dependent.
  • Many implementations severely restrict it, often
    to much less than 100.
  • They do so because sets and their operations are
    most efficiently implemented by representing set
    variables as bit strings that fit into a single
    machine word.

37
Sets in Pascal (2)
  • Pascal allows the following set operations
  • assignment of compatible set types
  • set union
  • set intersection
  • - set difference
  • set equality
  • lt gt set inequality
  • lt set included in (subset)
  • gt set includes (superset)
  • in element inclusion in a set

38
Sets in Pascal (3)
  • e.g.
  • type colors (red, blue, green, yellow, orange,
    white)
  • colorset set of colors
  • var set1, set2, set3 colorset
  • set1 red, blue, yellow, white
  • set2 blue, yellow

39
Sets in Pascal (4)
  • set3 set1 set2 set3 is now red, blue,
    yellow, white
  • set3 set1 set2 set3 is now blue, yellow
  • set3 set1 - set2 set2 is now red, white
  • set1 set2 is false
  • set1 lt gt set2 is true
  • set1 lt set2 is false
  • set1 gt set2 is true
  • red in set1 is true
  • red in set2 is false

40
Pointer Types (1)
  • A pointer type is one in which the variables have
    a range of values that consists of memory
    addresses and a special value, nil.
  • The value nil is not a valid address and is used
    to indicate that a pointer cannot currently be
    used to reference another object.

41
Pointer Types (2)
  • Two distinct kinds of uses.
  • Pointers provide some of the power of indirect
    addressing
  • Pointers provide a method of dynamic storage
    management.
  • A pointer access to a location in the area where
    storage is dynamically allocated is called a heap.

42
Heap
  • Complex organization occur because of randomly
    ordered dynamic allocations and deallocations.
  • Variables are dynamically allocated from heap.
  • No identifier associated with and can be
    referenced only by pointer variables called
    anonymous variables.

43
Design Issues of Pointer
  • What are the scope and lifetime of a pointer
    variable?
  • What is the lifetime of a dynamic variable?
  • Are pointers restricted as to the type of object
    to which they can point?

44
Variables
  • an abstraction of a computer memory cell, or a
    collection of cells.
  • translator converts the names of variables to
    actual addresses.
  • attributes of variables name, address, value,
    type, lifetime, scope.

45
Keywords
  • Special words in programming languages are used
    to make programs more readable by naming actions
    to be performed.
  • A keyword is a word of a programming language
    that is special only in certain contexts.
  • A reserved word is a special word of a
    programming language that cannot be used as a
    name.

46
Aliases
  • Multiple identifiers reference the same address,
    the names are called aliases.
  • Aliasing is a hindrance to readability because it
    allows a variable to have its value changed by an
    assignment to a different variable.
  • Aliases can be created through subprogram
    parameters, or using union (record variant).
  • Two pointer variables are aliases when they point
    to the same memory location.

47
Explicit And Implicit Declarations
  • An explicit declaration is a statement in a
    program that lists variable names and declares
    them to be of a particular type.
  • An implicit declaration is a means of associating
    variables with types through default conventions
    instead of declaration statements.

48
  • Although they are a minor convenience to
    programmers, implicit declarations can be
    detrimental to reliability, because they prevent
    the compilation process from detecting some
    programmer errors. Variables that are
    accidentally left undeclared by the programmer
    are given default types and unexpected
    attributes, which could cause subtle errors that
    are difficult to diagnose.

49
Blocks (1)
  • Some languages allow local variables to be
    defined in the midst of executable code.
  • Such variables have their storage allocated when
    the section is entered and deallocated when the
    section is exited.
  • Such a section of code is called a block.
  • C allows any compound statement (a statement
    sequence surrounded by matched braces) to have
    declarations

50
Blocks (2)
  • For example, if list were an integer array, one
    could write
  • if (listi lt listj)
  • int temp
  • temp listi
  • listi listj
  • listj temp

51
Named Constants
  • A named constant is bound to a value only at the
    time it is bound to storage.
  • Its value cannot be changed by assignment or by
    an input statement.
  • Named constants are useful as aids to readability
    and program reliability.
  • Named constants can aid modifiability.
  • Static binding of values cannot be changed
    during execution.
  • e.g. define PI 22/7 assigns PI the value during
    compilation.

52
Initialization (1)
  • Initialization of variables and binding a value
    to a named constant is the same process, except
    that named constant is permanent.
  • The binding of a variable to a value at the time
    it is bound to storage is called initialization.
  • If the variable is statically bound to storage,
    binding and initialization occur before run time.
    If the storage binding is dynamic, initialization
    is also dynamic.

53
Initialization (2)
  • In BASIC, all numeric variables are initialized
    to be zero during compile time.
  • In C, initial values of variables can be
    specified in the declaration statement.
  • Pascal provides a way to initialize variables,
    except at run time with assignment statements.
  • Initialization occurs only once for static
    variables, but it occurs with every allocation
    for dynamically allocated variables.

54
Expressions And Assignment Statements
  • The purpose of assignment statements is to change
    the values of variables.
  • To understand expression evaluation, it is
    necessary to be familiar with the orders of
    operator and operand evaluation.
  • The operator evaluation order of expressions is
    governed by the associativity and precedence
    rules of the language.

55
Operator Evaluation Order (1)
  • It includes the operator precedence rules and the
    associativity rules of the language.
  • The operator precedence rules for expression
    evaluation define the order in which the
    operators of different precedence levels are
    evaluated.
  • The operator precedence rules for expressions are
    based on the hierarchy of operator priorities of
    the language.

56
Operator Evaluation Order (2)
  • When an expression contains two adjacent
    occurrences of operators with the same level of
    precedence, the question of which operator is
    evaluated first is answered by the associativity
    rules of the language.
  • An operator can either have left or right
    associativity, meaning that the leftmost
    occurrence is evaluated first or the rightmost
    occurrence is evaluated first, respectively.

57
Mixed Mode Expressions (1)
  • An operator of an expression can have operands of
    different types.
  • Langauges that do allow such expressions, called
    mixed-mode expressions, must define conventions
    for implicit operand type conversions, called
    coercions, because computers usually do not have
    operations that use operands of different types.

58
Mixed Mode Expressions (2)
  • A coercion is an implicit type conversion that is
    initiated by the compiler.
  • Type conversions explicitly requested by the
    programmer as explicit conversions, or casts, not
    coercions.

59
Relational And Boolean Expressions (1)
  • A relational operator is an operator that
    compares the values of its two operands.
  • A relational expression has two operands and one
    relational operator.
  • The value of a relational expression is Boolean,
    except when Boolean is not a type in the language.

60
Relational And Boolean Expressions (2)
  • Boolean expressions consist of Boolean variables,
    Boolean constants, relational expressions and
    Logical operators.
  • Logical operators usually include those for the
    AND, OR, and NOT operations, and sometimes for
    exclusive OR and equivalence.
  • In C, no Boolean type and thus no Boolean values.

61
Relational And Boolean Expressions (3)
  • Instead, numeric values are used to represent
    Boolean values.
  • With zero considered false and all nonzero values
    considered true.
  • the result of evaluating such an expression is an
    integer, with the value 0 if false and 1 if true.
  • One odd result of C's design is that the
    expression a gt b gt c is legal.
  • Left associative.

62
Multiple Assignments
  • e.g. In C language, the statement
  • nl nw nc 0
  • sets all three variables to zero.
  • Right associativity.

63
Conditional Assignments (1)
  • In C, a conditional expression has the form
  • expression_1 ? expression_2 expression_3
  • where expression_1 is interpreted as a Boolean
    expression.
  • If expression_1 evaluates to true, the value of
    the whole expression is the value of
    expression_2
  • otherwise it is the value of expression_3.

64
Conditional Assignments (2)
  • e.g. 1
  • z (a lt b) ? a b / z min(a, b) /
  • set z to the minimum of a and b.
  • e.g. 2
  • average (count 0) ? 0 sum / count
  • prevents from divide by zero runtime error

65
Assigning Operators (1)
  • Compound assignment operators is a shorthand
    method of specifying commonly needed forms of
    assignments.
  • The form of assignment that can be abbreviated
    with this technique has the destination variable
    also appearing as the first operand in the
    expression on the right side.

66
Assigning Operators (2)
  • e.g. In C, compound assignment operators is the
    catenation of the desired binary operator to the
    operator.
  • sum value
  • Unary arithmetic operators combine increment and
    decrement operations with assignment.
  • Right associativity
  • - count is equivalent to - (count )

67
Assigning Operators (3)
  • Assignment statements as operands
  • In C, the assignment statement produces a result,
    which is the value assigned to the target.
  • It can therefore be used as an operand in
    expressions.
  • e.g.
  • while ((ch getchar( )) ! EOF)
  • .....
  • The result, or value assigned, is then compared
    with the constant EOF.

68
Unary Operators
  • The operators can be unary, meaning they have a
    single operand,
  • or binary, menaing they have two operands.

69
Mixed Mode Assignments
  • If the type of the expression is not the same as
    the type of the variable being assigned, coercion
    can be applied.

70
Control Structure
  • A control structure is a control statement and
    the collection of statements whose execution it
    controls.

71
Control Statements And Their Structures
  • Control statements are statements that provide
    the capabilities of choosing among alternative
    control flow paths (of statement execution) and
    of causing the repeated execution of certain
    collections of statements.

72
Single And Two Way Selection
  • if (expression)
  • ....
  • else
  • ....

73
Nesting Selectors
  • An interesting problem arises when two-way
    selection constructs can be nested.
  • e.g. In C,
  • if (a gt b)
  • if (b gt c)
  • z b
  • else
  • z c
  • this construct can be interpreted in two
    different ways, depending on whether the else
    clause is matched with the first then clause or
    the second.

74
Multiple Selectors
  • This structure is encapsulated and has single
    entry.
  • Implicit branches to a single point at the end of
    the whole construct are also provided for each
    selectable statement or compound statement.
  • This restricts the control flow through the
    structure to a single selectable segment.

75
Repetitive Constructs
  • An iterative statement is one that causes a
    statement or collection of statements to be
    executed zero, one or more times.

76
Problems With Unconditional Branching
  • The unconditional branch, or goto, is the most
    powerful statement for controlling the flow of
    execution of a program's statements.
  • Without restrictions on use, imposed either by
    language design or programming standards, goto
    statements can make programs virtually unreadable
    and, as a result, highly unreliable and difficult
    to maintain.

77
Subprograms
  • Subprograms are the fundamental building blocks
    of programs.
  • Each subprogram has a single entry point.
  • The calling program unit is suspended during the
    execution of the called subprogram, which implies
    that there is only one subprogram in execution at
    any given time.
  • Control always returns to the caller when the
    subprogram execution terminates.

78
Process Abstraction (1)
  • A subprogram definition describes the actions of
    the subprogram abstraction.
  • A subprogram call is the explicit request that
    the subprogram be executed.
  • An active subprogram after having been called,
    has begun execution but has not yet completed
    that execution.
  • A subprogram header is the first line of the
    definition.
  • The parameters in the subprogram header are
    called formal parameters.

79
Process Abstraction (2)
  • Subprogram call statements must include the name
    of the subprogram and a list of parameters to be
    bound to the formal parameters of the subprogram.
    These parameters are called actual parameters.
  • The first actual parameter is bound to the first
    formal parameter and so forth. Such parameters
    are called positional parameters.
  • In keyword parameters, the name of the formal
    parameter to which an actual parameter is to be
    bound is specified with the actual parameter.

80
Local Referencing (1)
  • Subprograms are generally allowed to declare
    their own variables, thereby defining local
    referencing environments.
  • Variables that are declared inside subprograms
    are called local variables.
  • Access to local variables is usually restricted
    to the subprogram in which they are declared.
  • Local variables can be either statically or
    dynamically bound to storage.

81
Local Referencing (2)
  • If local variables are semidynamic, they are
    bound to storage when the subprogram begins
    execution and unbound from storage when that
    execution terminates.
  • If local variables are static, they are bound to
    storage when the subprogram first begins
    execution.

82
Local Referencing (3)
  • Advantages of static local variables is that they
    are very efficient they usually can be accessed
    faster because there is no indirection and no
    run-time overhead of allocation and deallocation.
  • They allow subprograms to be history sensitive.
  • Disadvantage of static local variables is the
    inability to support recursion.

83
Parameter-Passing Methods
  • Parameter-passing methods are the ways in which
    parameters are transmitted to and / or from
    called subprograms.
  • Formal parameters are characterized by one of
    three distinct semantics models
  • They can receive data from the corresponding
    actual parameter,
  • they can transmit data to the actual parameter,
  • they can do both.

84
Parameter-Passing
  • These three semantics models are called in mode,
    out mode and inout mode respectively.

85
Value
  • the value of the actual parameter is used to
    initialize the corresponding formal parameter,
  • which then acts as a local variable in the
    subprogram,
  • thus providing in-mode semantics.
  • actual parameter can be an expression.
  • Disadvantages are additional storage is required
  • physically movement is required
  • The storage and the move operations can be costly
    if the parameter is a large object.

86
Result
  • out-mode parameters.
  • no value is transmitted to the subprogram
  • the corresponding formal parameter acts as a
    local variable.
  • but just before control is transferred back to
    the caller,
  • its value is passed back to the caller's actual
    parameter
  • actual parameter must be a variable.

87
Value-Result
  • inout-mode parameters.
  • pass-by-copy.
  • value of actual parameter is used to initialize
    the corresponding formal parameter
  • acts as a local variable
  • at subprogram termination, the value of the
    formal parameter is transmitted back to the
    actual parameter.

88
Reference
  • transmits an access path, usually just an
    address, to the called subprogram.
Write a Comment
User Comments (0)
About PowerShow.com