Dawno mówią: gdzie Bóg, tam zgoda. Orzechowski

[ Pobierz całość w formacie PDF ]

IfStatement = "IF" Condition "THEN" Statement .
WhileStatement = "WHILE" Condition "DO" Statement .
Condition = Expression RelOp Expression .
ReadStatement = "READ" "(" Variable { "," Variable } ")" .
WriteStatement = "WRITE"
[ "(" WriteElement { "," WriteElement } ")" ] .
WriteElement = string | Expression .
Expression = ( "+" Term | "-" Term | Term ) { AddOp Term } .
Term = Factor { MulOp Factor } .
Factor = Designator | number | "(" Expression ")" .
AddOp = "+" | "-" .
MulOp = "*" | "/" .
RelOp = "=" | "" | "=" .
END Clang.
8.7.3 A sample program
It is fairly common practice to illustrate a programming language description with an example of a
program illustrating many of the language s features. To keep up with tradition, we follow suit. The
rather obtuse way in which Eligible is incremented before being used in a subscripting expression
in line 16 is simply to illustrate that a subscript can be an expression.
PROGRAM Debug;
CONST
VotingAge = 18;
VAR
Eligible, Voters[100], Age, Total;
BEGIN
Total := 0;
Eligible := 0;
READ(Age);
WHILE Age > 0 DO
BEGIN
IF Age > VotingAge THEN
BEGIN
Voters[Eligible] := Age;
Eligible := Eligible + 1;
Total := Total + Voters[Eligible - 1]
END;
READ(Age);
END;
WRITE(Eligible,  voters. Average age =  , Total / Eligible);
END.
Exercises
8.21 Do the BNF style productions use right or left recursion? Write an equivalent grammar which
uses the opposite form of recursion.
8.22 Develop a set of syntax diagrams for Clang (see section 5.10).
8.23 We have made no attempt to describe the semantics of programs written in Clang; to a reader
familiar with similar languages they should be self-evident. Write simple programs in the language
to:
(a) Find the sum of the numbers between two input data, which can be supplied in either
order.
(b) Use Euclid s algorithm to find the HCF of two integers.
(c) Determine which of a set of year dates correspond to leap years.
(d) Read a sequence of numbers and print out the embedded monotonic increasing
sequence.
(e) Use a "sieve" algorithm to determine which of the numbers less than 255 are prime.
In the light of your experience in preparing these solutions, and from the intuition which you have
from your background in other languages, can you foresee any gross deficiencies in Clang as a
language for handling problems in integer arithmetic (apart from its lack of procedural facilities,
which we shall deal with in a later chapter)?
8.24 Suppose someone came to you with the following draft program, seeking answer to the
questions currently found in the comments next to some statements. How many of these questions
can you answer by referring only to the syntactic description given earlier? (The program is not
supposed to do anything useful!)
PROGRAM Query;
CONST
Header =  Title ; (* Can I declare a string constant? *)
VAR
L1[10], L2[10], (* Are these the same size? *)
L3[20], I, Query, (* Can I reuse the program name as a variable? *)
L3[15]; (* What happens if I use a variable name again? *)
CONST (* Can I declare constants after variables? *)
Max = 1000;
Min = -89; (* Can I define negative constants? *)
VAR (* Can I have another variable section? *)
BigList[Max]; (* Can I use named constants to set array sizes? *)
BEGIN
Write(Heading) (* Can I write constants? *)
L1[10] := 34; (* Does L[10] exist? *)
L1 := L2; (* Can I copy complete arrays? *)
Write(L3); (* Can I write complete arrays? *)
;; I := Query;;; (* What about spurious semicolons? *)
END.
8.25 As a more challenging exercise, consider a variation on Clang, one that resembles C++ rather
more closely than it does Pascal. Using the translation below of the sample program given earlier as
a guide, derive a grammar that you think describes this language (which we shall later call
"Topsy"). For simplicity, regard cin and cout as keywords leading to special statement forms.
void main (void) {
const VotingAge = 18;
int Eligible, Voters[100], Age, Total;
Total = 0;
Eligible = 0;
cin >> Age;
while (Age > 0) {
if (Age > VotingAge) {
Voters[Eligible] = Age;
Eligible = Eligible + 1;
Total = Total + Voters[Eligible - 1];
}
cin >> Age;
}
cout
}
8.26 In the light of your experience with Exercises 8.24 and 8.25, discuss the ease of
"reverse-engineering" a programming language description by consulting only a few example
programs? Why do you suppose so many students attempt to learn programming by imitation?
8.27 Modify the Clang language definition to incorporate Pascal-like forms of:
(a) the REPEAT ... UNTIL statement
(b) the IF ... THEN ... ELSE statement
(c) the CASE statement
(d) the FOR loop
(e) the MOD operator.
8.28 Repeat the last exercise for the language suggested by Exercise 8.25, using syntax that
resembles that found in C++.
8.29 In Modula-2, structured statements are each terminated with their own END. How would you
have to change the Clang language definition to use Modula-2 forms for the existing statements,
and for the extensions suggested in Exercise 8.27? What advantages, if any, do these forms have
over those found in Pascal or C++?
8.30 Study how the specification of string tokens has been achieved in Cocol. Some languages, like
Modula- 2, allow strings to be delimited by either single or double quotes, but not to contain the
delimiter as a member of the string (so that we might write "David s Helen s brother" or  He said
"Hello" , but not  He said "That s rubbish!" ). How would you specify string tokens if these had to
match those found in Modula-2, or those found in C++ (where various escape characters are
allowed within the string)? [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • jungheinrich.pev.pl
  • Wątki

    Cytat


    Ibi patria, ibi bene. - tam (jest) ojczyzna, gdzie (jest) dobrze
    Dla cierpiącego fizycznie potrzebny jest lekarz, dla cierpiącego psychicznie - przyjaciel. Menander
    Jak gore, to już nie trza dmuchać. Prymus
    De nihilo nihil fit - z niczego nic nie powstaje.
    Dies diem doces - dzień uczy dzień.