Sunteți pe pagina 1din 4

Q5

(a)
1. Introduce
Introduce a new start variable,

and a new rule

where

is the previous start

variable.
2. Eliminate all

rules

rules are rules of the form

, where

and

, where

is the CFG's

variable alphabet.
Remove every rule with

on its right hand side (RHS). For each rule with

set of new rules consisting of the different possible combinations of


with . If a rule has

in its RHS, add a

replaced or not replaced

as a singleton on its RHS, add a new rule

unless

has

already been removed through this process. For example, examine the following grammar

has one

rule. When the

is removed, we get the following:

Notice that we have to account for all possibilities of

and so we actually end up adding

3 rules.
3. Eliminate all unit rules

After all the

rules have been removed, you can begin removing unit rules, or rules whose RHS

contains one variable and no terminals (which is inconsistent with CNF).


To remove
, where

is a string of variables and terminals, add rule

unless this is a

unit rule which has already been removed.


4. Clean up the remaining rules that are not in Chomsky normal form.
Replace

wit

h
If

, where
, replace

in above rules with some new variable

(b) 1. Eliminating Useless Symbols from a CFG

are new variables.

and add rule

A symbol X is useful for a CFG if there is a derivation of the form S * X * w for


some string of terminals w.

If X is not useful, then we say X is useless.

To be useful, a symbol X needs to be


1. generating; that is, X needs to be able to derive some string of terminals.
2. reachable; that is, there needs to be a derivation of the form S * X where
and are strings of nonterminals and terminals.

To eliminate useless symbols from a grammar, we


1. identify the nongenerating symbols and eliminate all productions containing
one or more of these symbols, and then
2. eliminate all productions containing symbols that are not reachable from the
start symbol.

In the grammar
S AB | a
Ab
S, A, a, and b are generating. B is not generating.
Eliminating the productions containing the nongenerating symbols we get
Sa
Ab
Now we see A is not reachable from S, so we can eliminate the second production to
get
Sa

The generating symbols can be computed inductively bottom-up from the set of
terminal symbols.

The reachable symbols can be computed inductively starting from S.

2. Eliminating Unit Productions from a CFG

A unit production is one of the form A B where both A and B are nonterminals.

Let us assume we are given a grammar G with no -productions.

From G we can create an equivalent grammar H with no unit productions as follows.


o

Define (A, B) to be a unit pair if A * B in G.

We can inductively construct all unit pairs for G.

For each unit pair (A, B) in G, we add to H the productions A where B


is a nonunit production of G.

In the given grammar

F I
TF
ET
Are three unit productions.
Unit production F I can be removed by the help of
I a/b/Ia /Ib/I0/I1
and grammar becomes
I a/b/Ia /Ib/I0/I1
F (E)/ a/b/Ia /Ib/I0/I1
TF/T*F
E T/E+T
Now we can eliminate T F, by the help of
F (E)/ a/b/Ia /Ib/I0/I1
And now grammar become
I a/b/Ia /Ib/I0/I1
F (E)/ a/b/Ia /Ib/I0/I1
TT*F/(E)/ a/b/Ia /Ib/I0/I1
E T/E+T
Now let us remove E T by the help
T T*F/(E)/ a/b/Ia /Ib/I0/I1
And finally, unit production free grammar is
I a/b/Ia /Ib/I0/I1
F (E)/ a/b/Ia /Ib/I0/I1
TT*F/(E)/ a/b/Ia /Ib/I0/I1
E E+T/ T*F/(E)/ a/b/Ia /Ib/I0/I1
Q6
(a)

(b) Let the equivatent grammar be G.


The S productions are:
S [q0, Z0, q0]/ [q0, Z0, q1]
((q0,a, Z0)( q0,a Z0)) induces
[q0, Z0, q0] a [q0, a, q0] [q0, Z0, q0]
[q0, Z0, q0] a[q0, a,a] [q1, Z0, q0]
[q0, Z0, q1] a[q0, a, q0] [q0, Z0, q0]
[q0, Z0, q1] a[q0, a, q1][q1, Z0, q1]

((q0,a, a)( q1,aa)) induces


[q0, a, q1] a [q0, a, q0] [q0, a, q1]
[q0, a, q1] a[q0, a,q1] [q1, a, q1]

((q1,a, a)( q1,)) induces


[q1, a, q1] a
((q1,, Z0)( q1,)) induces
[q1, Z0 , q1]

Q7
(a) brain master page 124, Q-21
(b)
(c) brain master page 139, Q-53-v

S-ar putea să vă placă și