chapter 6 programming languages. 2 chapter 6: programming languages 6.1 historical perspective 6.2...

69
Chapter 6 Programming Languages

Post on 21-Dec-2015

227 views

Category:

Documents


4 download

TRANSCRIPT

Chapter 6

Programming Languages

2

Chapter 6 Programming Languages 61 Historical Perspective 62 Traditional Programming Concepts 63 Procedural Units 64 Language Implementation 65 Object Oriented Programming 66 Programming Concurrent Activities 67 Declarative Programming

3

Programming Languages

Complex software system like OS would be developed impossibly using machine instructions directly

Programming languages are designed for humans to express the algorithm easily yet are convertible to the machine instructions on the target platform

As such the details of registers memory addresses and even machine cycles are out of our focus

We may then concentrate on the properties of the problems in question

4

Historical Perspective Evolution of computer languages1048708 Micro-instructions Inside CPU they implement each machine

instruction1048708 Machine language binary form direct controls the hardware1048708 Eg ldquo4032rdquo R3lArrR2 very hard to read amp write the programs1048708 Assembly language mnemonic form of the machine language1048708 Eg ldquoMV R3 R2rdquois used to refer to ldquo4032rdquo1048708 Descriptive name like ldquoPricerdquo is used to represent some memory

address1048708 ldquoLD R5 Pricerdquo(156C) load the content in the address ldquoPricerdquo0X6C to R51048708 Mnemonic ADDI amp ADDF refer to integer amp floating-point

additions1048708

5

Historical Perspective

This is a major syntax of revolution in programming1048708

Assemblers are the programs which translate the program written in an assembly language to the target machine language1048708

Shortages still machine-dependent and too naiumlve to develop large-scaled programs

6

Historical Perspective High-level language English like language for more

productive development1048708FORTRAN (Formula Translator) for scientific and

engineering applications1048708COBOL (Common Business-Oriented Language) from the

US Navy1048708The approach is to identify a collection of high-level

programming primitives to construct the software blocks [in essentially the same spirit with pseudo codes]1048708

Translators or compilers(initiated by Grace Hopper) are used to compile several machine instructions in the target machines to simulate the activity represented by a single high-level primitive [interpreters]

7

Historical PerspectiveInterpretersmdashsimilar to translators except they

executed the instructions as they were translatedie rather than producing a machine language copy of a

program that would be executed later an interpreter actually executed a program from its high level form

Programming languages are constructed around a small set of primitives that could be expressed in a variety of natural languages with only simple modification to the translators

Natural language(English German) evloved over time without formal grammatical analysis

Formal language(programming language)mdashare precisely defined by grammars

8

Figure 61 Generations of programming languages

9

Second-generationAssembly language A mnemonic system for

representing programs Mnemonic names for op-codes Names for all registers Identifiers = descriptive names for

memory locations chosen by the programmer

10

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

11

Assembly language example

Machine language

156C166D505630CEC000

Assembly language

LD R5 PriceLD R6 ShippingChargeADDI R0 R5 R6ST R0 TotalCostHLT

12

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 short

sequence of machine language instructions

Converted to machine language by a program called a compiler

13

Machine Independence and Beyond Through compiling programs can be portedeasily to any

other machine1048708 The efficiency of compilers also determines the quality of

executable programs1048708 A programming language may have several dialects over

different machines1048708 Standardization ANSI and ISO have adopted and

published several standards for several popular programming languages [to improve compatibility]1048708

Human Centric To achieve true machine independence the machines need to be taught to ultimately understand the abstract concepts used in humans1048708

Creatively the machines can learn to automate the algorithm discovery process

14

Figure 62 The evolution of programming paradigms

15

Software Development Paradigms

Functional construct ldquoblack boxesrdquofrom elementary functions1048708

Goal decompose the entire transformation with initial input and final output into nested complexes of simpler functions [modular approach support OO]

LISP Average equiv(Divide (Sum Numbers) (Count Numbers)) Min equiv(First (Sort List))1048708

Object-oriented OOP uses active objects equivdata + methods1048708

Merits modular design message passing (good for CORBA) clientserver (distributed) over the networks1048708

Imperative(procedural) the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions)1048708

Goal find an algorithm for the problem1048708

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

2

Chapter 6 Programming Languages 61 Historical Perspective 62 Traditional Programming Concepts 63 Procedural Units 64 Language Implementation 65 Object Oriented Programming 66 Programming Concurrent Activities 67 Declarative Programming

3

Programming Languages

Complex software system like OS would be developed impossibly using machine instructions directly

Programming languages are designed for humans to express the algorithm easily yet are convertible to the machine instructions on the target platform

As such the details of registers memory addresses and even machine cycles are out of our focus

We may then concentrate on the properties of the problems in question

4

Historical Perspective Evolution of computer languages1048708 Micro-instructions Inside CPU they implement each machine

instruction1048708 Machine language binary form direct controls the hardware1048708 Eg ldquo4032rdquo R3lArrR2 very hard to read amp write the programs1048708 Assembly language mnemonic form of the machine language1048708 Eg ldquoMV R3 R2rdquois used to refer to ldquo4032rdquo1048708 Descriptive name like ldquoPricerdquo is used to represent some memory

address1048708 ldquoLD R5 Pricerdquo(156C) load the content in the address ldquoPricerdquo0X6C to R51048708 Mnemonic ADDI amp ADDF refer to integer amp floating-point

additions1048708

5

Historical Perspective

This is a major syntax of revolution in programming1048708

Assemblers are the programs which translate the program written in an assembly language to the target machine language1048708

Shortages still machine-dependent and too naiumlve to develop large-scaled programs

6

Historical Perspective High-level language English like language for more

productive development1048708FORTRAN (Formula Translator) for scientific and

engineering applications1048708COBOL (Common Business-Oriented Language) from the

US Navy1048708The approach is to identify a collection of high-level

programming primitives to construct the software blocks [in essentially the same spirit with pseudo codes]1048708

Translators or compilers(initiated by Grace Hopper) are used to compile several machine instructions in the target machines to simulate the activity represented by a single high-level primitive [interpreters]

7

Historical PerspectiveInterpretersmdashsimilar to translators except they

executed the instructions as they were translatedie rather than producing a machine language copy of a

program that would be executed later an interpreter actually executed a program from its high level form

Programming languages are constructed around a small set of primitives that could be expressed in a variety of natural languages with only simple modification to the translators

Natural language(English German) evloved over time without formal grammatical analysis

Formal language(programming language)mdashare precisely defined by grammars

8

Figure 61 Generations of programming languages

9

Second-generationAssembly language A mnemonic system for

representing programs Mnemonic names for op-codes Names for all registers Identifiers = descriptive names for

memory locations chosen by the programmer

10

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

11

Assembly language example

Machine language

156C166D505630CEC000

Assembly language

LD R5 PriceLD R6 ShippingChargeADDI R0 R5 R6ST R0 TotalCostHLT

12

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 short

sequence of machine language instructions

Converted to machine language by a program called a compiler

13

Machine Independence and Beyond Through compiling programs can be portedeasily to any

other machine1048708 The efficiency of compilers also determines the quality of

executable programs1048708 A programming language may have several dialects over

different machines1048708 Standardization ANSI and ISO have adopted and

published several standards for several popular programming languages [to improve compatibility]1048708

Human Centric To achieve true machine independence the machines need to be taught to ultimately understand the abstract concepts used in humans1048708

Creatively the machines can learn to automate the algorithm discovery process

14

Figure 62 The evolution of programming paradigms

15

Software Development Paradigms

Functional construct ldquoblack boxesrdquofrom elementary functions1048708

Goal decompose the entire transformation with initial input and final output into nested complexes of simpler functions [modular approach support OO]

LISP Average equiv(Divide (Sum Numbers) (Count Numbers)) Min equiv(First (Sort List))1048708

Object-oriented OOP uses active objects equivdata + methods1048708

Merits modular design message passing (good for CORBA) clientserver (distributed) over the networks1048708

Imperative(procedural) the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions)1048708

Goal find an algorithm for the problem1048708

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

3

Programming Languages

Complex software system like OS would be developed impossibly using machine instructions directly

Programming languages are designed for humans to express the algorithm easily yet are convertible to the machine instructions on the target platform

As such the details of registers memory addresses and even machine cycles are out of our focus

We may then concentrate on the properties of the problems in question

4

Historical Perspective Evolution of computer languages1048708 Micro-instructions Inside CPU they implement each machine

instruction1048708 Machine language binary form direct controls the hardware1048708 Eg ldquo4032rdquo R3lArrR2 very hard to read amp write the programs1048708 Assembly language mnemonic form of the machine language1048708 Eg ldquoMV R3 R2rdquois used to refer to ldquo4032rdquo1048708 Descriptive name like ldquoPricerdquo is used to represent some memory

address1048708 ldquoLD R5 Pricerdquo(156C) load the content in the address ldquoPricerdquo0X6C to R51048708 Mnemonic ADDI amp ADDF refer to integer amp floating-point

additions1048708

5

Historical Perspective

This is a major syntax of revolution in programming1048708

Assemblers are the programs which translate the program written in an assembly language to the target machine language1048708

Shortages still machine-dependent and too naiumlve to develop large-scaled programs

6

Historical Perspective High-level language English like language for more

productive development1048708FORTRAN (Formula Translator) for scientific and

engineering applications1048708COBOL (Common Business-Oriented Language) from the

US Navy1048708The approach is to identify a collection of high-level

programming primitives to construct the software blocks [in essentially the same spirit with pseudo codes]1048708

Translators or compilers(initiated by Grace Hopper) are used to compile several machine instructions in the target machines to simulate the activity represented by a single high-level primitive [interpreters]

7

Historical PerspectiveInterpretersmdashsimilar to translators except they

executed the instructions as they were translatedie rather than producing a machine language copy of a

program that would be executed later an interpreter actually executed a program from its high level form

Programming languages are constructed around a small set of primitives that could be expressed in a variety of natural languages with only simple modification to the translators

Natural language(English German) evloved over time without formal grammatical analysis

Formal language(programming language)mdashare precisely defined by grammars

8

Figure 61 Generations of programming languages

9

Second-generationAssembly language A mnemonic system for

representing programs Mnemonic names for op-codes Names for all registers Identifiers = descriptive names for

memory locations chosen by the programmer

10

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

11

Assembly language example

Machine language

156C166D505630CEC000

Assembly language

LD R5 PriceLD R6 ShippingChargeADDI R0 R5 R6ST R0 TotalCostHLT

12

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 short

sequence of machine language instructions

Converted to machine language by a program called a compiler

13

Machine Independence and Beyond Through compiling programs can be portedeasily to any

other machine1048708 The efficiency of compilers also determines the quality of

executable programs1048708 A programming language may have several dialects over

different machines1048708 Standardization ANSI and ISO have adopted and

published several standards for several popular programming languages [to improve compatibility]1048708

Human Centric To achieve true machine independence the machines need to be taught to ultimately understand the abstract concepts used in humans1048708

Creatively the machines can learn to automate the algorithm discovery process

14

Figure 62 The evolution of programming paradigms

15

Software Development Paradigms

Functional construct ldquoblack boxesrdquofrom elementary functions1048708

Goal decompose the entire transformation with initial input and final output into nested complexes of simpler functions [modular approach support OO]

LISP Average equiv(Divide (Sum Numbers) (Count Numbers)) Min equiv(First (Sort List))1048708

Object-oriented OOP uses active objects equivdata + methods1048708

Merits modular design message passing (good for CORBA) clientserver (distributed) over the networks1048708

Imperative(procedural) the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions)1048708

Goal find an algorithm for the problem1048708

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

4

Historical Perspective Evolution of computer languages1048708 Micro-instructions Inside CPU they implement each machine

instruction1048708 Machine language binary form direct controls the hardware1048708 Eg ldquo4032rdquo R3lArrR2 very hard to read amp write the programs1048708 Assembly language mnemonic form of the machine language1048708 Eg ldquoMV R3 R2rdquois used to refer to ldquo4032rdquo1048708 Descriptive name like ldquoPricerdquo is used to represent some memory

address1048708 ldquoLD R5 Pricerdquo(156C) load the content in the address ldquoPricerdquo0X6C to R51048708 Mnemonic ADDI amp ADDF refer to integer amp floating-point

additions1048708

5

Historical Perspective

This is a major syntax of revolution in programming1048708

Assemblers are the programs which translate the program written in an assembly language to the target machine language1048708

Shortages still machine-dependent and too naiumlve to develop large-scaled programs

6

Historical Perspective High-level language English like language for more

productive development1048708FORTRAN (Formula Translator) for scientific and

engineering applications1048708COBOL (Common Business-Oriented Language) from the

US Navy1048708The approach is to identify a collection of high-level

programming primitives to construct the software blocks [in essentially the same spirit with pseudo codes]1048708

Translators or compilers(initiated by Grace Hopper) are used to compile several machine instructions in the target machines to simulate the activity represented by a single high-level primitive [interpreters]

7

Historical PerspectiveInterpretersmdashsimilar to translators except they

executed the instructions as they were translatedie rather than producing a machine language copy of a

program that would be executed later an interpreter actually executed a program from its high level form

Programming languages are constructed around a small set of primitives that could be expressed in a variety of natural languages with only simple modification to the translators

Natural language(English German) evloved over time without formal grammatical analysis

Formal language(programming language)mdashare precisely defined by grammars

8

Figure 61 Generations of programming languages

9

Second-generationAssembly language A mnemonic system for

representing programs Mnemonic names for op-codes Names for all registers Identifiers = descriptive names for

memory locations chosen by the programmer

10

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

11

Assembly language example

Machine language

156C166D505630CEC000

Assembly language

LD R5 PriceLD R6 ShippingChargeADDI R0 R5 R6ST R0 TotalCostHLT

12

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 short

sequence of machine language instructions

Converted to machine language by a program called a compiler

13

Machine Independence and Beyond Through compiling programs can be portedeasily to any

other machine1048708 The efficiency of compilers also determines the quality of

executable programs1048708 A programming language may have several dialects over

different machines1048708 Standardization ANSI and ISO have adopted and

published several standards for several popular programming languages [to improve compatibility]1048708

Human Centric To achieve true machine independence the machines need to be taught to ultimately understand the abstract concepts used in humans1048708

Creatively the machines can learn to automate the algorithm discovery process

14

Figure 62 The evolution of programming paradigms

15

Software Development Paradigms

Functional construct ldquoblack boxesrdquofrom elementary functions1048708

Goal decompose the entire transformation with initial input and final output into nested complexes of simpler functions [modular approach support OO]

LISP Average equiv(Divide (Sum Numbers) (Count Numbers)) Min equiv(First (Sort List))1048708

Object-oriented OOP uses active objects equivdata + methods1048708

Merits modular design message passing (good for CORBA) clientserver (distributed) over the networks1048708

Imperative(procedural) the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions)1048708

Goal find an algorithm for the problem1048708

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

5

Historical Perspective

This is a major syntax of revolution in programming1048708

Assemblers are the programs which translate the program written in an assembly language to the target machine language1048708

Shortages still machine-dependent and too naiumlve to develop large-scaled programs

6

Historical Perspective High-level language English like language for more

productive development1048708FORTRAN (Formula Translator) for scientific and

engineering applications1048708COBOL (Common Business-Oriented Language) from the

US Navy1048708The approach is to identify a collection of high-level

programming primitives to construct the software blocks [in essentially the same spirit with pseudo codes]1048708

Translators or compilers(initiated by Grace Hopper) are used to compile several machine instructions in the target machines to simulate the activity represented by a single high-level primitive [interpreters]

7

Historical PerspectiveInterpretersmdashsimilar to translators except they

executed the instructions as they were translatedie rather than producing a machine language copy of a

program that would be executed later an interpreter actually executed a program from its high level form

Programming languages are constructed around a small set of primitives that could be expressed in a variety of natural languages with only simple modification to the translators

Natural language(English German) evloved over time without formal grammatical analysis

Formal language(programming language)mdashare precisely defined by grammars

8

Figure 61 Generations of programming languages

9

Second-generationAssembly language A mnemonic system for

representing programs Mnemonic names for op-codes Names for all registers Identifiers = descriptive names for

memory locations chosen by the programmer

10

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

11

Assembly language example

Machine language

156C166D505630CEC000

Assembly language

LD R5 PriceLD R6 ShippingChargeADDI R0 R5 R6ST R0 TotalCostHLT

12

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 short

sequence of machine language instructions

Converted to machine language by a program called a compiler

13

Machine Independence and Beyond Through compiling programs can be portedeasily to any

other machine1048708 The efficiency of compilers also determines the quality of

executable programs1048708 A programming language may have several dialects over

different machines1048708 Standardization ANSI and ISO have adopted and

published several standards for several popular programming languages [to improve compatibility]1048708

Human Centric To achieve true machine independence the machines need to be taught to ultimately understand the abstract concepts used in humans1048708

Creatively the machines can learn to automate the algorithm discovery process

14

Figure 62 The evolution of programming paradigms

15

Software Development Paradigms

Functional construct ldquoblack boxesrdquofrom elementary functions1048708

Goal decompose the entire transformation with initial input and final output into nested complexes of simpler functions [modular approach support OO]

LISP Average equiv(Divide (Sum Numbers) (Count Numbers)) Min equiv(First (Sort List))1048708

Object-oriented OOP uses active objects equivdata + methods1048708

Merits modular design message passing (good for CORBA) clientserver (distributed) over the networks1048708

Imperative(procedural) the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions)1048708

Goal find an algorithm for the problem1048708

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

6

Historical Perspective High-level language English like language for more

productive development1048708FORTRAN (Formula Translator) for scientific and

engineering applications1048708COBOL (Common Business-Oriented Language) from the

US Navy1048708The approach is to identify a collection of high-level

programming primitives to construct the software blocks [in essentially the same spirit with pseudo codes]1048708

Translators or compilers(initiated by Grace Hopper) are used to compile several machine instructions in the target machines to simulate the activity represented by a single high-level primitive [interpreters]

7

Historical PerspectiveInterpretersmdashsimilar to translators except they

executed the instructions as they were translatedie rather than producing a machine language copy of a

program that would be executed later an interpreter actually executed a program from its high level form

Programming languages are constructed around a small set of primitives that could be expressed in a variety of natural languages with only simple modification to the translators

Natural language(English German) evloved over time without formal grammatical analysis

Formal language(programming language)mdashare precisely defined by grammars

8

Figure 61 Generations of programming languages

9

Second-generationAssembly language A mnemonic system for

representing programs Mnemonic names for op-codes Names for all registers Identifiers = descriptive names for

memory locations chosen by the programmer

10

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

11

Assembly language example

Machine language

156C166D505630CEC000

Assembly language

LD R5 PriceLD R6 ShippingChargeADDI R0 R5 R6ST R0 TotalCostHLT

12

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 short

sequence of machine language instructions

Converted to machine language by a program called a compiler

13

Machine Independence and Beyond Through compiling programs can be portedeasily to any

other machine1048708 The efficiency of compilers also determines the quality of

executable programs1048708 A programming language may have several dialects over

different machines1048708 Standardization ANSI and ISO have adopted and

published several standards for several popular programming languages [to improve compatibility]1048708

Human Centric To achieve true machine independence the machines need to be taught to ultimately understand the abstract concepts used in humans1048708

Creatively the machines can learn to automate the algorithm discovery process

14

Figure 62 The evolution of programming paradigms

15

Software Development Paradigms

Functional construct ldquoblack boxesrdquofrom elementary functions1048708

Goal decompose the entire transformation with initial input and final output into nested complexes of simpler functions [modular approach support OO]

LISP Average equiv(Divide (Sum Numbers) (Count Numbers)) Min equiv(First (Sort List))1048708

Object-oriented OOP uses active objects equivdata + methods1048708

Merits modular design message passing (good for CORBA) clientserver (distributed) over the networks1048708

Imperative(procedural) the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions)1048708

Goal find an algorithm for the problem1048708

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

7

Historical PerspectiveInterpretersmdashsimilar to translators except they

executed the instructions as they were translatedie rather than producing a machine language copy of a

program that would be executed later an interpreter actually executed a program from its high level form

Programming languages are constructed around a small set of primitives that could be expressed in a variety of natural languages with only simple modification to the translators

Natural language(English German) evloved over time without formal grammatical analysis

Formal language(programming language)mdashare precisely defined by grammars

8

Figure 61 Generations of programming languages

9

Second-generationAssembly language A mnemonic system for

representing programs Mnemonic names for op-codes Names for all registers Identifiers = descriptive names for

memory locations chosen by the programmer

10

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

11

Assembly language example

Machine language

156C166D505630CEC000

Assembly language

LD R5 PriceLD R6 ShippingChargeADDI R0 R5 R6ST R0 TotalCostHLT

12

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 short

sequence of machine language instructions

Converted to machine language by a program called a compiler

13

Machine Independence and Beyond Through compiling programs can be portedeasily to any

other machine1048708 The efficiency of compilers also determines the quality of

executable programs1048708 A programming language may have several dialects over

different machines1048708 Standardization ANSI and ISO have adopted and

published several standards for several popular programming languages [to improve compatibility]1048708

Human Centric To achieve true machine independence the machines need to be taught to ultimately understand the abstract concepts used in humans1048708

Creatively the machines can learn to automate the algorithm discovery process

14

Figure 62 The evolution of programming paradigms

15

Software Development Paradigms

Functional construct ldquoblack boxesrdquofrom elementary functions1048708

Goal decompose the entire transformation with initial input and final output into nested complexes of simpler functions [modular approach support OO]

LISP Average equiv(Divide (Sum Numbers) (Count Numbers)) Min equiv(First (Sort List))1048708

Object-oriented OOP uses active objects equivdata + methods1048708

Merits modular design message passing (good for CORBA) clientserver (distributed) over the networks1048708

Imperative(procedural) the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions)1048708

Goal find an algorithm for the problem1048708

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

8

Figure 61 Generations of programming languages

9

Second-generationAssembly language A mnemonic system for

representing programs Mnemonic names for op-codes Names for all registers Identifiers = descriptive names for

memory locations chosen by the programmer

10

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

11

Assembly language example

Machine language

156C166D505630CEC000

Assembly language

LD R5 PriceLD R6 ShippingChargeADDI R0 R5 R6ST R0 TotalCostHLT

12

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 short

sequence of machine language instructions

Converted to machine language by a program called a compiler

13

Machine Independence and Beyond Through compiling programs can be portedeasily to any

other machine1048708 The efficiency of compilers also determines the quality of

executable programs1048708 A programming language may have several dialects over

different machines1048708 Standardization ANSI and ISO have adopted and

published several standards for several popular programming languages [to improve compatibility]1048708

Human Centric To achieve true machine independence the machines need to be taught to ultimately understand the abstract concepts used in humans1048708

Creatively the machines can learn to automate the algorithm discovery process

14

Figure 62 The evolution of programming paradigms

15

Software Development Paradigms

Functional construct ldquoblack boxesrdquofrom elementary functions1048708

Goal decompose the entire transformation with initial input and final output into nested complexes of simpler functions [modular approach support OO]

LISP Average equiv(Divide (Sum Numbers) (Count Numbers)) Min equiv(First (Sort List))1048708

Object-oriented OOP uses active objects equivdata + methods1048708

Merits modular design message passing (good for CORBA) clientserver (distributed) over the networks1048708

Imperative(procedural) the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions)1048708

Goal find an algorithm for the problem1048708

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

9

Second-generationAssembly language A mnemonic system for

representing programs Mnemonic names for op-codes Names for all registers Identifiers = descriptive names for

memory locations chosen by the programmer

10

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

11

Assembly language example

Machine language

156C166D505630CEC000

Assembly language

LD R5 PriceLD R6 ShippingChargeADDI R0 R5 R6ST R0 TotalCostHLT

12

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 short

sequence of machine language instructions

Converted to machine language by a program called a compiler

13

Machine Independence and Beyond Through compiling programs can be portedeasily to any

other machine1048708 The efficiency of compilers also determines the quality of

executable programs1048708 A programming language may have several dialects over

different machines1048708 Standardization ANSI and ISO have adopted and

published several standards for several popular programming languages [to improve compatibility]1048708

Human Centric To achieve true machine independence the machines need to be taught to ultimately understand the abstract concepts used in humans1048708

Creatively the machines can learn to automate the algorithm discovery process

14

Figure 62 The evolution of programming paradigms

15

Software Development Paradigms

Functional construct ldquoblack boxesrdquofrom elementary functions1048708

Goal decompose the entire transformation with initial input and final output into nested complexes of simpler functions [modular approach support OO]

LISP Average equiv(Divide (Sum Numbers) (Count Numbers)) Min equiv(First (Sort List))1048708

Object-oriented OOP uses active objects equivdata + methods1048708

Merits modular design message passing (good for CORBA) clientserver (distributed) over the networks1048708

Imperative(procedural) the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions)1048708

Goal find an algorithm for the problem1048708

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

10

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

11

Assembly language example

Machine language

156C166D505630CEC000

Assembly language

LD R5 PriceLD R6 ShippingChargeADDI R0 R5 R6ST R0 TotalCostHLT

12

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 short

sequence of machine language instructions

Converted to machine language by a program called a compiler

13

Machine Independence and Beyond Through compiling programs can be portedeasily to any

other machine1048708 The efficiency of compilers also determines the quality of

executable programs1048708 A programming language may have several dialects over

different machines1048708 Standardization ANSI and ISO have adopted and

published several standards for several popular programming languages [to improve compatibility]1048708

Human Centric To achieve true machine independence the machines need to be taught to ultimately understand the abstract concepts used in humans1048708

Creatively the machines can learn to automate the algorithm discovery process

14

Figure 62 The evolution of programming paradigms

15

Software Development Paradigms

Functional construct ldquoblack boxesrdquofrom elementary functions1048708

Goal decompose the entire transformation with initial input and final output into nested complexes of simpler functions [modular approach support OO]

LISP Average equiv(Divide (Sum Numbers) (Count Numbers)) Min equiv(First (Sort List))1048708

Object-oriented OOP uses active objects equivdata + methods1048708

Merits modular design message passing (good for CORBA) clientserver (distributed) over the networks1048708

Imperative(procedural) the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions)1048708

Goal find an algorithm for the problem1048708

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

11

Assembly language example

Machine language

156C166D505630CEC000

Assembly language

LD R5 PriceLD R6 ShippingChargeADDI R0 R5 R6ST R0 TotalCostHLT

12

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 short

sequence of machine language instructions

Converted to machine language by a program called a compiler

13

Machine Independence and Beyond Through compiling programs can be portedeasily to any

other machine1048708 The efficiency of compilers also determines the quality of

executable programs1048708 A programming language may have several dialects over

different machines1048708 Standardization ANSI and ISO have adopted and

published several standards for several popular programming languages [to improve compatibility]1048708

Human Centric To achieve true machine independence the machines need to be taught to ultimately understand the abstract concepts used in humans1048708

Creatively the machines can learn to automate the algorithm discovery process

14

Figure 62 The evolution of programming paradigms

15

Software Development Paradigms

Functional construct ldquoblack boxesrdquofrom elementary functions1048708

Goal decompose the entire transformation with initial input and final output into nested complexes of simpler functions [modular approach support OO]

LISP Average equiv(Divide (Sum Numbers) (Count Numbers)) Min equiv(First (Sort List))1048708

Object-oriented OOP uses active objects equivdata + methods1048708

Merits modular design message passing (good for CORBA) clientserver (distributed) over the networks1048708

Imperative(procedural) the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions)1048708

Goal find an algorithm for the problem1048708

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

12

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 short

sequence of machine language instructions

Converted to machine language by a program called a compiler

13

Machine Independence and Beyond Through compiling programs can be portedeasily to any

other machine1048708 The efficiency of compilers also determines the quality of

executable programs1048708 A programming language may have several dialects over

different machines1048708 Standardization ANSI and ISO have adopted and

published several standards for several popular programming languages [to improve compatibility]1048708

Human Centric To achieve true machine independence the machines need to be taught to ultimately understand the abstract concepts used in humans1048708

Creatively the machines can learn to automate the algorithm discovery process

14

Figure 62 The evolution of programming paradigms

15

Software Development Paradigms

Functional construct ldquoblack boxesrdquofrom elementary functions1048708

Goal decompose the entire transformation with initial input and final output into nested complexes of simpler functions [modular approach support OO]

LISP Average equiv(Divide (Sum Numbers) (Count Numbers)) Min equiv(First (Sort List))1048708

Object-oriented OOP uses active objects equivdata + methods1048708

Merits modular design message passing (good for CORBA) clientserver (distributed) over the networks1048708

Imperative(procedural) the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions)1048708

Goal find an algorithm for the problem1048708

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

13

Machine Independence and Beyond Through compiling programs can be portedeasily to any

other machine1048708 The efficiency of compilers also determines the quality of

executable programs1048708 A programming language may have several dialects over

different machines1048708 Standardization ANSI and ISO have adopted and

published several standards for several popular programming languages [to improve compatibility]1048708

Human Centric To achieve true machine independence the machines need to be taught to ultimately understand the abstract concepts used in humans1048708

Creatively the machines can learn to automate the algorithm discovery process

14

Figure 62 The evolution of programming paradigms

15

Software Development Paradigms

Functional construct ldquoblack boxesrdquofrom elementary functions1048708

Goal decompose the entire transformation with initial input and final output into nested complexes of simpler functions [modular approach support OO]

LISP Average equiv(Divide (Sum Numbers) (Count Numbers)) Min equiv(First (Sort List))1048708

Object-oriented OOP uses active objects equivdata + methods1048708

Merits modular design message passing (good for CORBA) clientserver (distributed) over the networks1048708

Imperative(procedural) the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions)1048708

Goal find an algorithm for the problem1048708

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

14

Figure 62 The evolution of programming paradigms

15

Software Development Paradigms

Functional construct ldquoblack boxesrdquofrom elementary functions1048708

Goal decompose the entire transformation with initial input and final output into nested complexes of simpler functions [modular approach support OO]

LISP Average equiv(Divide (Sum Numbers) (Count Numbers)) Min equiv(First (Sort List))1048708

Object-oriented OOP uses active objects equivdata + methods1048708

Merits modular design message passing (good for CORBA) clientserver (distributed) over the networks1048708

Imperative(procedural) the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions)1048708

Goal find an algorithm for the problem1048708

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

15

Software Development Paradigms

Functional construct ldquoblack boxesrdquofrom elementary functions1048708

Goal decompose the entire transformation with initial input and final output into nested complexes of simpler functions [modular approach support OO]

LISP Average equiv(Divide (Sum Numbers) (Count Numbers)) Min equiv(First (Sort List))1048708

Object-oriented OOP uses active objects equivdata + methods1048708

Merits modular design message passing (good for CORBA) clientserver (distributed) over the networks1048708

Imperative(procedural) the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions)1048708

Goal find an algorithm for the problem1048708

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

16

Software Development Paradigms

1048708 Declarative emphasizes more on what the

problem is than how to solve it1048708 Goal discoverimplement a general problem-

solving algorithm and developwrite a precise statement of the problem [SQL formal logic logic programming]

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

17

Figure 63 A function for checkbook balancing constructed from simpler functions

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

18

Software Development Paradigms

Which of the following is an example of a language that is based on the object-oriented paradigm

A LISP B PROLOG C C D C++ Most machine languages are based on the A Imperative paradigm B Declarative paradigm C Functional paradigm D Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level

imperative programming language A Imperative statement B Exclamatory statement C Declarative statement D Comment statement

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

19

Traditional Programming Concepts

Generally programming languages contains1048708 Declarative statements define customized

names to be used later in the program1048708Imperative statements describe the steps in

the underlying algorithms1048708Comments enhance the readability of a

program for handy explanation

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

20

Traditional Programming Concepts

Declarative statements 1048708Variables are identifiers or customized names defined to

refer some memory locations1048708Literals are fixed predetermined values1048708Constants are customized names initiated with fixed

values (meaningful literals)1048708Data type variables or constants are defined with both

names and data types 1048708Primitive data types integer real character Boolean

(predefined in languages)1048708Derived data types are customized data types

combinations of primitive ones1048708

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

21

Traditional Programming Concepts

Declarative statements 1048708Homogeneousndash a block of values of the same

typesheterogeneous ndasha block of data in which different

elements can have different data typearray (structure) queues objects or the hybrid

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

22

Traditional Programming Concepts

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

23

Figure 64 The composition of a typical imperative program or program unit

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

24

Constants and literals

EffectiveAlt lt- alitmeter + 645

In C const int Airaltitude = 645 In Java final int Airaltitude = 645EffectiveAlt lt- alitmeter + Airaltitude

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

25

Figure 65 Variable declarations in C C++ C and Java

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

26

Figure 66 A two-dimensional array with two rows and nine columns

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

27

Figure 67 Declaration of heterogeneous array

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

28

Figure 68 Control structures and their representations in C C++ C and Java

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

29

Figure 69 The for loop structure and its representation in C++ C and Java

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

30

Traditional Programming Concepts

Which of the following does not require a Boolean structure

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

Which of the following is not a control statement

A If-then-else statement B While loop statement

C Assignment statement D For loop statement

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

31

Procedural Unit

A program = code blocks in a concatenated or hierarchical fashion1048708

Procedures are some code blocks (or programming units)

Procedurersquos header = the procedure name + other details1048708

Declaration statements to declare local variables used within the procedureWith the same names local variables in different procedures are distinct1048708

Global variables in the parent block are unique in all children blocks1048708

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

32

Procedural Unit

Imperative statements to describe the steps performed in the procedure1048708

Invoking the procedure1048708FORTRAN ldquoCALL GetNamesrdquo in AdaCC++JavaPascall ldquoGetNamesrdquo

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

33

Figure 610 The flow of control involving a procedure

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

34

Figure 611 The procedure Project Population written in the programming language C

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

35

Procedural Unit

Parameters are additional information passed to the procedure upon invocation1048708

Callers use the actual parameters each of whose values mapped orderly to the corresponding one of formal parameters in the procedure1048708

ldquoPassed by valuerdquo-copy protect the data in callers but inefficient if data is large1048708The changes on formal parameters do not reflect on actual parameters1048708

ldquoPassed by referencerdquo the same data of callers are used directly in the procedure1048708It is favorable to sort the long list in place

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

36

Procedure Demo(Formal)Formallt-Formal +1print formalactual = 5apply Demo to actualprint actual

Figure 612 Executing the procedure Demo and passing parameters by value

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

37

Figure 613 Executing the procedure Demo and passing parameters by reference

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

38

Procedural Unit

Functionmdashbehaves like variables --similar to a procedure except

that a value is transferred back to the calling program init as ldquo the value of the functionrdquo

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

39

Figure 614 The function CylinderVolume written in the programming language C

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

40

Procedural Unit

InputOutput Statements call the default procedures for the program IO

In Pascal ldquo readln(Value) rdquoretrieves a value from keyboard to the variable Valueldquo writeln(Value) rdquooutputs the value of the variable Value to the monitor

In C ldquo scanf rdquoamp ldquo printf(ldquoThe price is dnrdquo Price)rdquofunctions are used instead

In C++ ready-made ldquo cin gtgt value rdquoamp ldquo coutltlt Price rdquoobjects represent the IO devices

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

41

Language Implementation

The code blocks are ultimately translated into the sequence of machine instructions

Translation Process1048708 The source program is the program in

the original programming language1048708 The object program is the translated

version resulted from the translation process

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

42

Figure 615 The translation process

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

43

Language Implementation

Lexical analysis by lexical analyzer Recognize which strings of symbols

representing a single entity EX const alt = 153 Classify entity by numeric values words

arithmetic operators etc Generate a bit pattern known as tokens1048708 The entire program byte streams are

recognized as a series of tokens of various types( if a lt b then a++ else b++)

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

44

Language Implementation

Parsing by parser1048708 Tokens regarding to comments are skippedParser

groups tokens together into statements according to the grammatical rules(syntax)1048708

Early programming languages (fixed-format languages) insist the particular positions for statements most languages today are free-format using delimiters like semicolons key words reserved words etc to delineate statements1048708

Grammatical rules can be illustrated by syntax diagrams1048708

Code generation by code generator

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

45

Figure 616 A syntax diagram of our if-then-else pseudocode statement

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

46

Figure 619 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

47

Ambiguities Avoidance

Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens1048708

The single statement ldquoif B1 then ifB2 thenS1 elseS2rdquoreveals such flaw

Avoidance (uniqueness)1048708Using parentheses to specify the precise join

[programmers]1048708Adjusting the syntax rules for default join

(nearest) [languagedesigners]

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

48

Figure 617 Syntax diagrams describing the structure of a simple algebraic expression

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

49

Figure 618 The parse tree for the string x + y x z based on the syntax diagrams in Figure 617

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

50

Code Generation

To analyze imperative statements the parser first processes the declarative statements and records each user-defined identifier in the symbol table

For variables their data types and data structures

are bookedFor procedures their return types and lists of

formal parameters are stored 1048708

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

51

Code Generation

As an example ldquoTotal larrPrice + Taxrdquois translated into integer or floating-point additions according to the types of variables Price and Tax1048708

An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible)

Such implicit conversion is called coercion1048708Strongly typed languages disallow coercion and

treat them as errors

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

52

Figure 620 An object-oriented approach to the translation process

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

53

Linking and Loading

One or more object programs need to be connected together as an executable1048708It is done by the linker to produce the executable file (load module)1048708

Object programs can be developed by another teams system library or any third party

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

54

Figure 621 The complete program preparation process

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

55

Objects and Classes Object = active program unit

containing both data and procedures

Class = a template for all objects of the same type

An Object is often called an instance of the class

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

56

Components of an object Instance variable = variable

within an object Method = function or procedure

within an object Can manipulate the objectrsquos instance

variables

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

57

Figure 622 The structure of a class describing a laser weapon in a computer game

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

58

Class amp Object Create an object of ldquotyperdquo and assign

it to Laser 1 LaserClass Laser1 = new

LaserClass() --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1fire()

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

59

Constructor = special method to initialize a new object instance

LaserClass Laser1(50) Laser2(100)

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

60

Encapsulation Encapsulation = a way of

restricting access to the internal components of an object Privatemdashonly object itself can access

it Publicmdashaccessible from outside of

objects

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

61

Figure 624 Our LaserClass definition using encapsulation as it would appear in a Java or C program

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

62

Additional object-oriented concepts

Inheritance allows new classes to be defined in terms of previously defined classes

Class RechargeableLaser extends LaserClass void fire () RechargeableLaser Laser3 Laser4 Polymorphism allows method calls to

be interpreted by the object that receives the callLaser3fire()

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

63

Programming concurrent activities Parallel or concurrent processing =

simultaneous execution of multiple processes True concurrent processing requires multiple

CPUs Can be simulated using time-sharing with a

single CPU To facilitate such demands modern

programming languages provide syntax for expressing the semantic structures1048708

In Ada an activation is called a task while in Java it is a thread

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

64

Figure 625 Spawning processes

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

65

Interaction between processes Mutual exclusion = a method for

ensuring that data can be accessed by only one process at a time

ApproachFrom process side Monitor = a data item augmented

with the ability to control access to itself

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

66

Declarative programming provides a general problem-solving

algorithm around which declarative programming can be constructed over the logic deduction

Resolution = combining two or more statements to produce a new logically equivalent statement

Example (P OR Q) AND (R OR Q) resolves to (P OR R)

Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components

are connected by the Boolean operation OR EX P OR Q P-gtQ Q OR ~P

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

67

Figure 626 Resolving the statements (P OR Q) and (R OR notQ) to produce (P OR R)

If the original statement is true then the resolvent must also Be true If Q is true then R must be true but if Q is false thenP must be true Regardless Q is true or false either P or R must Be true

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

68

Figure 627 Resolving the statements (P OR Q) (R OR notQ) notR and notP

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules

69

Prolog(Programming in Logic) Fact = predicateName(arguments)

Example parent(bill mary) Rule = conclusion - premise

- means ldquoifrdquo Example wise(X) - old(X)old(x)-gt wise(x) Example faster(XZ) - faster(XY) faster(YZ)

((faster(xy) and faster(Yz))-gt faster(xz)

All statements must be fact or rules