Introduction I reasons for Studying Concepts of Programming Languages



Download 48.01 Kb.
Date28.01.2017
Size48.01 Kb.
INTRODUCTION

I Reasons for Studying Concepts of Programming Languages

The following are six reasons for studying concepts of programming languages:



  1. 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:

  1. By choosing the programming language with the best features to solve a particular problem.

  2. 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.

  1. Improving the background for choosing appropriate languages: that means to make an informed language choice for a new project.

  2. 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.



  1. 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.



Exercise 1

  1. What are the six reasons for studying the concepts of programming languages?

  2. 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:

  1. Scientific applications: are characterized by the following features:

  1. Simple data structures are needed: most common data structures are arrays and matrices.

  2. Problems require large numbers of floating-point arithmetic computations.

  1. 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.

  1. Business Applications: are characterized by the following features:

  1. Production of elaborate reports

  2. Need of precise ways for describing and storing character data (strings) and decimal numbers.

  3. 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.



  1. Artificial Intelligence:

  • 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.

  1. Systems Programming:

  • 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.

  1. Web Software

  • 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).

  • A Web 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:

    • JavaScript Java Script statements are embedded in Web documents and are interpreted by the browser.

    • 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.



Special-Purpose Languages

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:


  1. input specification (files and data)

  2. switch specification (Boolean)

  3. calculation and process

  4. output specification

Simulation Languages:

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.

SIMULA

- 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.

Exercise 2

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?



    1. All applications have a lot of floating-point arithmetic computations and require only arrays as the major data structure.

    2. All applications are used to compute payrolls and also to generate reports.

    3. 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.

    4. All applications are used for game playing with symbolic manipulations of data represented as linked lists.

    5. All applications are used for the operation of the computer.

    6. All applications are embedded in HTLM documents, and are interpreted on the server before the document is sent to a requesting browser.

    7. All applications are embedded in HTML documents and are interpreted by a browser that displays the document.

    8. All applications are common Gateway Interface (CGI) programs for the World Wide Web.

2.

  1. What are the major characteristics of each of the following programming domains: Scientific and business applications.

  2. What are the most commonly used languages in each of the above programming domains.

  3. Provide two special-purpose languages with their characteristics.

  1. Review questions 2, 3, 4, 5, and 6 page 32.

III Language Categories

  • High-level programming languages may be categorized as:

  • Imperative languages

  • Functional languages

  • 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:

  1. variables are used to represent memory locations

  2. assignment statements are used to store values in memory locations

  3. use of iteration as major form of repetition

  4. an algorithm is specified in great detail and the specific order of execution of the instructions/statements must be included.

  5. 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.

  • These capabilities are also available in all the other four .NET languages: C#, JScript (Microsoft’s version of JavaScript), J# (Microsoft version of Java) and managed C++.

Functional (Applicative) Languages are characterized by the following:

  1. computation is mostly done by applying functions to arguments

  2. 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.

  3. 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)

(COND


((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:

  1. use of logical statements (facts and rules) to describe a program

  2. 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.

  3. computation is performed by deriving new facts from existing ones using an inference mechanism (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

father(mark, ann).

father(roy, bob).

parent(x, y) :- father(x, y). x is a parent of y if it is his father

parent(x, y) :- mother(x, y). x is a parent of y if it is his mother

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:


  1. 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).

  2. 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

  3. 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.

Example of class inheritance in C++:

class employee {

public:

employee ( ); // default constructor



employee(const string, const string); // constructor

void print() const; // print first and last name

private:

string firstname;

string lastname;

};
class hourlyworker : public employee {

public:

hourlyworker( )



hourlyworker (const string , const string, double, double);

double getpay() const; // calculate and return salary

Void print() const; //overriden base-class print

private:


double wage;

double hours;

};

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.

Exercise 3

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?

2. Review questions 12, 20, 22, and 23, pages 32, 33.

V Language Implementation


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

Notes

  • 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:

----> BEGINEND



---->

---->

----> identifier =

----> identier

----> constant

----> +

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.

Notes


  • 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

Exercise 4

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.

  • Pure interpretation: a program called interpreter takes as input the high-level language program and its input data; it executes the statements of this program on the computer and produces the result(s) of the program. Example of pure interpreted languages are JavaScript and PHP. Refer to Figure 1.4, page 28.

  • 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.


Preprocessors

  • 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

#include

#include “mylib.h”

#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:

result = ( (num1 +5) + num2) /2;



Exercise 5

1. Review questions 25, 26, 27, 28, and 30, page 33.

2. a) Provide the three major methods (each with an example) of implementing programming languages.

b) What is the difference between a language interpreter and a compiler?

c) Provide the major components of a compiler with a brief description of their functions.

d) Provide a description of the functions of the linker and loader.






--






Download 48.01 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2020
send message

    Main page