Sei sulla pagina 1di 18

Closure Properties of Context Free Languages

278
Union of CFLs
Let L
1
be language recognized by G
1
= (V
1
, T
1
, S
1
, P
1
) and L
2
the
language recognized by G
2
= (V
2
, T
2
, S
2
, P
2
)
Is L
1
L
2
a context free language? Yes.
Just add the rule S S
1
|S
2
But make sure that V
1
V
2
= (by renaming some variables). So
formally G has
V = V
1
V
2
{S}
T = T
1
T
2
P = P
1
P
2
{S S
1
|S
2
}
279
Concatenation, Kleene Closure
Let L
1
be language recognized by G
1
= (V
1
, T
1
, S
1
, P
1
) and L
2
the
language recognized by G
2
= (V
2
, T
2
, S
2
, P
2
)
Concatenation: L
1
L
2
generated by adding S S
1
S
2
Kleene Closure: L

1
generated by adding S S
1
S|
As before, ensure that V
1
V
2
= , S is a new start symbol.
280
Homomorphism
Proposition: Context free languages are closed under homomorphisms.
Let G = (V, T, S, P) be the grammar generating L, and let h : T

be a homomorphism.
h(G) is a new grammar over terminals , where the productions are
obtained by taking the productions of G and replacing each symbol
a by h(a).
h(G) generates h(L) (proof left to reader).
Example: S 0S0|1S1| and h(0) = aba and h(1) = bb. The h(G) has
the following rules: S abaSaba|bbSbb|
281
Substitution
Recall, Substitution means we associate each symbol a with a language
L
a
and the image of L under the substitution is dened as
{w
1
w
2
w
n
| a
1
a
2
a
n
L and w
i
L
a
i
}
Regular substitution: Every L
a
is a regular language
CFL Substitution: Every L
a
is a CFL
Homomorphism is a special case of substitution
Proposition: Every CFL L is closed under CFL substitution.
Proof: Let L be generated by G, and let L
a
be generated by
G
a
= (V
a
, T
a
, S
a
, P
a
). Then in each production of G, replace the symbol
a by S
a
This allows any word in L
a
to substitute symbol a
It must be the case that the sets V
a
and V (variables of G) are all
disjoint.
282
Intersection
Let L
1
and L
2
be context free languages. L
1
L
2
is not context free!!
L
1
= {a
i
b
i
c
j
| i, j 0} is CFL
S XY
X aXb|
Y cY |
L
2
= {a
i
b
j
c
j
| i, j 0} is a CFL
S XY
X aX|
Y bY c|
But L
1
L
2
= {a
n
b
n
c
n
| n 0} is not context free.
283
Intersection with Regular Languages
Proposition: If L is a CFL and R is a regular language then L R is a
CFL.
Proof: Let P be the PDA that accepts L by nal state, and let M be the
DFA that accepts R. The PDA recognizing L R simulates P and R
simultaneously, and accepts if both P and M accept the input.
Like in the cross-product construction, states of the new machine
are pairs of states, where one element of the pair corresponds to the
state of P and the other corresponds to the state of M.
Whenever, an input symbol is read, both P and M are simulated; if
P needs to make an move then M remains stationary.
Why does this construction not work for intersection of CFLs?
284
Complementation
Let L be a context free language. Is L context free? No!
Reason 1: If it were closed under complementation, then by De Morgans
Law, and by the fact that CFLs are closed under union, CFLs would be
closed under intersection.
Reason 2: L = {x | x not of the form ww} is a CFL. But
L = {ww | w {a, b}

} is not a CFL.
If L were a CFL, then L

= L a

= {a
i
b
j
a
i
b
j
| i, j 0}
would be a CFL. But L

is not a CFL!
285
Set Dierence
If L
1
is a CFL and L
2
is a CFL then L
1
\ L
2
is not a CFL
CFL not closed under complementation, and complementation is a
special case.
If L is a CFL and R is a regular language then L \ R is a CFL
L \ R = L R
286
Inverse Homomorphisms
Recall, let L be a language and h a homomorphism. The
h
1
(L) = {w | h(w) L}
Proposition: If L is CFL then h
1
(L) is CFL.
Proof: For regular languages, the DFA for h
1
(L) on reading a symbol
a, simulated the DFA for L on h(a). Can we do the same with PDAs?
The problem is that we need to ensure that the pushes and pops on the
stack are done correctly, when we simulate the PDA on h(a).
The key idea is to store the h(a) on a buer and process symbols from
h(a) one at a time, and the next input symbol is processed only after the
buer has been emptied.
Where do we store this buer? In the state of the new PDA!
Construction details in the book.
287
Decision Problems for CFLs
288
Algorithms for CFLs
CFL given as a grammar or PDA.
Size of grammar or PDA is the length of the representation
Size of grammar is an upper bound on number of variables,
number of productions, length of each production etc.
Size of PDA is an upper bound on the number of states, number
of transitions, number of symbols pushed onto to the stack in
any step, etc.
289
Converting PDA Acceptance forms
Converting Final State Acceptance to Empty Stack Acceptance
Push an additional stack symbol at the bottom, and have new initial
state and popping state.
Time = O(n)
Converting Empty Stack Acceptance to Final State Acceptance
Push additional stack symbol at the bottom, and have new initial
state and nal state.
Time = O(n)
290
Converting CFG to PDA
Constructed PDA has one state, and one transition rule for each
production in the grammar.
New PDA has same size as CFG
Time = O(n)
291
Converting PDA to CFG
Number of new variables of the form [pXq] = O(n
3
)
For each transition (r
0
, Y
1
Y
2
Y
k
) (p, a, X) we have productions
[pXr
k
] a[r
0
Y
1
r
1
] [r
k1
Y
k
r
k
], for every choice of r
1
, . . . r
k
.
So each PDA transition can result in O(n
n
) production rules.
Can convert any PDA into a PDA which pushes at most 2 symbols
onto stack at each step. (exercise)
Each transition results in O(n
2
) productions.
Overall convertion O(n
3
) time.
292
Eliminating Useless Symbols
Generating Symbols: Recall, the algorithm is
If A x, where x T

, is a production then A is generating


If A is a production and all variables in are generating, then A is generating.
Each iteration of step 2, discovers at least one new variable. So
number of iterations = O(n).
Time in each iteration = O(n)
Total time = O(n
2
)
Reachable symbols: Time taken is O(n
2
)
293
Eliminating Unit Productions
Determining Unit Pairs: Once again, the algorithm is
A, A is unit pair
If A, B is a unit pair and B C is a production then A, C is a unit pair.
Number of iterations = O(n
2
)
Time in each iteration = O(n)
Total time = O(n
3
)
Eliminating Unit Pairs: Use unit pairs to add new productions; each unit
pair results in O(n) new rules. So total time = O(n
3
).
294
Eliminating -Productions
Determining Nullable variables = O(n
2
) time
Each production A B
1
B
2
B
k
adds new rules depending on whether
each B
i
is nullable. If each B
i
is nullable we have 2
k
1 new rules.
New size of grammar = O(2
n
)
Transform grammar by rst bounding the length of right hand side of
productions to length 2.
Size of bounded length grammar = O(n)
After -elimination, size = O(n).
295

Potrebbero piacerti anche