(1) Optimize graph:
<exp>::= <simple exp>
| <simple exp><relational opr><simp exp>
begin
T(1);T(
2);T(
3);...
end;
Perform sequence of statements that handle each element = T(i).
i.e. process1, then
2 etc.
The begin-end are needed because the sequence forms a block.
This can be translated into
case sym of
L1:T(1);
L2:T(2);
:
Ln:T(n)
end;
Zero or more occurences
while sym in L do T();
L = set of first();
input(ch)
n0;
while ch in ['0'..'9'] do
begin
nn*10 + ord(ch)- ord('0')
input(ch)
end
repeat
T(i);
until not sym in L;
= one or more occurrences
Example:
repeat
input(ch);
nn*10 + ord(ch) - ord('0');
until not ch in ['0'..'9'];
A non-terminal symbol == another graph can be translated into a call
to the procedure which parses that symbol.
sentence::=<subject><predicate>
procedure sentence;
begin
subject( );
predicate( );
end
Terminal symbols: where we apply base level rules.
For correct grammar, the next symbol must be 'x'.
Therefore
if sym = x then
getsym /* symbol ok - get next symbol */
else
/* error has occurred in input sequence
since it is not what it should be */
Pascal Example:
var i,for:integer;
^
identifier expected
^ ':' expected
^ ';' expected
begin
for i:=1 to 10 do
write( , )
end.
^ Statement must end with a ';','end'
'else' or 'until'.
UNEXPECTED END OF FILE.