Programming language features exist to provide building blocks to be combined to express programming ideals. Ideally, a programming language should: For example, different paradigms may differentiate: They follow a set of rules called a
syntax. The purpose of defining a solution in terms of its
formal language is to generate an
algorithm to solve the underlining problem.
Generations of programming language monitor on a
W65C816S microprocessor The evolution of programming languages began when the
EDSAC (1949) used the first
stored computer program in its
von Neumann architecture. Programming the EDSAC was in the first
generation of programming language. • The
first generation of programming language is
machine language.
Machine language requires the programmer to enter instructions using
instruction numbers called
machine code. For example, the ADD operation on the
PDP-11 has instruction number 24576. • The
second generation of programming language is
assembly language. ::*
Labels allow the programmer to work with
variable names. The assembler will later translate labels into physical
memory addresses. ::*
Operations allow the programmer to work with mnemonics. ::*
Operands tell the assembler which data the operation will process. The assembler will later translate mnemonics and operands into instruction numbers. ::*
Comments allow the programmer to articulate a narrative because the instructions alone may be opaque to humans. :: The key characteristic of an assembly language program is it forms a one-to-one mapping to its corresponding machine language target. • The
third generation of programming language uses
compilers and
interpreters to execute computer programs. The distinguishing feature of a
third generation language is its independence from particular hardware. Early languages include
FORTAN (1958),
COBOL (1959),
ALGOL (1960), and
BASIC (1964). Whereas
third-generation languages historically generated many machine instructions for each statement, C has statements that may generate a single machine instruction. Moreover, an
optimizing compiler might overrule the programmer and produce fewer machine instructions than statements. Today, an entire
paradigm of languages fill the
imperative,
third generation spectrum. • The
fourth generation of programming language emphasizes what output results are desired, rather than how programming statements should be constructed. • A
declaration introduces a
variable name to the
computer program and assigns it to a
datatype – for example: var x: integer; • An
expression yields a value – for example: 2 + 2 yields 4 • A
statement might
assign an expression to a variable or use the value of a variable to alter the program's
control flow – for example: x := 2 + 2;
if x = 4 then do_something();
Fortran FORTRAN (1958) was unveiled as "The IBM Mathematical FORmula TRANslating system". It was designed for scientific calculations, without
string handling facilities. Along with
declarations,
expressions, and
statements, it supported: •
arrays. •
subroutines. •
"do" loops. It succeeded because: • programming and debugging costs were below computer running costs. • it was supported by IBM. • applications at the time were scientific. However, non-IBM vendors also wrote Fortran compilers, but with a syntax that would likely fail IBM's compiler. The
US Department of Defense influenced COBOL's development, with
Grace Hopper being a major contributor. The statements were English-like and verbose. The goal was to design a language so managers could read the programs. However, the lack of structured statements hindered this goal. COBOL's development was tightly controlled, so dialects did not emerge to require ANSI standards. As a consequence, it was not changed for 15 years until 1974. The 1990s version did make consequential changes, like
object-oriented programming. Emerging from a committee of European and American programming language experts, it used standard
mathematical notation and had a readable, structured design. Algol was first to define its syntax using the
Backus–Naur form. One region is called the
initialized data segment, where variables declared with default values are stored. The other region is called the
block started by segment, where variables declared without default values are stored. :* Variables stored in the
global and static data region have their
addresses set at compile time. They retain their values throughout the life of the process. :* The global and static region stores the
global variables that are declared on top of (outside) the main() function. Global variables are visible to main() and every other function in the source code. : On the other hand, variable declarations inside of main(), other functions, or within { }
block delimiters are
local variables. Local variables also include
formal parameter variables. Parameter variables are enclosed within the parenthesis of a function definition. Parameters provide an
interface to the function. :*
Local variables declared using the static prefix are also stored in the
global and static data region. Variables placed in the stack are populated from top to bottom. are called
automatic variables Like the stack, the addresses of heap variables are set during runtime. An
out of memory error occurs when the heap pointer and the stack pointer meet. :*
C provides the malloc() library function to
allocate heap memory. Populating the heap with data is an additional copy function. Variables stored in the heap are economically passed to functions using pointers. Without pointers, the entire block of data would have to be passed to the function via the stack.
C++ In the 1970s,
software engineers needed language support to break large projects down into
modules. One obvious feature was to decompose large projects
physically into separate
files. A less obvious feature was to decompose large projects
logically into
abstract data types.
Object-oriented imperative languages developed by combining the need for classes and the need for safe
functional programming. A
function, in an object-oriented language, is assigned to a class. An assigned function is then referred to as a
method,
member function, or
operation.
Object-oriented programming is executing
operations on
objects.
Object-oriented languages support a syntax to model
subset/superset relationships. In
set theory, an
element of a subset inherits all the attributes contained in the superset. For example, a student is a person. Therefore, the set of students is a subset of the set of persons. As a result, students inherit all the attributes common to all persons. Additionally, students have unique attributes that other people do not have.
Object-oriented languages model
subset/superset relationships using
inheritance.
Object-oriented programming became the dominant language paradigm by the late 1990s. It was designed to expand
C's capabilities by adding the object-oriented facilities of the language
Simula. An object-oriented module is composed of two files. The definitions file is called the
header file. Here is a C++
header file for the
GRADE class in a simple school application: // grade.h // ------- // Used to allow multiple source files to include // this header file without duplication errors. // ---------------------------------------------- • ifndef GRADE_H • define GRADE_H class GRADE { public: // This is the constructor operation. // ---------------------------------- GRADE ( const char letter ); // This is a class variable. // ------------------------- char letter; // This is a member operation. // --------------------------- int grade_numeric( const char letter ); // This is a class variable. // ------------------------- int numeric; }; • endif A
constructor operation is a function with the same name as the class name. It is executed when the calling operation executes the
new statement. A module's other file is the
source file. Here is a C++ source file for the
GRADE class in a simple school application: // grade.cpp // --------- • include "grade.h" GRADE::GRADE( const char letter ) { // Reference the object using the keyword 'this'. // ---------------------------------------------- this->letter = letter; // This is Temporal Cohesion // ------------------------- this->numeric = grade_numeric( letter ); } int GRADE::grade_numeric( const char letter ) { if ( ( letter == 'A' || letter == 'a' ) ) return 4; else if ( ( letter == 'B' || letter == 'b' ) ) return 3; else if ( ( letter == 'C' || letter == 'c' ) ) return 2; else if ( ( letter == 'D' || letter == 'd' ) ) return 1; else if ( ( letter == 'F' || letter == 'f' ) ) return 0; else return -1; } Here is a C++
header file for the
PERSON class in a simple school application: // person.h // -------- • ifndef PERSON_H • define PERSON_H class PERSON { public: PERSON ( const char *name ); const char *name; }; • endif Here is a C++
source file for the
PERSON class in a simple school application: // person.cpp // ---------- • include "person.h" PERSON::PERSON ( const char *name ) { this->name = name; } Here is a C++
header file for the
STUDENT class in a simple school application: // student.h // --------- • ifndef STUDENT_H • define STUDENT_H • include "person.h" • include "grade.h" // A STUDENT is a subset of PERSON. // -------------------------------- class STUDENT : public PERSON{ public: STUDENT ( const char *name ); GRADE *grade; }; • endif Here is a C++
source file for the
STUDENT class in a simple school application: // student.cpp // ----------- • include "student.h" • include "person.h" STUDENT::STUDENT ( const char *name ): // Execute the constructor of the PERSON superclass. // ------------------------------------------------- PERSON( name ) { // Nothing else to do. // ------------------- } Here is a driver program for demonstration: // student_dvr.cpp // --------------- • include • include "student.h" int main( void ) { STUDENT *student = new STUDENT( "The Student" ); student->grade = new GRADE( 'a' ); std::cout // Notice student inherits PERSON's name name grade->numeric Here is a
makefile to compile everything: • makefile • -------- all: student_dvr clean: rm student_dvr *.o student_dvr: student_dvr.cpp grade.o student.o person.o c++ student_dvr.cpp grade.o student.o person.o -o student_dvr grade.o: grade.cpp grade.h c++ -c grade.cpp student.o: student.cpp student.h c++ -c student.cpp person.o: person.cpp person.h c++ -c person.cpp
Declarative languages Imperative languages have one major criticism: assigning an expression to a
non-local variable may produce an unintended
side effect.
Declarative languages generally omit the assignment statement and the control flow. They describe
what computation should be performed and not
how to compute it. Two broad categories of declarative languages are
functional languages and
logical languages. The principle behind a
functional language is to use
lambda calculus as a guide for a well defined
semantic. In mathematics, a function is a rule that maps elements from an
expression to a range of
values. Consider the function: times_10(x) = 10 * x The
expression 10 * x is mapped by the function times_10() to a range of
values. One
value happens to be 20. This occurs when x is 2. So, the application of the function is mathematically written as: times_10(2) = 20 A
functional language compiler will not store this value in a variable. Instead, it will
push the value onto the computer's
stack before setting the
program counter back to the calling function. The calling function will then
pop the value from the stack.
Imperative languages do support functions. Therefore,
functional programming can be achieved in an imperative language, if the programmer uses discipline. However, a
functional language will force this discipline onto the programmer through its syntax. Functional languages have a syntax tailored to emphasize the
what. A functional program is developed with a set of primitive functions followed by a single driver function. Moreover, their lack of side-effects have made them popular in
parallel programming and
concurrent programming. However, application developers prefer the
object-oriented features of
imperative languages. It is tailored to process
lists. A full structure of the data is formed by building lists of lists. In memory, a
tree data structure is built. Internally, the tree structure lends nicely for
recursive functions. The syntax to build a tree is to enclose the space-separated
elements within parenthesis. The following is a
list of three elements. The first two elements are themselves lists of two elements: ((A B) (HELLO WORLD) 94) Lisp has functions to extract and reconstruct elements. The function head() returns a list containing the first element in the list. The function tail() returns a list containing everything but the first element. The function cons() returns a list that is the concatenation of other lists. Therefore, the following expression will return the list x: cons(head(x), tail(x)) One drawback of Lisp is when many functions are nested, the parentheses may look confusing. Also,
Lisp is not concerned with the
datatype of the elements at compile time. Instead, it assigns (and may reassign) the datatypes at
runtime. Assigning the datatype at runtime is called
dynamic binding. Whereas dynamic binding increases the language's flexibility, programming errors may linger until late in the
software development process. stands for "Meta Language". ML checks to make sure only data of the same type are compared with one another. For example, this function has one input parameter (an integer) and returns an integer:
ML is not parenthesis-eccentric like
Lisp. The following is an application of times_10(): times_10 2 It returns "20 : int". (Both the results and the datatype are returned.) Like
Lisp,
ML is tailored to process lists. Unlike
Lisp, each element is the same datatype. Moreover,
ML assigns the datatype of an element at
compile time. Assigning the datatype at compile time is called
static binding. Static binding increases reliability because the compiler checks the context of variables before they are used.
Prolog Prolog (1972) stands for "PROgramming in LOGic". It is a
logic programming language, based on formal
logic. The language was developed by
Alain Colmerauer and Philippe Roussel in Marseille, France. It is an implementation of
Selective Linear Definite clause resolution, pioneered by
Robert Kowalski and others at the
University of Edinburgh. The building blocks of a Prolog program are
facts and
rules. Here is a simple example: cat(tom). % tom is a cat mouse(jerry). % jerry is a mouse animal(X) :- cat(X). % each cat is an animal animal(X) :- mouse(X). % each mouse is an animal big(X) :- cat(X). % each cat is big small(X) :- mouse(X). % each mouse is small eat(X,Y) :- mouse(X), cheese(Y). % each mouse eats each cheese eat(X,Y) :- big(X), small(Y). % each big animal eats each small animal After all the facts and rules are entered, then a question can be asked: : Will Tom eat Jerry? ?- eat(tom,jerry). true The following example shows how Prolog will convert a letter grade to its numeric value: numeric_grade('A', 4). numeric_grade('B', 3). numeric_grade('C', 2). numeric_grade('D', 1). numeric_grade('F', 0). numeric_grade(X, -1) :- not X = 'A', not X = 'B', not X = 'C', not X = 'D', not X = 'F'. grade('The Student', 'A'). ?- grade('The Student', X), numeric_grade(X, Y). X = 'A', Y = 4 Here is a comprehensive example: 1) All dragons billow fire, or equivalently, a thing billows fire if the thing is a dragon: billows_fire(X) :- is_a_dragon(X). 2) A creature billows fire if one of its parents billows fire: billows_fire(X) :- is_a_creature(X), is_a_parent_of(Y,X), billows_fire(Y). 3) A thing X is a parent of a thing Y if X is the mother of Y or X is the father of Y: is_a_parent_of(X, Y):- is_the_mother_of(X, Y). is_a_parent_of(X, Y):- is_the_father_of(X, Y). 4) A thing is a creature if the thing is a dragon: is_a_creature(X) :- is_a_dragon(X). 5) Norberta is a dragon, and Puff is a creature. Norberta is the mother of Puff. is_a_dragon(norberta). is_a_creature(puff). is_the_mother_of(norberta, puff). Rule (2) is a
recursive (inductive) definition. It can be understood declaratively, without the need to understand how it is executed. Rule (3) shows how
functions are represented by using relations. Here, the mother and father functions ensure that every individual has only one mother and only one father. Prolog is an untyped language. Nonetheless,
inheritance can be represented by using predicates. Rule (4) asserts that a creature is a superclass of a dragon. Questions are answered using
backward reasoning. Given the question: ?- billows_fire(X). Prolog generates two answers : X = norberta X = puff Practical applications for Prolog are
knowledge representation and
problem solving in
artificial intelligence.
Object-oriented programming Object-oriented programming is a programming method to execute
operations (
functions) on
objects. The basic idea is to group the characteristics of a
phenomenon into an object
container and give the container a name. The
operations on the phenomenon are also grouped into the container. This programming method need not be confined to an
object-oriented language. In an object-oriented language, an object container is called a
class. In a non-object-oriented language, a
data structure (which is also known as a
record) may become an object container. To turn a data structure into an object container, operations need to be written specifically for the structure. The resulting structure is called an
abstract datatype. However,
inheritance will be missing. Nonetheless, this shortcoming can be overcome. Here is a
C programming language header file for the
GRADE abstract datatype in a simple school application: /* grade.h */ /* ------- */ /* Used to allow multiple source files to include */ /* this header file without duplication errors. */ /* ---------------------------------------------- */ • ifndef GRADE_H • define GRADE_H typedef struct { char letter; } GRADE; /* Constructor */ /* ----------- */ GRADE *grade_new( char letter ); int grade_numeric( char letter ); • endif The grade_new() function performs the same algorithm as the C++
constructor operation. Here is a C programming language
source file for the
GRADE abstract datatype in a simple school application: /* grade.c */ /* ------- */ • include "grade.h" GRADE *grade_new( char letter ) { GRADE *grade; /* Allocate heap memory */ /* -------------------- */ if ( ! ( grade = calloc( 1, sizeof ( GRADE ) ) ) ) { fprintf(stderr, "ERROR in %s/%s/%d: calloc() returned empty.\n", __FILE__, __FUNCTION__, __LINE__ ); exit( 1 ); } grade->letter = letter; return grade; } int grade_numeric( char letter ) { if ( ( letter == 'A' || letter == 'a' ) ) return 4; else if ( ( letter == 'B' || letter == 'b' ) ) return 3; else if ( ( letter == 'C' || letter == 'c' ) ) return 2; else if ( ( letter == 'D' || letter == 'd' ) ) return 1; else if ( ( letter == 'F' || letter == 'f' ) ) return 0; else return -1; } In the constructor, the function calloc() is used instead of malloc() because each memory cell will be set to zero. Here is a C programming language
header file for the
PERSON abstract datatype in a simple school application: /* person.h */ /* -------- */ • ifndef PERSON_H • define PERSON_H typedef struct { char *name; } PERSON; /* Constructor */ /* ----------- */ PERSON *person_new( char *name ); • endif Here is a C programming language
source file for the
PERSON abstract datatype in a simple school application: /* person.c */ /* -------- */ • include "person.h" PERSON *person_new( char *name ) { PERSON *person; /* Error checking omitted */ person = calloc( 1, sizeof ( PERSON ) ); person->name = name; return person; } Here is a C programming language
header file for the
STUDENT abstract datatype in a simple school application: /* student.h */ /* --------- */ • ifndef STUDENT_H • define STUDENT_H • include "person.h" • include "grade.h" typedef struct { /* A STUDENT is a subset of PERSON. */ /* -------------------------------- */ PERSON *person; GRADE *grade; } STUDENT; /* Constructor */ /* ----------- */ STUDENT *student_new( char *name ); • endif Here is a C programming language
source file for the
STUDENT abstract datatype in a simple school application: /* student.c */ /* --------- */ • include "student.h" • include "person.h" STUDENT *student_new( char *name ) { STUDENT *student; /* Error checking omitted */ student = calloc( 1, sizeof ( STUDENT ) ); /* Execute the constructor of the PERSON superclass. */ /* ------------------------------------------------- */ student->person = person_new( name ); return student; } Here is a driver program for demonstration: /* student_dvr.c */ /* ------------- */ • include • include "student.h" int main( void ) { STUDENT *student = student_new( "The Student" ); student->grade = grade_new( 'a' ); printf( "%s: Numeric grade = %d\n", /* Whereas a subset exists, inheritance does not. */ student->person->name, /* Functional programming is executing functions just-in-time (JIT) */ grade_numeric( student->grade->letter ) ); return 0; } Here is a
makefile to compile everything: • makefile • -------- all: student_dvr clean: rm student_dvr *.o student_dvr: student_dvr.c grade.o student.o person.o gcc student_dvr.c grade.o student.o person.o -o student_dvr grade.o: grade.c grade.h gcc -c grade.c student.o: student.c student.h gcc -c student.c person.o: person.c person.h gcc -c person.c The formal strategy to build object-oriented objects is to: • Identify the objects. Most likely these will be nouns. • Identify each object's attributes. What helps to describe the object? • Identify each object's actions. Most likely these will be verbs. • Identify the relationships from object to object. Most likely these will be verbs. For example: • A person is a human identified by a name. • A grade is an achievement identified by a letter. • A student is a person who earns a grade.
Syntax and semantics The
syntax of a
computer program is a
list of
production rules which form its
grammar. A programming language's grammar correctly places its
declarations,
expressions, and
statements. Complementing the
syntax of a language are its
semantics. The
semantics describe the meanings attached to various syntactic constructs. A syntactic construct may need a semantic description because a production rule may have an invalid interpretation. Also, different languages might have the same syntax; however, their behaviors may be different. The syntax of a language is formally described by listing the production rules. Whereas the syntax of a
natural language is extremely complicated, a subset of the English language can have this production rule listing: • a
sentence is made up of a
noun-phrase followed by a
verb-phrase; • a
noun-phrase is made up of an
article followed by an
adjective followed by a
noun; • a
verb-phrase is made up of a
verb followed by a
noun-phrase; • an
article is 'the'; • an
adjective is 'big' or • an
adjective is 'small'; • a
noun is 'cat' or • a
noun is 'mouse'; • a
verb is 'eats'; The words in
bold-face are known as
non-terminals. The words in 'single quotes' are known as
terminals. From this production rule listing, complete sentences may be formed using a series of replacements. The process is to replace
non-terminals with either a valid
non-terminal or a valid
terminal. The replacement process repeats until only
terminals remain. One valid sentence is: •
sentence •
noun-phrase verb-phrase •
article adjective noun verb-phrase •
the adjective noun verb-phrase •
the big noun verb-phrase •
the big cat verb-phrase •
the big cat verb noun-phrase •
the big cat eats noun-phrase •
the big cat eats article adjective noun •
the big cat eats the adjective noun •
the big cat eats the small noun •
the big cat eats the small mouse However, another combination results in an invalid sentence: •
the small mouse eats the big cat Therefore, a
semantic is necessary to correctly describe the meaning of an
eat activity. One
production rule listing method is called the
Backus–Naur form (BNF). BNF describes the syntax of a language and itself has a
syntax. This recursive definition is an example of a
metalanguage. ::= ::= + | - ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Notice the recursive production rule: ::= | This allows for an infinite number of possibilities. Therefore, a
semantic is necessary to describe a limitation of the number of digits. Notice the leading zero possibility in the production rules: ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Therefore, a
semantic is necessary to describe that leading zeros need to be ignored. Two formal methods are available to describe
semantics. They are
denotational semantics and
axiomatic semantics. ==Software engineering and computer programming==