• Non ci sono risultati.

# Linguaggi di programmazione

N/A
N/A
Protected

Condividi "Linguaggi di programmazione"

Copied!
25
0
0

Testo completo

(1)

## Linguaggi di Programmazione

(2)

(3)

(4)

(5)

### Java

Object-oriented

Imperative

Declarative Functional

(6)

### (Find diff (Find sum Old balance Credits) (Find sum Debits))

functions) so that each unit’s outputs are used as another unit’s inputs in such a way that the desired overall input-to-output relationship is obtained. In short, the programming process under the functional paradigm is that of building func- tions as nested complexes of simpler functions.

As an example, Figure 6.3 shows how a function for balancing your checkbook can be constructed from two simpler functions. One of these, called Find_sum, accepts values as its input and produces the sum of those values as its output. The other, called Find_diff, accepts two input values and computes their difference.

The structure displayed in Figure 6.3 can be represented in the LISP programming language (a prominent functional programming language) by the expression (Find_diff (Find_sum Old_balance Credits) (Find_sum Debits)) The nested structure of this expression (as indicated by parantheses) reflects the fact that the inputs to the function Find_diff are produced by two applications of Find_sum. The first application of Find_sum produces the result of adding all the Credits to the Old_balance. The second application of Find_sum computes the total of all Debits. Then, the function Find_diff uses these results to obtain the new checkbook balance.

To more fully understand the distinction between the functional and impera- tive paradigms, let us compare the functional program for balancing a checkbook to the following pseudocode program obtained by following the imperative paradigm:

Total_credits←sum of all Credits Temp_balance←Old_balance + Total_credits Total_debits←sum of all Debits Balance←Temp_balance - Total_debits 246 Chapter 6 Programming Languages

Old_balance Credits Debits Inputs:

Output: New_balance Find_diff

Find_sum Find_sum

Figure 6.3 A function for checkbook balancing constructed from simpler functions

(7)

(8)

(9)

(10)

(11)

(12)

(13)

(14)

(15)

(16)

(17)

### 264

Chapter 6 Programming Languages

a. When the procedure is called, a copy of the data is given to the procedure

Actual

Calling environment Procedure‘s environment

Formal

5 5

b. and the procedure manipulates its copy.

Actual

Calling environment Procedure‘s environment

Formal

5 6

Calling environment

c. Thus, when the procedure has terminated, the calling environment has not been changed.

Actual 5

(18)

### 265 6.3 Procedural Units Figure 6.11 Executing the procedure Demo and passing parameters by reference

a. When the procedure is called, the formal parameter becomes

### a reference to the actual parameter.

b. Thus, changes directed by the procedure are made to the

### actual parameter

c. and are, therefore, preserved after the procedure has

(19)

(20)

(21)

(22)

Expression

Term

Factor

x y z

(23)

(24)

### Sintassi: parsing (ambigui)

The process of parsing a program is essentially that of constructing a parse tree for the source program. Indeed, a parse tree represents the parser’s interpretation of the program’s grammatical composition. For this reason the syntax rules describing a program’s grammatical structure must not allow two distinct parse trees for one string, since this would lead to ambiguities within the parser. A grammar that does allow two distinct parse trees for one string is said to be an ambiguous grammar.

Ambiguities in grammars can be quite subtle. In fact, the rule in Figure 6.14 con- tains such a flaw. It allows both the parse trees in Figure 6.17 for the single statement

if B1 then if B2 then S1 else S2 272 Chapter 6 Programming Languages

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

if then

then else

if

Statement

B1

B2 S1

Boolean

expression Statement S2 Boolean

expression Statement Statement

then if

then else

if Statement

B1

B2 S1 S2

Boolean

expression Statement Statement

Boolean

expression Statement

(25)

### analyzer

Riferimenti

Documenti correlati

The proof of the following result shows that under suitable assumptions on the real sequence {t k }, which make the matrix A p.d., such A (besides being p.d. as A) has exatly

A critical analysis of the presently dominant rhetoric of innovation and unlimited growth is necessary to shed light on the relationship between science and

[ 18 F ]- 22 was ﬁrst synthesized in two radiochemical steps from the N-Boc- protected bromo-precursor and was obtained in low radiochemical yield (Scheme 16c) (Zhang and Horti

Following the filter we have the key-component of this work, that is the Low Noise Amplifier, then the first Mixer, that translates the carrier frequency to a lower and

Interestingly, when the SG CFA is employed at the sensing stage, our implementation of the framework, which does not adopt any patch division strategy, provides demosaiced image

In the case of COVID in Italy, our estimate – based on a ﬁt of the epidemiological data by the model, see Sec- tions VII and VIII below – is that only 10% of infec- tives

We allow for discontinuous coefficients and random initial condition and, under suitable assumptions, we prove that in the limit as the number of particles grows to infinity

The results have shown that, in the presence of the positively charged surfactant, while positively charged copper ions pass through ultrafiltration membrane in