1 / 39

Computer Science: An Overview Eleventh Edition by J. Glenn Brookshear

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.

eman
Télécharger la présentation

Computer Science: An Overview Eleventh Edition by J. Glenn Brookshear

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6:Programming Languages Computer Science: An OverviewEleventh Edition by J. Glenn Brookshear

  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

  3. Figure 6.1 Generations of programming languages

  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

  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

  6. Machine language156C166D505630CEC000 Assembly languageLD R5, PriceLD R6, ShippingChargeADDI R0, R5 R6ST R0, TotalCostHLT Program Example

  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

  8. Figure 6.2 The evolution of programming paradigms

  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.

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

  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)

  12. Figure 6.3 A function for checkbook balancing constructed from simpler functions

  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

  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.

  15. Figure 6.4 The composition of a typical imperative program or program unit

  16. Data Types • Integer: Whole numbers • Real (float): Numbers with fractions • Character: Symbols • Boolean: True/false

  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

  18. Figure 6.10 The procedure ProjectPopulation written in the programming language C

  19. Figure 6.11 Executing the procedure Demo and passing parameters by value

  20. Figure 6.12 Executing the procedure Demo and passing parameters by reference

  21. Figure 6.13 The function CylinderVolume written in the programming language C

  22. Figure 6.14 The translation process

  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.

  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;

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

  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

  27. Figure 6.16 Syntax diagrams describing the structure of a simple algebraic expression

  28. Figure 6.17 The parse tree for the string x + y x z based on the syntax diagrams in Figure 6.16

  29. Figure 6.18 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

  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.

  31. Figure 6.20 The structure of a class describing a laser weapon in a computer game

  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

  33. Figure 6.21 A class with a constructor

  34. Object Integrity • Encapsulation: A way of restricting access to the internal components of an object • Private versus public

  35. Figure 6.22 Our LaserClass definition using encapsulation

  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

  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(); }

  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’); }

  39. Polymorphism (Cont.) void g() { Array_stack as(200); f(as); } void h() { List_stack ls; f(ls); }

More Related