420 likes | 732 Views
Chapter 6: Programming Languages. Computer Science: An Overview Eleventh Edition by J. Glenn Brookshear. 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.
E N D
Chapter 6:Programming Languages Computer Science: An OverviewEleventh Edition by J. Glenn Brookshear
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
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
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
Machine language156C166D505630CEC000 Assembly languageLD R5, PriceLD R6, ShippingChargeADDI R0, R5 R6ST R0, TotalCostHLT Program Example
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
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.
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).
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)
Figure 6.3 A function for checkbook balancing constructed from simpler functions
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
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.
Figure 6.4 The composition of a typical imperative program or program unit
Data Types • Integer: Whole numbers • Real (float): Numbers with fractions • Character: Symbols • Boolean: True/false
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
Figure 6.10 The procedure ProjectPopulation written in the programming language C
Figure 6.11 Executing the procedure Demo and passing parameters by value
Figure 6.12 Executing the procedure Demo and passing parameters by reference
Figure 6.13 The function CylinderVolume written in the programming language C
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.
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;
An example of lexical analysis letter_ -> A | B | C |…| Z | a | b |…| z | _ digit -> 0 | 1 | 2 |…| 9 id -> letter_(letter_ | digit)* Regular expression letter_ or digit letter_ start Finite state machine S1 S0
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
Figure 6.16 Syntax diagrams describing the structure of a simple algebraic expression
Figure 6.17 The parse tree for the string x + y x z based on the syntax diagrams in Figure 6.16
Figure 6.18 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2
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.
Figure 6.20 The structure of a class describing a laser weapon in a computer game
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
Object Integrity • Encapsulation: A way of restricting access to the internal components of an object • Private versus public
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
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(); }
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’); }
Polymorphism (Cont.) void g() { Array_stack as(200); f(as); } void h() { List_stack ls; f(ls); }