Computer Science: An Overview Eleventh Edition by J. Glenn Brookshear
description
Transcript of Computer Science: An Overview Eleventh Edition by J. Glenn Brookshear
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Computer Science: An OverviewEleventh Edition
by J. Glenn Brookshear
Chapter 6:Programming Languages
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-2
Chapter 6: Programming Languages
• 6.1 Historical Perspective• 6.2 Traditional Programming Concepts• 6.3 Procedural Units• 6.4 Language Implementation• 6.5 Object Oriented Programming
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-3
Figure 6.1 Generations of programming languages
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-4
Second-generation:Assembly language
• A mnemonic system for representing machine instructions– Mnemonic names for op-codes– Identifiers: Descriptive names for memory
locations, chosen by the programmer
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-5
Assembly Language Characteristics
• One-to-one correspondence between machine instructions and assembly instructions– Programmer must think like the machine
• Inherently machine-dependent• Converted to machine language by a
program called an assembler
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-6
Program Example
Machine language
156C166D505630CEC000
Assembly language
LD R5, PriceLD R6, ShippingChargeADDI R0, R5 R6ST R0, TotalCostHLT
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-7
Third Generation Language
• Uses high-level primitives– Similar to our pseudocode in Chapter 5
• Machine independent (mostly)• Examples: FORTRAN, COBOL• Each primitive corresponds to a sequence
of machine language instructions• Converted to machine language by a
program called a compiler
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-8
Figure 6.2 The evolution of programming paradigms
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-9
Declarative paradigm
• A programmer’s tasks are to describe the problem to be solved rather than an algorithm to be followed.
• Apply a pre-established general-purpose problem-solving algorithm
• Applications: Simulations for weather forecasting, political, economic, environmental systems, etc.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-10
Declarative Programming: Prolog
• Fact: A Prolog statement establishing a fact– Consists of a single predicate
– Form: predicateName(arguments).• Example: parent(bill, mary).
• Rule: A Prolog statement establishing a general rule– Form: conclusion :- premise.
• :- means “if”
– Example: wise(X) :- old(X).– Example: faster(X,Z) :- faster(X,Y), faster(Y,Z).
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-11
Declarative Programming: Prolog (Cont.)
– Example: a set of initial statements (facts and rules) faster(turtle, snail) faster(rabbit, turtle)
faster(X,Z) :- faster(X,Y), faster(Y,Z).
Questions: faster(turtle, snail)? Yes
faster(rabbit, turtle)? Yes
faster(rabbit, snail)? Yes
faster(W, snail)? faster(turtle, snail)
faster(rabbit, snail)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-12
Figure 6.3 A function for checkbook balancing constructed from simpler functions
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-13
Figure 6.3 A function for checkbook balancing constructed from simpler functions (Cont.)
• In LISP functional programming language
(Find_diff (Find_sum Old_balance Credits) (Find_sum Debits))
• In the imperative paradigm Total_credits <- sum of all Credits
Temp_balance <- Old_balance + Total_credits
Total_debits <- sum of all Debits
Balance <- Temp_balance – Total_debits
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-14
Object-oriented paradigm
• A software system is viewed as a collection of objects.
• For example, to process a list of names– In the traditional imperative paradigm,
• The list is merely a collection of data
• Any program unit accessing the list would have to contain the algorithms for performing the required manipulations
– In the object-oriented paradigm, • The list is constructed as an object that consisted of the list
together with the algorithms.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-15
Figure 6.4 The composition of a typical imperative program or program unit
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-16
Data Types
• Integer: Whole numbers• Real (float): Numbers with fractions• Character: Symbols• Boolean: True/false
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-17
Procedural Units
• A procedure: a set of instructions for performing a task that can be used as an abstract tool by other program units.
• Local versus Global Variables• Formal versus Actual Parameters• Passing parameters by value versus
reference• Procedures versus Functions
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-18
Figure 6.10 The procedure ProjectPopulation written in the programming language C
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Figure 6.11 Executing the procedure Demo and passing parameters by value
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Figure 6.12 Executing the procedure Demo and passing parameters by reference
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-21
Figure 6.13 The function CylinderVolume written in the programming language C
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-22
Figure 6.14 The translation process
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-23
The lexical analyzer, parser and code generator
• The lexical analysis: the process of – recognizing which strings of symbols from the
source program represent a single entity called token
– identifying whether they are numeric values, words, arithmetic operators, and so on.
• The parsing process: group tokens into statements based on a set of rules, collectively called a grammar.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-24
The lexical analyzer, parser and code generator (Cont.)
• The code generation process: – constructing the machine-language instructions to
implement the statements recognized by the parser– performing code optimization, e.g.,
x <- y + z;
w <- x +z;
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-25
An example of lexical analysis
letter_ -> A | B | C |…| Z | a | b |…| z | _
digit -> 0 | 1 | 2 |…| 9
id -> letter_(letter_ | digit)*
S0 S1
letter_ or digit
letter_start
Regular expression
Finite state machine
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-26
Figure 6.15 A syntax diagram of our if-then-else pseudocode statement
• Terminals: appear in ovals (i.e., if, then, else)
• Nonterminals: appear in rectangles, which require further description
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-27
Figure 6.16 Syntax diagrams describing the structure of a simple algebraic expression
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-28
Figure 6.17 The parse tree for the string x + y x z based on the syntax diagrams in Figure 6.16
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Figure 6.18 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-30
Objects and Classes
• Object: Active program unit containing both data and procedures
• Class: A template from which objects are constructed
An object is called an instance of the class.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-31
Figure 6.20 The structure of a class describing a laser weapon in a computer game
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-32
Components of an Object
• Instance Variable: Variable within an object– Holds information within the object
• Method: Procedure within an object– Describes the actions that the object can perform
• Constructor: Special method used to initialize a new object when it is first constructed
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-33
Figure 6.21 A class with a constructor
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-34
Object Integrity
• Encapsulation: A way of restricting access to the internal components of an object– Private versus public
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-35
Figure 6.22 Our LaserClass definition using encapsulation
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-36
Additional Object-oriented Concepts
• Inheritance: Allows new classes to be defined in terms of previously defined classes
• Polymorphism: Allows method calls to be interpreted by the object that receives the call
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-37
Polymorphism
class Stack{public: virtual void push(char c) = 0; virtual char pop() = 0;}class Array_stack : pubilc Stack{ char *p; int max_size; int top;public: Array_stack(int s); ~Array_stack(); void push(char c); char pop();}
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-38
Polymorphism (Cont.)
class List_stack : pubilc Stack{ list<char> lc;public: List_stack(); ~List_stack(); void push(char c); char pop();}void f(Stack& s_ref){ s_ref.push(‘c’);}
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-39
Polymorphism (Cont.)
void g(){ Array_stack as(200); f(as);}void h(){ List_stack ls; f(ls);}