Introduction Computer science is the discipline that seeks

  • Slides: 27
Download presentation
Introduction • Computer science is the discipline that seeks to build a scientific foundation

Introduction • Computer science is the discipline that seeks to build a scientific foundation for a variety of topics. • Computer science provides the underpinnings for today’s computer applications as well as the foundations for tomorrow’s applications.

Ch. 5 Programming Languages • • • Historical perspective. Traditional programming concepts. Program units.

Ch. 5 Programming Languages • • • Historical perspective. Traditional programming concepts. Program units. Language implementation. Parallel computing. Declarative programming.

Historical Perspective • Machine language - binary form; directly control the hardware. • Assembly

Historical Perspective • Machine language - binary form; directly control the hardware. • Assembly language - mnemonic form of the machine language. • From machine language to assembly language – still low level – still machine-dependent

Historical Perspective • Higher level language – machine-independent • portability – English-like language •

Historical Perspective • Higher level language – machine-independent • portability – English-like language • Total=Price+Tax – compiler Vs. interpreter

Historical Perspective HLL Compiler Machine independent Assembler 1 Assembler n Machine dependent Arch 1

Historical Perspective HLL Compiler Machine independent Assembler 1 Assembler n Machine dependent Arch 1 Arch n

Historical Perspective • • 1 st-generation - machine language. 2 nd-generation - assembly language.

Historical Perspective • • 1 st-generation - machine language. 2 nd-generation - assembly language. 3 rd-generation - machine independent. 4 th-generation - software packages that allow users to customize computer software to their applications without needing technical expertise.

Historical Perspective Problems solved in an environment in which the human must conform to

Historical Perspective Problems solved in an environment in which the human must conform to the machine’s characteristics 1 st Problems solved in an environment in which the machine conforms to the human’s characteristics 4 th

Programming Paradigms • Imperative paradigm: based on CPU’s fetch-decode-execute cycle. – development of a

Programming Paradigms • Imperative paradigm: based on CPU’s fetch-decode-execute cycle. – development of a sequence of commands which manipulate data to produce the result – procedure paradigm – machine languages, FORTRAN, COBOL, ALGOL, BASIC, APL, C, PASCAL, ADA

Programming Paradigms • Declarative paradigm implements a general problem-solving algorithm. – GPSS, Prolog –

Programming Paradigms • Declarative paradigm implements a general problem-solving algorithm. – GPSS, Prolog – what is the problem? NOT how to solve the problem – 一個親戚關係的問題

Programming Paradigms parent(X, Y) : - mother(X, Y) parent(X, Y) : - father(X, Y)

Programming Paradigms parent(X, Y) : - mother(X, Y) parent(X, Y) : - father(X, Y) sibling(X, Y): - mother(M, X), mother(M, Y), father(F, X), father(F, Y) grandparent(X, Z): - parent(X, Y), parent(Y, Z)

Programming Paradigms • Functional paradigm – views the process of program development as the

Programming Paradigms • Functional paradigm – views the process of program development as the construction of “black boxes, ” each accepts inputs and produces outputs (Divide (Sum Numbers)(Count Numbers)) (First (Sort List)) – modular approach – LISP, ML, Scheme

Programming Paradigms • Object-oriented paradigm: units of data are viewed as active “objects” rather

Programming Paradigms • Object-oriented paradigm: units of data are viewed as active “objects” rather than the passive units envisioned by the imperative paradigm. • SIMULA, Smalltalk, C++, Ada 95, Java. Data methods

Programming Paradigms • Active object: data and a collection of procedures for manipulating the

Programming Paradigms • Active object: data and a collection of procedures for manipulating the data. – icons: click and drag – NOT designing an algorithm to manipulate the data, but asking the object to do it itself – passing messages as in computer networks – building block and software reuse • CORBA: implementing message passing between objects in a network.

Traditional Programming Concepts • Statements in programming languages tend to fall into three categories:

Traditional Programming Concepts • Statements in programming languages tend to fall into three categories: declarative statements, imperative statements, and comments. • Declarative statements - define customized terminology used in the program. • Imperative statements - describe steps in the underlying algorithm. • Comments.

Traditional Programming Concepts • Variable, literal, and constant. • Data type - integer, real,

Traditional Programming Concepts • Variable, literal, and constant. • Data type - integer, real, Boolean, character (Figure 5. 4). • Data structure - homogeneous array and heterogeneous array (Figures 5. 5 and 5. 6). • Assignment statement. – operator precedence • 2*4+6/2

Traditional Programming Concepts – overloading: the meaning of a symbol is determined by the

Traditional Programming Concepts – overloading: the meaning of a symbol is determined by the data types of the operands • “+” can be addition or concatenation • Control statement. – rat’s nests by “go to” – Figures 5. 7 and 5. 8 – only a few of these structures are needed • the choice of which to incorporate into a language is a design decision • structured programming • Comments - internal documentation.

Procedural Units • Breaking large programs into manageable units. • Procedures. – local variable

Procedural Units • Breaking large programs into manageable units. • Procedures. – local variable and global variable – parameters in procedure’s header • formal parameters and actual parameters • pass by value and pass by reference (Figures 5. 10 and 5. 11) • Functions. • I/O statements.

Language Implementation • Translation - converting a program from one language to another. •

Language Implementation • Translation - converting a program from one language to another. • Translation involves three activities (Figure 5. 12): – Lexical analysis – Parsing – Code generation • Lexical analysis - recognizing which strings of symbols from the source program represent a single entity.

Language Implementation • Parsing - identifying the grammatical structure of the program and recognizing

Language Implementation • Parsing - identifying the grammatical structure of the program and recognizing the role of each component. – The man the horse that lost the race threw was not hurt – Fixed-format languages Vs. free-format languages • punctuation marks/key words/reserved words – Syntax rules by diagrams (Figures 5. 13 and 5. 14)

Language Implementation – Parse tree (Figure 5. 15) • parsing -> constructing parse trees

Language Implementation – Parse tree (Figure 5. 15) • parsing -> constructing parse trees • one string -> one parse tree (Figure 5. 16) – Parsing declarative statements -> symbol table (data types) – Total = Price + Tax • • integer addition op-code floating-point addition op-code coercion strongly typed

Language Implementation • Code generation - constructing the machine language instructions to simulate the

Language Implementation • Code generation - constructing the machine language instructions to simulate the statements recognized by the parser. • Code optimization. – x = y + z; w = x + z; – x and z need not be loaded from memory for computing w

Language Implementation • Linker - link all necessary object programs to produce a complete,

Language Implementation • Linker - link all necessary object programs to produce a complete, executable program. – Load module • Loader - place the program in memory for execution. – Multitasking – Relocatable module • a jump instruction must jump to the correct address within the program • Figure 5. 18.

Object-Oriented Programming • class Small. Business { … } • Small. Business. X; •

Object-Oriented Programming • class Small. Business { … } • Small. Business. X; • class Mail. Order Business extends Small. Business {…} • Mail. Order. Business. Y; – Inheritance – Polymorphism Vs. overloading • Encapsulation: restrict access to an object’s internal properties

Parallel Computing • Developing languages for describing processes that execute simultaneously. • Process spawning.

Parallel Computing • Developing languages for describing processes that execute simultaneously. • Process spawning. • Interprocess communication • Monitor controls access to shared data – Object-oriented paradigm

Declarative Programming • Logical deduction - resolution. – From P OR Q and R

Declarative Programming • Logical deduction - resolution. – From P OR Q and R OR -Q, we conclude P OR R – From -P -> Q and Q -> R, we conclude -P -> R • Resolution can be applied only to pairs of statements that appear in clause form. • Inconsistent collection of statements – P and -P – Repeated application of resolution produces an empty clause (Figure 5. 20)

Declarative Programming • To conform that a collection of statements implies P => to

Declarative Programming • To conform that a collection of statements implies P => to contradict -P => to apply resolution to the original collection of statements and -P to produce an empty clause • From (Mary is at X) -> (Mary’s lamb is at X) and Mary is at home, we conclude (Mary’s lamb is at home) – unification

Declarative Programming • Prolog (PROgramming in LOGic) - a declarative programming language based on

Declarative Programming • Prolog (PROgramming in LOGic) - a declarative programming language based on repeated resolution. • Facts: faster(turtle, snail). faster(rabbit, turtle). • Rule: faster(X, Z) : - faster(X, Y), faster(Y, Z). • Deduce faster(rabbit, snail). • faster(W, snail). faster(rabbit, W). faster(V, W). faster(snail, rabbit).