| Top-down Parsing |
). At each such
step one must make two choices:
< S >
a < R > | b < S > b < R >
< R >
b < R > | a
| Matched terminals | Tail of sentential form | Pending input |
| < S > | bbaababa | |
| b | < S > b < R > | baababa |
| bb | < S > b < R > b < R > | aababa |
| bba | < R > b < R > b < R > | ababa |
| bbaab | < R > b < R > | aba |
| bbaabab | < R > | a |
| bbaababa | |
|
| < stmt > |
| if < expr > then < stmt > end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | | if < expr > then < stmt > else < stmt > end | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| < stmt > |
| if < expr > then < stmt > < iftail > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| < iftail > |
| else < stmt > end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
A grammar with these two properties is said to be an S-grammar. Any S-grammar is LL(1).
(where x is a string of terminals, A is a non-terminal and
is composed of terminals and non-terminals) while
trying to parse
, we would want
to read in at least the prefix x of
before proceeding
further.
we will either
need to read past all the terminals that will eventually be
matched by A (saving them so that we can check that they
match later) or have to remember to check the correctness
of the substitution made for A later.
< S >
a < R > | b < S > b < R >
< R >
b < R > | a
procedure R;
if ch = 'b' then
getnextchar;
R;
else if ch = 'a' then
getnextchar;
else
error
end
end R;
procedure S;
if ch = 'a' then
getnextchar;
R;
else if ch = 'b' then
getnextchar;
S;
if ch = 'b' then
getnextchar;
else
error;
end;
R;
end
end R
| Top-down Parsing |