W ell sort of; y ou lose the abilit y to generate as a - - PDF document

w ell sort of y ou lose the abilit y to generate as a
SMART_READER_LITE
LIVE PREVIEW

W ell sort of; y ou lose the abilit y to generate as a - - PDF document

Cleaning Up Grammars W e can \simplify" grammars to a great exten t. Some of the things w e can do are: 1. Get rid of sym b ols | those that do useless not participate in an y deriv ation of a terminal


slide-1
SLIDE 1 Cleaning Up Grammars W e can \simplify" grammars to a great exten t. Some
  • f
the things w e can do are: 1. Get rid
  • f
useless sym b
  • ls
| those that do not participate in an y deriv ation
  • f
a terminal string. 2. Get rid
  • f
  • pr
  • ductions
| those
  • f
the form v ariable ! .

W ell sort
  • f;
y
  • u
lose the abilit y to generate
  • as
a string in the language. 3. Get rid
  • f
unit pr
  • ductions
| those
  • f
the form v ariable ! v ariable. 4. Chomsky normal form |
  • nly
pro duction forms are v ariable ! t w
  • v
ariables and v ariable ! terminal. Useless Sym b
  • ls
In
  • rder
for a sym b
  • l
X to b e useful, it m ust: 1. Deriv e some terminal string (p
  • ssibly
X is a terminal). 2. Be r e achable from the start sym b
  • l;
i.e., S ) * X
  • .
  • Note
that X w
  • uldn't
really b e useful if
  • r
  • included
a sym b
  • l
that didn't satisfy (1), so it is imp
  • rtan
t that (1) b e tested rst, and sym b
  • ls
that don't deriv e terminal strings b e eliminated b efor e testing (2). Finding Sym b
  • ls
That Don't Deriv e An y T erminal String Recursiv e construction: Basis : A terminal surely deriv es a terminal string. Induction : If A is the head
  • f
a pro duction whose b
  • dy
is X 1 X 2
  • X
k , and eac h X i is kno wn to deriv e a terminal string, then surely A deriv es a terminal string.
  • Keep
going un til no more sym b
  • ls
that deriv e terminal strings are disco v ered. Example S ! AB j C ; A ! 0B j C ; B ! 1 j A0; C ! AC j C 1.
  • Round
1: and 1 are \in."
  • Round
2: B ! 1 sa ys B is in. 1
slide-2
SLIDE 2
  • Round
3: A ! 0B sa ys A is in.
  • Round
4: S ! AB sa ys S is in.
  • Round
5: Nothing more can b e added.
  • Th
us, C can b e eliminated, along with an y pro duction that men tions it, lea ving S ! AB ; A ! 0B ; B ! 1 j A0. Finding Sym b
  • ls
That Cannot Be Deriv ed F rom the Start Sym b
  • l
Another recursiv e algorithm: Basis : S is \in." Induction : If v ariable A is in, then so is ev ery sym b
  • l
in the pro duction b
  • dies
for A.
  • Keep
going un til no more sym b
  • ls
deriv able from S can b e found. Example S ! AB ; A ! 0B ; B ! 1 j A0.
  • Round
1: S is in.
  • Round
2: A and B are in.
  • Round
3: and 1 are in.
  • Round
4: Nothing can b e added.
  • In
this case, all sym b
  • ls
are deriv able from S , so no c hange to gramma r.
  • Reader
has an example where not
  • nly
are there sym b
  • ls
not deriv able from S , but y
  • u
must eliminate rst the sym b
  • ls
that don't deriv e terminal strings,
  • r
y
  • u
get the wrong grammar. Eliminati ng
  • Pro
duction s A v ariable A is nul lable if A ) * . Find them b y a recursiv e algorithm: Basis : If A !
  • is
a pro duction, then A is n ullable. Induction : If A is the head
  • f
a pro duction whose b
  • dy
consists
  • f
  • nly
n ullable sym b
  • ls,
then A is n ullable.
  • Once
w e ha v e the n ullable sym b
  • ls,
w e can add additional pro ductions and then thro w a w a y the pro ductions
  • f
the form A !
  • for
an y A. 2
slide-3
SLIDE 3
  • If
A ! X 1 X 2
  • X
k is a pro duction, add all pro ductions that can b e formed b y eliminating some
  • r
all
  • f
those X i 's that are n ullable.

But, don't eliminate all k if they are all n ullable. Example If A ! B C is a pro duction, and b
  • th
B and C are n ullable, add A ! B j C . Eliminati ng Unit Pro ductions 1. Eliminate useless sym b
  • ls
and
  • pro
ductions. 2. Disco v er those pairs
  • f
v ariables (A; B ) suc h that A ) * B .

Because there are no
  • pro
ductions, this deriv ation can
  • nly
use unit pro ductions.

Th us, w e can nd the pairs b y computing reac hablit y in a graph where no des = v ariables, and arcs = unit pro ductions. 3. Replace eac h com bination where A ) * B )
  • and
  • is
  • ther
than a single v ariable b y A ! .

I.e., \short circuit" sequences
  • f
unit pro ductions, whic h m ust ev en tually b e follo w ed b y some
  • ther
kind
  • f
pro duction. Remo v e all unit pro ductions. Chomsky Normal F
  • rm
0. Get rid
  • f
useless sym b
  • ls,
  • pro
ductions, and unit pro ductions (already done). 1. Get rid
  • f
pro ductions whose b
  • dies
are mixes
  • f
terminals and v ariables,
  • r
consist
  • f
more than
  • ne
terminal. 2. Break up pro duction b
  • dies
longer than 2.
  • Result:
All pro ductions are
  • f
the form A ! B C
  • r
A ! a. No Mixed Bo dies 1. F
  • r
eac h terminal a, in tro duce a new v ariable A a , with
  • ne
pro duction A a ! a. 2. Replace a in an y b
  • dy
where it is not the en tire b
  • dy
b y A a .

No w, ev ery b
  • dy
is either a single terminal
  • r
it consists
  • nly
  • f
v ariables. 3
slide-4
SLIDE 4 Example A ! 0B 1 b ecomes A ! 0; A 1 ! 1; A ! A B A 1 . Making Bo dies Short If w e ha v e a pro duction lik e A ! B C D E , w e can in tro duce some new v ariables that allo w the v ariables
  • f
the b
  • dy
to b e in tro duced
  • ne
at a time.
  • A
b
  • dy
  • f
length k requires k
  • 2
new v ariables.
  • Example:
In tro duce F and G; replace A ! B C D E b y A ! B F ; F ! C G; G ! D E . Summary Theorem If L is an y CFL, there is a gramma r G that generates L
  • fg,
for whic h eac h pro duction is
  • f
the form A ! B C
  • r
A ! a, and there are no useless sym b
  • ls.
CFL Pumping Lemma Similar to regular-language PL, but y
  • u
ha v e to pump t w
  • strings
in the middle
  • f
the string, in tandem (i.e., the same n um b er
  • f
copies
  • f
eac h). F
  • rmally:
  • 8
CFL L
  • 9
in teger n
  • 8
z in L, with jz j
  • n
  • 9
uv w xy = z suc h that jv w xj
  • n
and jv xj >
  • 8
i
  • 0,
uv i w x i y is in L. Outline
  • f
Pro
  • f
  • f
PL
  • Let
there b e a Chomsky-normal
  • fo
rm CF G for L with m v ariables. Pic k n = 2 m .
  • Because
CNF grammars ha v e b
  • dies
  • f
no more than 2 sym b
  • ls,
a string z
  • f
length
  • n
m ust ha v e some path with at least m + 1 v ariables.
  • Th
us, some v ariable m ust app ear t wice
  • n
the path.

Compare with the DF A argumen t ab
  • ut
a path longer than the n um b er
  • f
states. 4
slide-5
SLIDE 5
  • F
  • cus
  • n
some path that is as long as an y path in the tree. In this path, w e can nd a duplication
  • f
some v ariable A among the b
  • ttom
m + 1 v ariables
  • n
the path.

Let the lo w er A deriv e w and the upp er A deriv e v w x.
  • CNF
guaran tees us that jv w xj
  • n
and v x 6= .
  • By
rep eatedly replacing the lo w er A's tree b y the upp er A's tree, w e see uv i w x i y has a parse tree for all i > 1.

And replacing the upp er b y the lo w er sho ws the case i = 0; i.e., uw y is in L. Example L = f0 k 2 j k is an y in tegerg is not a CFL.
  • Supp
  • se
it w ere. Then let n b e the PL constan t for L.
  • Consider
z = n 2 . W e can write z = uv w xy , with jv w xj
  • n
and jv xj > 0.
  • Then
uv v w xxy is in L. But n 2 < juv v w xxy j
  • n
2 + n < (n + 1) 2 , so there is no p erfect square that juv v w xxy j could b e.
  • By
\pro
  • f
b y con tradiction," L is not a CFL. 5