Sie sind auf Seite 1von 4

NATIONAL UNIVERSITY OF LESOTHO

Department of Mathematics and Computer Science


CS 3410 -Introduction to the Theory of Computation
Reading Assignment 02
16th May 2020 Marks: N/A Due: 22nd May 2020

A. THE ASSIGNMENT
A.1. The Assignment Task:-
Read Pages 123 to 133 on the topic: TOP DOWN PARSING

The topic to read are the following:

1. Introduction (Definition 2.19),


2. Push Down Automata & Predictive Parsing
3. Theorems on Context Free Grammars
4. The First and The Follow Sets of a String

A.2. A Brief Discussion of the Topics:-

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:

S Y aSb (using S 6aSb), where the FSA returns “aa”


Y aabb (using S 6ab), where the FSA returns “ab”

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.

2. Push Down Automata (PDA) & Predictive Parsing:-


A PDA is a TM. The infinite tape is its stack – the infinite tape with one end closed (and
the other end opened). The closed end is called the bottom (of the stack) and the open end
is the top (of the stack). Due to this restrictive configuration, the writing and deleting on
the tape is eccentric. When we write we push elements on the stack and when we delete
we pop elements off the stack!

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.

3. Theorems on Context Free Grammars


There are two theorems here on LL(k) grammars, namely that

(a) A strong LL(k) is unambiguous (which is a good thing)


(b) An LL(k) has no left recursive non-terminals (since it applies leftmost derivation.
Any left recursion will lead to infinite looping, which will be a disaster!).

4. The First and The Follow Sets of a String


The introduction of the concepts here is formal and as such it is a bit terse. In the
forgoing, I am putting the concepts more or less informally for purposes of elicitation as
follows:

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â

where á, â 0 (N c T)* and x 0T+

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â

where â 0 (N c T)* and x 0T+

A.3. Learning Outcomes:-


1. To be able to determine whether an LL(k) grammar is “weak” or “strong” LL(k)
2. Given an LL(k) grammar, G, and a chosen wff, w, apply the PDA algorithm to determine
whether or not w is in L(G).
3. Given an LL(k) grammar, G, be able to compute the “first” and the “follow” sets of each

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-

Das könnte Ihnen auch gefallen