Professional Documents
Culture Documents
A. The Assignment: National University of Lesotho
A. The Assignment: National University of Lesotho
A. The Assignment: National University of Lesotho
A. THE ASSIGNMENT
A.1. The Assignment Task:-
Read Pages 123 to 133 on the topic: TOP DOWN PARSING
1. Introduction:-
Definition 2.19 introduces the concept of LL(k) or LR(k) grammar.
The first “L” in LL(k) is the same as the “L” in LR(k). It refers to the phrase “left to right
scan”. That is, we are reading the grammar from left to right (which is our usual way of
reading in the Western Hemisphere). The second “L” in LL(k) refers to the phrase
“leftmost derivation”. By now, you must be familiar with this term from “your
constructing parse/semantic trees”. In LR(k), the “R” refers to the phrase “rightmost
derivation”.
In all the cases, the “k” in the brackets refers to the FSA reading mechanism which reads
up to “k” symbols/tokens before a “derivation decision” is made. The reader should note
that where there are at least two productions with the same LHS, it may be difficult to
make a derivation based on, say, 1-look ahead mechanism, but be able to do so on a 2-
look ahead mechanism. For example, given the grammar
S 6aSb
S 6ab
CS 3410 -Introduction to the Theory of Computation 16th May 2020 Reading Ass 02 Page -1/4-
and the wff:
aabb
When one is doing a derivation based on 1-look ahead, that is, “reading just one token”,
which will be the first “a” in “aabb”, it is not possible to deterministically choose which of
the two productions to apply since both of them have their RHS beginning with “a”.
However, if one were to apply 2-look ahead FSA, that is “read 2 tokens”, which will be
“aa” in this case, then one can deterministically choose to apply the production:
S 6aSb
in the first step of the derivation, since this production derives “aabb” in 2 steps! So, the
derivations will be:
In this way then, the given grammar is said to be “strong LL(2)” and “weak LL(1)”. It is
strong LL(2) because on a 2-look ahead mechanism, we are able to make a deterministic
choice between any two productions with the same LHS! It is weak LL(1) because, based
on a 1-look ahead mechanism we cannot make deterministic choice between any two
productions with the same LHS.
When we predict a rule, the RHS of the rule is pushed onto the stack in reverse order so
that when we pop off the stack the reading will be in “left to right scan” of the input
stream (since the stack maintains the LIFO discipline).
Although the given PDA algorithm is for a strong LL(1) grammar, we can still apply it on
any strong LL(k) grammar by just appling the corresponding k-look ahead mechanism.
For example, for a strong LL(2) grammar, we will read next 2 tokens (as opposed to
reading 1 token).
In the given examples, 2.32 to 2.34, both the stack and its environment, the input stream
and the actions being taking when applying the PDA algorithm are represented in each
CS 3410 -Introduction to the Theory of Computation 16th May 2020 Reading Ass 02 Page -2/4-
table row. The input is being read in a left to right scan. The actions indicated in the
current row have their effects reflected in the next row, on the stack column and input
stream column.
The “first” of a string is its “prefix” and the “follow” is its “suffix”. Now, applied to a
grammar, a non-terminal A can have its “first” and “follow” as outlined below.
The “follow” of a non-terminal A is the set containing all strings that immediately follow A
where A appears on the RHS of such productions. For example, x is an element of the
follow of A if:
S Y áAxâ
The “first” of a non-terminal A is the set containing all strings that appear as the prefix of
each RHS of the productions where A is the LHS (of the productions). For example, x is
an element of the first of A if:
A Y xâ
CS 3410 -Introduction to the Theory of Computation 16th May 2020 Reading Ass 02 Page -3/4-
non-terminal.
CS 3410 -Introduction to the Theory of Computation 16th May 2020 Reading Ass 02 Page -4/4-