W e can alw a ys rename v ariables whatev er w e lik - - PDF document

w e can alw a ys rename v ariables whatev er w e lik e so
SMART_READER_LITE
LIVE PREVIEW

W e can alw a ys rename v ariables whatev er w e lik - - PDF document

Closure Prop erties of CFL's | Substitut i on If a substitution assigns a CFL to ev ery sym b ol s in the alphab et of a CFL L , then s ( L ) is a CFL. Pro of T ak e a grammar for L and a gramma r


slide-1
SLIDE 1 Closure Prop erties
  • f
CFL's | Substitut i
  • n
If a substitution s assigns a CFL to ev ery sym b
  • l
in the alphab et
  • f
a CFL L, then s(L) is a CFL. Pro
  • f
  • T
ak e a grammar for L and a gramma r for eac h language L a = s(a).
  • Mak
e sure all the v ariables
  • f
all these grammars are dieren t.

W e can alw a ys rename v ariables whatev er w e lik e, so this step is easy .
  • Replace
eac h terminal a in the pro ductions for L b y S a , the start sym b
  • l
  • f
the grammar for L a .
  • A
pro
  • f
that this construction w
  • rks
is in the reader.

In tuition: this replacemen t allo ws an y string in L a to tak e the place
  • f
an y
  • ccurrence
  • f
a in an y string
  • f
L. Example
  • L
= f0 n 1 n j n
  • 1g,
generated b y the grammar S ! 0S 1 j 01.
  • s(0)
= fa n b m j m
  • ng,
generated b y the grammar S ! aS b j A; A ! aA j ab.
  • s(1)
= fab; abcg, generated b y the grammar S ! abA; A ! c j . 1. Rename second and third S 's to S and S 1 , resp ectiv ely . Rename second A to B . Resulting grammars are: S ! 0S 1 j 01 S ! aS b j A; A ! aA j ab S 1 ! abB ; B ! c j
  • 2.
In the rst grammar, replace b y S and 1 b y S 1 . The com bined grammar: S ! S S S 1 j S S 1 S ! aS b j A; A ! aA j ab S 1 ! abB ; B ! c j
  • Consequences
  • f
Closure Under Substitut i
  • n
1. Closed under union, concatenation, star.

Pro
  • fs
are the same as for regular languages, e.g. for concatenation
  • f
CFL's L 1 , L 2 , use L = fabg, s(a) = L 1 , and s(b) = L 2 . 1
slide-2
SLIDE 2 2. Closure
  • f
CFL's under homomorphi sm . Nonclosure Under In tersection
  • The
reader sho ws the follo wing language L = f0 i 1 j 2 k 3 l j i = k and j = l g not to b e a CFL.

In tuitiv ely , y
  • u
need a v ariable and pro ductions lik e A ! 0A2 j 02 to generate the matc hing 0's and 2's, while y
  • u
need another v ariable to generate matc hing 1's and 3's. But these v ariables w
  • uld
ha v e to generate strings that did not in terlea v e.
  • Ho
w ev er, the simpler language f0 i 1 j 2 k 3 l j i = k g is a CFL.

A grammar: S ! S 3 j A A ! 0A2 j B B ! 1B j
  • Lik
ewise the CFL f0 i 1 j 2 k 3 l j j = l g.
  • Their
in tersection is L. Nonclosure
  • f
CFL's Under Complemen t
  • Pro
  • f
1: Since CFL's are closed under union, if they w ere also closed under complemen t, they w
  • uld
b e closed under in tersection b y DeMorgan's la w.
  • Pro
  • f
2: The complemen t
  • f
L ab
  • v
e is a CFL. Here is a PD A P recognizing it:

Guess whether to c hec k i 6= k
  • r
j 6= l . Sa y w e w an t to c hec k i 6= k .

As long as 0's come in, coun t them
  • n
the stac k.

Ignore 1's.

P
  • p
the stac k for eac h 2.

As long as w e ha v e not just exp
  • sed
the b
  • ttom-of-stac
k mark er when the rst 3 comes in, accept, and k eep accepting as long as 3's come in.

But w e also ha v e to accept, and k eep accepting, as so
  • n
as w e see that the input is not in L(0
  • 1
  • 2
  • 3
  • ).
Closure
  • f
CFL's Under Rev ersal Just rev erse the b
  • dy
  • f
ev ery pro duction. 2
slide-3
SLIDE 3 Closure
  • f
CFL's Under In v erse Homomorphism PD A-based construction.
  • Keep
a \buer" in whic h w e place h(a) for some input sym b
  • l
a.
  • Read
inputs from the fron t
  • f
the buer ( OK).
  • When
the buer is empt y , it ma y b e reloaded with h(b) for the next input sym b
  • l
b,
  • r
w e ma y con tin ue making
  • mo
v es. T esting Emptiness
  • f
a CFL As for regular languages, w e really tak e a represen tation
  • f
some language and ask whether it represen ts ;.
  • In
this case, the represen tation can b e a CF G
  • r
PD A.

Our c hoice, since there are algorithms to con v ert
  • ne
to the
  • ther.
  • The
test: Use a CF G; c hec k if the start sym b
  • l
is useless? T esting Finiteness
  • f
a CFL
  • Let
L b e a CFL. Then there is some pumping- lemma constan t n for L.
  • T
est all strings
  • f
length b et w een n and 2n
  • 1
for mem b ership (as in next section).
  • If
there is an y suc h string, it can b e pump ed, and the language is innite.
  • If
there is no suc h string, then n
  • 1
is an upp er limit
  • n
the length
  • f
strings, so the language is nite.

T ric k: If there w ere a string z = uv w xy
  • f
length 2n
  • r
longer, y
  • u
can nd a shorter string uw y in L, but it's at most n shorter. Th us, if there are an y strings
  • f
length 2n
  • r
more, y
  • u
can rep eatedly cut
  • ut
v x to get, ev en tually , a string whose length is in the range n to 2n
  • 1.
T esting Mem b ership
  • f
a String in a CFL Sim ulating a PD A for L
  • n
string w do esn't quite w
  • rk,
b ecause the PD A can gro w its stac k indenitely
  • n
  • input,
and w e nev er nish, ev en if the PD A is deterministic. 3
slide-4
SLIDE 4
  • There
is an O (n 3 ) algorithm (n = length
  • f
w ) that uses a \dynamic programmi ng" tec hnique.

Called Co c k e-Y
  • unger-Kasami
(CYK) algorithm.
  • Start
with a CNF grammar for L.
  • Build
a t w
  • -dimensional
table:

Ro w = length
  • f
a substring
  • f
w .

Column = b eginning p
  • sition
  • f
the substring.

En try in ro w i and column j = set
  • f
v ariables that generate the substring
  • f
w b eginning at p
  • sition
j and extending for i p
  • sitions.

In reader, these en tries are denoted X j;i+j 1 , i.e., the subscripts are the rst and last p
  • sitions
  • f
the string represen ted, so the rst ro w is X 11 ; X 22 ; : : : ; X nn , the second ro w is X 12 ; X 23 ; : : : ; X n1;n , and so
  • n.
Basis : (ro w 1) X ii = the set
  • f
v ariables A suc h that A ! a is a pro duction, and a is the sym b
  • l
at p
  • sition
i
  • f
w . Induction : Assume the ro ws for substrings
  • f
length up to m
  • 1
ha v e b een computed, and compute the ro w for substrings
  • f
length m.
  • W
e can deriv e a i a i+1
  • a
j from A if there is a pro duction A ! B C , B deriv es an y prex
  • f
a i a i+1
  • a
j , and C deriv es the rest.
  • Th
us, w e m ust ask if there is an y v alue
  • f
k suc h that

i
  • k
< j .

B is in X ik .

C is in X k +1;j . Example In class, w e'll w
  • rk
the table for the grammar: S ! AS j S B j AB A ! a B ! b and the string aabb. 4