The following are six reasons for studying concepts of programming languages:
Increasing the capacity to express ideas:
In the process of developing software, programmers can only use the control structures, the data structures, and the abstractions provided by the language that they have chosen, and these constrains place limits in the forms of algorithms that they can construct. For example, the fact that C/C++ programming language provides no basic data type to represent and manipulate fixed decimal numbers makes it difficult to represent and process dollars and cents in a C/C++ program.
The awareness of a wider variety of programming language features can reduce such limitations in two different ways:
By choosing the programming language with the best features to solve a particular problem.
By simulating features from one language in those that do not have them. For example, the string and fixed decimal basic data types of PL/1 can be simulated in C/C++.
The study of programming language concepts builds an appreciation for valuable language features and encourages programmers to use them.
Improving the background for choosing appropriate languages: that means to make an informed language choice for a new project.
Increasing the ability to learn new languages: by having a thorough understanding of the fundamental concepts of programming languages, it becomes easier to see how these concepts are incorporated into the design of the language being learned.
4. Better understanding of the significance of implementation: the understanding of how the features of a programming language are implemented helps to write efficient codes and improves debugging skills: For example, knowing how function calls and recursions are implemented will help a programmer to write and use functions more intelligently. Also, knowing how certain data structures such as arrays, records/structures, or unions are implemented in a programming language may help in the debugging process of a program.
5. Better use of languages that are already known: Many contemporary programming languages are large and complex and programmers tend to be familiar only with the features of the language that they use. By studying the concepts of programming languages, a programmer can learn about previously unknown and unused features of a language that they already knew.
Overall advancement of computing: the familiarity with programming language concepts by those in position to choose languages will help to make best languages widely accepted. For example FORTRAN was chosen over ALGOL 60 because programmers and software developer managers did not understand the conceptual design of ALGOL 60: they found its description difficult to read and understand, and did not appreciate the benefits of block structure, recursion, and well-structured controlled statements of ALGOL 60.
What are the six reasons for studying the concepts of programming languages?
How can the awareness of a wider variety of programming language features reduces the limitations that the control structures, data structures and abstractions of a programming language impose on a programmer?
II Programming/Application Domains
The appropriate language to use often depends on the application domain for the problem to be solved: problems to be solved by digital computers may be classified according to the features that they require from programming languages. Examples of these features are basic and structured data types, operations on data, and control structures. In general, these features are provided in a programming language to allow programmers to efficiently write programs to solve problems in a particular application domain. These application domains and their associated languages follow:
Scientific applications: are characterized by the following features:
Simple data structures are needed: most common data structures are arrays and matrices.
Problems require large numbers of floating-point arithmetic computations.
Most common control structures are counting loops and selections
FORTRAN is the most commonly used language in this domain. An example of FORTRAN program is provided in pages 46 and 47.
Business Applications: are characterized by the following features:
Production of elaborate reports
Need of precise ways for describing and storing character data (strings) and decimal numbers.
Ability to specify decimal arithmetic operations
COBOL is the most commonly used language in this domain. An example of COBOL program is provided in pages 61, 62, and 63.
AI problems (game playing, theorem proving, knowledge representation and reasoning, natural language processing, . . . etc) require information to be represented in symbolic form rather than numeric form. Symbolic data is conveniently represented using linked lists (for names) and logical formulas (for facts and rules).
The most commonly used languages in this domain are LISP for list representation of data and PROLOG for logical formula representation of data. Examples of LISP structures and their internal representations are provided in pages 49 and 50, and an example of LISP function is provided in page 51. Examples of Prolog statements are provided in page 80.
Systems software consists of the operating system and all the programming support tools of a computer system: compilers, assemblers, linkers, . . . etc.
These programs are mainly used for the operation of the computer system.
A language used to write software in this domain must provide low-level features that allow software interfaces to external devices to be written: system calls or APIs. They must also provide for fast execution.
C is the most commonly used system programming language.
A web site consists of one or more web pages.
A web page is a document or file that may contain text, graphics, music, animation, and video.
The content of a web document is specified by using a markup language such as HTML (XHTML).
Markup languages are not programming languages: they are used to specify the layout of information in Web documents. However, because of the need for dynamic Web contents, computations are often specified in a web documents.
Computations are specified in one of the following ways:
By embedding programming codes in an HTML (XHTML) document.
By making requests (in a Web document) that separate programs or procedures be executed either on the Web server or by the browser.
A Web application framework is a software framework that is designed to support the development of dynamic websites, Web applications, and Web services.
Many frameworks provide libraries for database access, frameworks to create templates, session management, and often promote code reuse.
The following are examples of Web application frameworks:
(Active Server Page) ASP.NET
Common Gateway Interface (CGI).
AWeb programming languages either has an associated Web application framework or provides features that can be used for the development of Web application frameworks.
The following are examples of Web programming languages:
Java Two types of Java programs reside on a Web server:
Applets are executed by the browser on the client computer.
Servlets are executed on the Web server.
C# and VB.NET are the most popular languages used to create Web applications using the ASP.NET platform. However, since ASP.NET is built on the Common Language Runtime, any supported .NET language can be used. Programs/procedures written in these languages are either embedded in a Web document or located on the Web server and are executed on the Web server.
PHP is a server-side HTML/XHTML- embedded scripting language.
Perl is the ideal language for the CGI programming. CGI programs reside and are executed on the Web server.
Other Web programming languages are Python, Ruby, Lua, Common Lisp, and Smalltalk.
General Purpose Languages
A general purpose language is a language that can be used to develop applications in two or more domains. Examples of general purpose languages are Java, Ada, C#, Perl, and C++, PL/1, Pascal.
RPG (Report Program Generator):
was introduced by IBM in the 1960s for the generation of reports.
It has a fixed coding structure with formats for:
input specification (files and data)
switch specification (Boolean)
calculation and process
The simulation of discrete systems is done by modeling these systems with a series of state changes.
For example, in the simulation of a soda machine system, the state of the system can be the amount of coins received so far and the types of soda cans remaining with their quantities.
A simulation technique follows the system elements through their change of state and gathers quantitative or factual information.
This information is then used to predict the properties of the system under hypothetical situations.
GPSS (General -Purpose Simulation System)
- is a simulation language that provides the user with a set of different block types to represent the activities of the system being simulated.
- The line joining the blocks indicates the logical sequence in which the activities can be executed.
- is a language for system description and simulation with the concept of class
- A system in this case is viewed as a collection of independent entities (objects) interacting with each other.
- For example, for the soda machine system, the entities could be the soda machine, the can container, and the users.
- Systems are simulated by examining the life cycles of the entities of the system.
1. You work in a company that wants to use only one programming language to implement all its programming applications. What is the most appropriate language in each of the following situations?
All applications have a lot of floating-point arithmetic computations and require only arrays as the major data structure.
All applications are used to compute payrolls and also to generate reports.
Some applications have a lot of floating-point arithmetic computations and require only arrays as the major data structure, but others are used to compute payrolls and also to generate reports.
All applications are used for game playing with symbolic manipulations of data represented as linked lists.
All applications are used for the operation of the computer.
All applications are embedded in HTLM documents, and are interpreted on the server before the document is sent to a requesting browser.
All applications are embedded in HTML documents and are interpreted by a browser that displays the document.
All applications are common Gateway Interface (CGI) programs for the World Wide Web.
What are the major characteristics of each of the following programming domains: Scientific and business applications.
What are the most commonly used languages in each of the above programming domains.
Provide two special-purpose languages with their characteristics.
Review questions 2, 3, 4, 5, and 6 page 32.
III Language Categories
High-level programming languages may be categorized as:
Logic programming language
object-oriented programming languages.
Each of these categories of programming languages corresponds to the major software development methodologies.
Imperative Languages have the following major features:
variables are used to represent memory locations
assignment statements are used to store values in memory locations
use of iteration as major form of repetition
an algorithm is specified in great detail and the specific order of execution of the instructions/statements must be included.
Examples of imperative languages are C, Pascal, PL/1, COBOL, FORTRAN
Visual Languages such as Visual Basic .Net form a subcategory of imperative languages.
These languages include capabilities for drag-and-drop generation of code segments.
They also provide a simple way to generate graphical user interfaces to programs.
For example, in VB.NET, the code to produce a display or a form control such as a button or a text box can be created with a single keystroke.
Functional (Applicative) Languages are characterized by the following:
computation is mostly done by applying functions to arguments
Variables (symbols) do not necessarily represent memory locations and can be bound to values (atoms or lists) by special forms or when a function is called.
recursion is the primary means of repetition
Examples of functional languages are Lisp, Scheme, ML (Meta Language), and Miranda
Examples of program constructs from Lisp:
(A B C D) is a simple list; A, B, C and D are atoms
(A (B C) D (E F)) is a nested list
(SETQ X 3) bind the value 3 to the symbol X
(+ 3 7) call to function + with arguments 3 and 7
Recursive definition of a function to compare two lists and return TRUE if both list are equal and FALSE otherwise.
(DEFUN EQUAL_LIST (LIST1 LIST2)
((ATOM LIST1) (EQ LIST1 LIST2))
((ATOM LIST2) >NIL)
(T (AND (EQUAL_LIST (CAR LIST1) (CAR LIST2))
(EQUAL_LIST (CDR LIST1 (CDR LIST2))))))
(EQUAL_LIST (A B C) (A B C)) will return true whereas
(EQUAL_LIST (A B ) (A B C)) will return false.
Logic Programming Languages are characterized by the following:
use of logical statements (facts and rules) to describe a program
Programming in logic is nonprocedural because there is no way to specify in a program exactly how a result can be computed. The rules are specified in no particular order.
computation is performed by deriving new facts from existing ones using an inferencemechanism (resolution) and the rules
An example of logic program is Prolog
Examples of Prolog statements:
mother( ann, peter). ann is the mother of peter
father(bob, peter). bob is the father of peter
parent(x, y) :- father(x, y). x is a parent of y if it is his father
grandparent(x, z) :- parent(x, y) , parent(y, z). x is a grand parent of z if there is a y such that x is parent of y and y is a parent of z
The question grandparent(mark, peter)? returns true
Whereas the question grandparent(ann, mark)? returns false.
Object-Oriented Programming Languages have the following characteristics:
Data abstraction and information hiding: A programmer may create a class and hide from its clients the implementation of that class (information hiding) and a client of a class need not know how a class is implemented in order to use its functionality. The description of class functionality is independent of its implementation. For example, the class stack (with the methods push and pop).
Inheritance: The definition of one class (derived class) can be derived from that of another class (base class). For example, graduate student from student, . . . etc
Dynamic binding of the methods to the messages (function calls): a function call is bound to a function at execution time.
In object-oriented programming, a program is viewed as a collection of entities interacting with each other.
An example of object-oriented programming language is Smalltalk.
Languages such as C++, Eiffel and Java combine imperative and object-oriented features. These languages are said to support object-oriented programming and are considered as a subcategory of imperative languages.
double getpay() const; // calculate and return salary
Void print() const; //overriden base-class print
IV Influences on Language Design
The major influences on language design have been machine architecture and software design methodologies.
Machine architecture: imperative languages have been designed based on the von Neumann architecture: CPU, main memory, and I/O systems. The program to be executed and its input data are stored in the main memory
Software design methodologies: programming languages have been designed to support either the top down design and step-wise refinement (process-oriented) or the object-oriented programming methodology.
1. a) Provide the characteristics of the following high-level programming language categories: imperative languages; functional (applicative) languages; logic programming languages; object-oriented programming languages.
b) Provide one or more examples of each of the above programming language categories.
c) What are the major types of statements (with their meanings) of an imperative language?
A programming language is characterized by its lexical elements, its syntax, and its semantics.
The Lexical Elements: consist of the following:
The character set: set of all characters allowed in the text of a program.
The rules for grouping characters into words (lexems) (For example, the rules for constructing identifiers, integer constants, string constants, . . . etc)
The use of reserved words and keywords
the manner in which blanks and line termination characters are handled
How comments are written
lexems are grouped into categories called tokens
The rules for specifying tokens may be specified using formal systems such as regular expressions or context-free grammars (CFG)
The Syntax of a Programming Language
It describes the correct form of the syntactic units of a programming language such as arithmetic expressions, assignment statements, programs, procedures, functions, . . . , etc.
It is specified by providing the rules for constructing the syntactic units from tokens and other syntactic units.
These rules are written by using context-free grammars, Bakus-Naur Forms or syntax graphs. For example, consider a programming language in which a program start with the keyword BEGIN and ends with the keyword END and consists of one or more assignment statements. An arithmetic expression is either a variable, a constant, or the addition of two arithmetic expressions. The syntactic units of this programming language may be specified using CFG as follows:
----> identifier =
The Semantics of Programming Languages
It describes the meanings that may be attached to its syntactic units. There are two types of semantics:
Static Semantics: are the rules that describe certain constraints of the language such as all identifiers must be declared, data types of operands and operators are compatible, functions are called with the proper number of arguments, . . . etc
Dynamic (Run-Time) Semantics: specify what each syntactic unit does and how it should be translated.
Static semantics are specified using attribute grammars
The major formalisms that are used to specify dynamic semantics are: operational semantics, axiomatic semantics, and denotational semantics
1. a) What are the three things that characterize a programming language?
b) What are the lexical elements of a programming language?
c) Provide the two types of semantics (each with a brief description) of a programming language
VI Language Implementation Methods
There are three major methods for implementing programming languages:
Compiler implementation: a high-level language program is translated into a machine language program, which can be executed directly on the computer. Examples are C/C++, Pascal, and COBOL. Refer to Figure 1.3 in page 26.
Hybrid implementation: the high-level language program is translated into an intermediate language designed to allow easy interpretation. Refer to Figure 1.5, page 29.
The intermediate code is then executed using an interpreter. Examples are Perl, Lisp, and initial implementations of Java.
Current implementation of Java and the .NET languages use Just-in time (JIT) implementations: the intermediate code of a method is compiled into machine language when it is called for the first time.
VII. Structure of a Compiler (refer to Figure 1.3, page 26)
The four major components of a compiler are the lexical analyzer (or scanner), the syntax analyzer (or parser), the intermediate code generator (that contains the semantic analyzer), and the code generator.
The lexical analyzer reads a source program character by character; groups those characters into words (lexems), and classifies those words into tokens and comments.
The syntax analyzer takes as input the lexems produced by the lexical analyzer; and groups the lexems into the syntactic structures of the program. In some cases, it also generates the parse tree of each syntactic structure.
The semantic analyzer/intermediate code generator takes as input the parse trees of the syntactic structures produced by the syntax analyzer. The semantic analyzer checks for errors such as type errors on the syntactic structures, and the intermediate code generator generates the intermediate (sometimes assembly language) codes that correspond to the syntactic structures.
The code generator takes as input the intermediate codes produced by the intermediate code generator, and translates the intermediate codes into machine language.
Some compilers also have an optimization unit which takes as input a program in an intermediate code and make it smaller and/or faster.
The symbol table is used in the compilation process to hold all identifiers (user-defined names) of a program with their type and attribute information. This information is placed in the table by the lexical analyzer and the syntax analyzer, and is used by the semantic analyzer and the code generator.
The preprocessor is a program that processes a program source module immediately before it is compiled.
Examples of preprocessor directives in the C/C++ language are #include and #define.
Examples: given the following preprocessor directives
#define MAX 50
#define average(A, B) (A + B) / 2
The statement: result = average( (num1 +5), num2);
will be expanded by the preprocessor to the following statement: