* in order to express the syntax of Pascal,
we will use a notation (actually, another
language)
especially designed for this purpose
* the idea of using one language to describe
the grammar of another language should
not puzzle you:
-- it's the same idea
used in foreign-language
textbooks, where we use English, for
example, to describe French grammar
b) The notation we will use is called
"production rules",
or "re-write rules"
* General format of a re-write rule:
<left-hand side> --> <right-hand side>
* The angle brackets around a name mean:
"an object called..."
e.g., <instruction> means:
"an object called
an instruction"
* The arrow means: "can be", or "can consist
of"
or "can be rewritten as"
* The right-hand side partially defines the
left-hand
side
e.g.) <identifier> --> a sequence of letters
&/or
digits that begins with a letter
e.g.) <string expression> --> <identifier>
* The right-hand side can be a sequence of
angle-bracketed items. This usually
means
that the items will be written right
next to
each other.
2. As an example, here's a set of re-write rules
for a small fragment
of English:
a- <sentence> --> <noun phrase> <verb phrase>
Note
that this means that a <sentence> consists of
a <noun phrase> followed by a <verb phrase>
b- <noun phrase> --> <noun>
c- <noun phrase> --> <article> <noun>
d- <verb phrase> --> <verb>
e- <verb phrase> --> <verb> <noun phrase>
f- <noun> -->
boy
g- <noun> -->
girl
h- <article> --> the
i- <article> -->
a
j- <verb> -->
kissed
Which of the following sentences can be "generated"
(or "derived") from these re-write rules?
1- The boy kissed the girl
2- The girl kissed the boy
3- The boy kissed girl
4- The girl kisses a boy
5- A boy gives a book to a girl
Answers:
1- yes
2- yes
3- yes! (Even though this is not grammatical
in real English, it is grammatical
according to our "toy" grammar)
4- no! (Our grammar does not recognize
the word "kisses", only the word "kissed")
5- no! (Our grammar does not recognize
the words "gives", "book", "to", or
the syntax
<noun phrase><verb><noun
phrase> <prepositional phrase>
* Here's a "derivation" of sentence 3 from our
toy grammar of a fragment of English:
<sentence> gets re-written by rule a as:
<noun phrase> <verb phrase>,
which gets rewritten by rule c as:
<article> <noun> <verb phrase>
which gets rewritten by rule e as:
<article> <noun> <verb> <noun phrase>
which gets rewritten by rule b as:
<article> <noun> <verb> <noun>
which gets rewritten by rules h,f,j,g as:
The boy kissed girl
I. <p> -->
x <c> y z <d>
II. <c> -->
a
III. <c> -->
b
IV. <d> -->
<c> <c>
V. <d> -->
z
b) Starting from <p>, can we generate any of these?
i- xayzz
ii- xbyzab
iii- xayzba
iv- xayzbab
c) E.g., i can be generated as follows:
1. <p>
2. x <c> y z <d>
, from line 1, by rule I
3. x a
y z <d> , from line 2, by rule II
4. x a
y z z , from line 3, by rule
V
(If this reminds you of proofs in geometry, good!)
d) Computer scientists sometimes describe the
process demonstrated in
(c) this way:
We have "proved" xayzz
or We have "produced" xayzz
or We have "generated" xayzz
or We have "derived" xayzz
<identifier> --> a sequence
of letters &/or digits
that begins with a letter
<var declaration> -->
I.e., a <var declaration> could
be rewritten as nothing at all;
in other words,
a program doesn't have to have one.
<var declaration> -->
var <identifier>, ..., <identifier> : <type>
<type> --> char
<type> --> varying [<positive integer>] of <type>
<positive integer> --> any
natural number,
i.e., 1, 2, 3, ..., maxint
<compound statement> --> begin
<statement>;
.
.
.
<statement>
end
<statement> --> writeln(<string expression>)
<statement> --> readln(<identifier>)
<string expression> --> <identifier>
<string expression> -->
'any string of printable characters '
<statement> --> <compound statement>
<statement> --> if <test>
then <statement>
else <statement>
<test> --> any true/false
sentence
b) program <identifier>;
<var declaration>
<compound statement>.
which can be rewritten as:
c) program printdata;
<var declaration>
<compound statement>.
which can be rewritten as:
d) program printdata;
<compound statement>.
which (etc.)
e) program printdata;
begin
<statement>;
<statement>
end.
f) program printdata;
begin
writeln(<string expression>);
writeln(<string expression>)
end.
g) program printdata;
begin
writeln('This is a');
writeln('test program')
end.
which is a grammatically correct
Pascal program!