cs382 introduction

Upload: userraj

Post on 10-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 CS382 Introduction

    1/13

    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:

    a) By choosing the programming language with the best features to solve a particular

    problem.

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

    2. Improving the background for choosing appropriate languages: that means to make

    an informed language choice for a new project.

    3. Increasing the ability to learn new languages: by having a thorough understanding ofthe 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. Increasing the ability to design new languages: simple examples of such languages areinput format languages for interactive software that require users to traverse several levels of

    menus and enter a variety of commands, as in the case of word processors, or query languages as

    in the case of databases.

    6. Overall advancement of computing: the familiarity with programming language

    -1-

  • 8/8/2019 CS382 Introduction

    2/13

    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.

    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:

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

    b) Problems require large numbers of floating-point arithmetic computations.

    c) 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 48 and 49.

    2) Business Applications: are characterized by the following features:

    a) Production of elaborate reports

    b) Need of precise ways for describing and storing character data (strings) and

    decimal numbers.

    c) 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 63, 64, and 65.

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

    -2-

  • 8/8/2019 CS382 Introduction

    3/13

    data and PROLOG for logical formula representation of data. Examples of LISP

    structures and their internal representations are provided in page 52, and an example of

    LISP function is provided in pages 52 and 53. Examples of Prolog statements are

    provided in page 82.

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

    5) 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:

    o By embedding programming codes in an HTML (XHTML) document.

    o 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 frameworkis 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:

    o (Active Server Page) ASP.NET

    o Common Gateway Interface (CGI).

    -3-

  • 8/8/2019 CS382 Introduction

    4/13

    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:

    o JavaScript Java Script statements are embedded in Web documents and

    are interpreted by the browser.

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

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

    o PHP is a server-side HTML/XHTML- embedded scripting language.

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

    III Language Categories

    High-level programming languages may be categorized as imperative,functional, logic orobject-

    oriented. These categories of programming languages correspond to the major software development

    methodologies.

    -4-

  • 8/8/2019 CS382 Introduction

    5/13

    Imperative Languages have the following major features:

    a. variables are used to represent memory locations

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

    c. use of iteration as major form of repetition

    d. an algorithm is specified in great detail and the specific order of execution of the

    instructions/statements must be included.

    e. 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 atext box can be created with a single keystroke.

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

    Functional (Applicative) Languages are characterized by the following:

    a. computation is mostly done by applying functions to arguments

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

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

    -5-

  • 8/8/2019 CS382 Introduction

    6/13

    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:

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

    b. Programming in logic is nonproceduralbecause there is no way to specify in a programexactly how a result can be computed. The rules are specified in no particular order.

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

    -6-

  • 8/8/2019 CS382 Introduction

    7/13

    Object-Oriented Programming Languages have the following characteristics:

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

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

    c. Dynamic binding of the methods: 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 subcategoryof imperative languages.

    Example of class inheritance in C++:

    class employee {

    public:

    employee(const char *, const char *); // constructor

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

    ~employee(): // destructor

    private:

    char *firstname;

    char *lastname;

    };

    class hourlyworker : public employee {

    public:

    hourlyworker (const char * , const char *, double, double);

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

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

    private:

    double wage;

    double hours;

    };

    -7-

  • 8/8/2019 CS382 Introduction

    8/13

    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:

    a. input specification (files and data)b. switch specification (Boolean)

    c. calculation and process

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

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

    -8-

  • 8/8/2019 CS382 Introduction

    9/13

    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.

    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 orcontext-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 orsyntax 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

    -9-

  • 8/8/2019 CS382 Introduction

    10/13

    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

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

    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 andproduces the result(s) of the program. Example of pure interpreted languages are

    JavaScript and PHP. Refer to Figure 1.4, page 30.

    Hybrid implementation: the high-level language program is translated into an

    intermediate language designed to allow easy interpretation. Refer to Figure 1.5, page 31.

    -10-

  • 8/8/2019 CS382 Introduction

    11/13

    o The intermediate code is then executed using an interpreter. Examples are Perl, Lisp,

    and initial implementations of Java.

    o 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 27)

    The four major components of a compiler are the lexical analyzer(orscanner), thesyntax

    analyzer(orparser), the intermediate code generator(that contains thesemantic analyzer), and

    the code generator.

    The lexical analyzer reads a source program character by character; groups those charactersinto 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 thelexems into the syntactic structures of the program. In some cases, it also generates the parse treeof each syntactic structure.

    The semantic analyzer/intermediate code generator takes as input the parse trees of thesyntactic structures produced by the syntax analyzer. The semantic analyzer checks for errorssuch as type errors on the syntactic structures, and the intermediate code generator generatesthe intermediate (sometimes assembly language) codes that correspond to the syntactic structures.

    The code generator takes as input the intermediate codes produced by the intermediate codegenerator, 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.

    -11-

  • 8/8/2019 CS382 Introduction

    12/13

    Examples:

    #include

    #include mylib.h

    #define MAX 50

    #define average(A, B) (A + B) / 2

    Review Questions:

    Part 1: Pages 34 and 35:

    Review questions 2, 3, 4, 5, 7, 12, 20, 22, 23, 25, 26, 27, 28, and 30.

    Part 2:

    1. You work in a company that wants to use only one programming language to implement all itsprogramming applications. What is the most appropriate language in each of the following situations?

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

    major data structure.

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

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

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

    lists.

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

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

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

    document.

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

    2.

    a. What are the major characteristics of each of the following programming

    domains: Scientific and business applications.

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

    domains.

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

    3.

    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

    -12-

  • 8/8/2019 CS382 Introduction

    13/13

    categories.

    c) What are the major types of statements (with their meanings) of an imperative

    language?

    4.

    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 aprogramming language

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

    -13-