properties of context-free languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · properties of...

34
Properties of context-Free Languages We simplify CFL’s. Greibach Normal Form Chomsky Normal Form We prove pumping lemma for CFL’s. We study closure properties and decision properties. Some of them remain, some not. Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 1 / 26

Upload: others

Post on 19-May-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Properties of context-Free Languages

We simplify CFL’s.Greibach Normal FormChomsky Normal Form

We prove pumping lemma for CFL’s.We study closure properties and decision properties. Some of them remain,some not.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 1 / 26

Page 2: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Greibach Normal Form

It would be nice to know which rule to selectexpecially dificult is the left recursion A→ Aα

DefinitionGreibach Normal Form or a CFG We say a grammar G is in Greibach NormalForm iff all rules are in the form A→ aβ, where a ∈ T , β ∈ V ∗ (a string ofvariables).

the terminal in the body of the rule helps to select appropriate ruleexpecially if we have a unique rule with this terminal.

Theorem (Greibach Normal Form)For each CFL language L there exists a CFG grammar G in Greibach Normal Formsuch that L(G) = L− {λ}.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 2 / 26

Page 3: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Join the rules

Lemma (Joining the rules)Let us have a rule A→ αBβ in grammar G and B → ω1, . . . ,B → ωk are all rulesfor B.

If we replace the rule A→ αBβ by rules A→ αω1β, . . . ,A→ αωkβ

we get an equivalent grammar.

Proof:

A⇒ αBβ ⇒∗ α|Bβ ⇒ α|ωiβ in the original grammarA⇒ αωiβ ⇒∗ α|ωiβ in the new grammar

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 3 / 26

Page 4: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Left Recursion

Lemma (Left Recursion)Let A→ Aω1, . . . ,A→ Aωk be all productions with left recursion in the grammarG for A and A→ α1, . . . ,A→ αm be all other rules for A, Z is a new variable.Then, by replaceing these rules by the rules:1. A→ αi ,A→ αiZ ,Z → ωj ,Z → ωjZ, nebo2. A→ αiZ ,Z → ωjZ ,Z → λwe get an equivalent grammar.

Proof:

A⇒ Aωin ⇒ . . .⇒ Aωi1 . . . ωin ⇒ αjωi1 . . . ωin (G)A⇒ αjZ ⇒ αjωi1Z . . .⇒ αjωi1 . . . ωin−1Z ⇒ αjωi1 . . . ωin (1)A⇒ αjZ ⇒ αjωi1Z . . .⇒ αjωi1 . . . ωinZ ⇒ αjωi1 . . . ωin (2)

Theorem (Greibach Normal Form)For each CFL language L there exists a CFG grammar G in Greibach Normal Formsuch that L(G) = L− {λ}.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 4 / 26

Page 5: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Proof: Greibach Normal Form

join rules and remove left recursionwe enumerate all variables {A1, . . . ,An}we allow recursion only in the form Ai → Ajω, where i < jwe iterate i from 1 to nAi → Ajω for j < i removed by joining the rules

for j = i remove left recursionwe get rules in the form Ai → Ajω (i < j), Ai → aω (a ∈ T ), Zi → ω

rules with Ai (original variables) only in the form Ai → aωiteratively join the rules for i from n to 1 (for n already holds)rules with Zi (new variables) only in the form Zi → aω

In none rule for Zi the body begins with Zjeither it is in required form or we join it with the rule Aj → aω

we remove terminals inside rules.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 5 / 26

Page 6: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Reduction to GreibachNF Example

Original GrammarE → E + T |TT → T ∗ F |FF → (E )|a

Left Recursion RemovedE → T |TE |E | → +T |+ TE |T → F |FT |T | → ∗F | ∗ FT |F → (E )|a

(almost) Greibach Normal FormE → (E )|a|(E )T ||aT ||(E )E ||aE ||(E )T |E ||aT |E |E | → +T |+ TE |T → (E )|a|(E )T ||aT |T | → ∗F | ∗ FT |F → (E )|a

Greibach Normal FormE → (EP|a|(EPT ||aT ||(EPE ||aE ||(EPT |E ||aT |E |

E | → +T |+ TE |

T → (EP|a|(EPT ||aT |

T | → ∗F | ∗ FT |

F → (EP|aP →)

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 6 / 26

Page 7: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Normal Forms for Context-Free Grammars

Chomsky Normal Form: all production are of the form A→ BC or A→ a,A,B,C where are variables, a is a terminal.Every CFL (without λ) is generated by a CFG in Chomsky Normal Form.

To get there, we perform simplificationsEliminate useless symbolseliminate λ-productions A→ λ for some variable Aeliminate unit productions A→ B for variables A,B.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 7 / 26

Page 8: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Eliminating Useless symbols

Definition (useful symbol)A symbol X is useful for a grammar G = (V ,T ,P,S) if there is somederivation of the form S ∗⇒ αXβ ∗⇒ w where w ∈ T ∗,X ∈ (V ∪ T ).If X is not useful, we say it is useless.X is generating if X ∗⇒ w for some terminal string w . Note always w ∗⇒ wby zero steps.X is reachable if the is S ∗⇒ αXβ for some α, β.

We aim eliminate non generating and not reachable symbols.

Example

Consider the grammar:S → AB|aA→ b

Eliminate B(nongenerating):S → aA→ b.

Eliminate A(not reachable):S → a.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 8 / 26

Page 9: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Theorem (Eliminating useless symbols)Let G = (V ,T ,P,S) be a CFG, and assume that L(G) 6= ∅. LetG1 = (V1,T1,P1,S) is obtained:

Eliminate nongenerating symbols and all productions involving them.Eliminate all symbols that are not reachable after previous step.

Then G1 has no useless symbols, and L(G1) = L(G).

Generating symbols:BASIS: Every a ∈ T isgenerating.INDUCTION: For anyproduction A→ α and everysymbol of α is generating. ThenA is generating.(This includes A→ ∅).

Reachable symbols:BASIS: S is surely reachable.INDUCTION: If A isreachable, for all productionwith A in the head, allsymbols of the bodies arealso reachable.

TheoremThe algorithms above find all and only the generating / reachable symbols.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 9 / 26

Page 10: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Eliminating λ-Productions

Without λ-production, λ /∈ L.We aim to prove: L has a CFG, then L− {λ} has a CFG withoutλ-productions.

Definition (nullable variable)

A variable A is nullable if A ∗⇒ λ.

For nullable variables in the body B → CAD, we create two versions of theproduction - with and without this variable.An algorithm to find all nullable symbols of G:

If A→ λ is a production of G , then A is nullable.If B → C1 . . .Ck where each Ci is nullable, then B is nullable (note terminalCi ∈ T is not nullable).

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 10 / 26

Page 11: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Construction of a grammar without λ-productions from G = (V ,T ,P,S).Determine nullable symbols.For each production A→ X1 . . .Xk ∈ P, k ≥ 1 suppose m of Xi ’s arenullable. The new grammar G1 will have 2m versions of this productionwith/without each nullable symbol except λ in case m = k.

Example

Consider the grammar:S → ABA→ aAA|λB → bBB|λ

S → AB|A|BA→ aAA|aA|aA|aB → bBB|bB|bB|b

Final grammar:S → AB|A|BA→ aAA|aA|aB → bBB|bB|b.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 11 / 26

Page 12: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Eliminating Unit Productions

Definition (unit production)A unit production is A→ B ∈ P where both A,B are variables.

Example

I → a|b|Ia|Ib|I0|I1F → I|(E )T → F |T ∗ FE → T |E + T

Expanding T in E → TE → F |T ∗ FExpanding E → FE → I|(E )

Expanding E → IE → a|b|Ia|Ib|I0|I1

Together: E → a|b|Ia|Ib|I0|I1|(E )|T ∗ F |E + T .

We have to avoid possible cycles.

Definition (unit pair)

A pair A,B ∈ V such that A ∗⇒ B using only unit productions is called a unit pair.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 12 / 26

Page 13: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Unit pairs identificationBASIS: (A,A) for A ∈ V is a unit pair.INDUCTION: If (A,B) is a unit pair and B → C ∈ P, then (A,C) is a unitpair.

Example (Unit pairs from previous grammar)(E ,E ), (T ,T ), (F ,F ), (I, I), (E ,T ), (E ,F ), (E , I), (T ,F ), (T , I), (F , I).

To eliminate unit productions from G = (V ,T ,P,S):Find all unit pairs of G .For each unit pair (A,B) new grammar all productions A→ α thereB → α ∈ P.

Example

I → a|b|Ia|Ib|I0|I1F → I|(E )T → F |T ∗ FE → T |E + T

I → a|b|Ia|Ib|I0|I1F → (E )|a|b|Ia|Ib|I0|I1T → T ∗ F |(E )|a|B|Ia|Ib|I0|I1E → E + T |T ∗ F |(E )|a|b|Ia|Ib|I0|I1

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 13 / 26

Page 14: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Normal Form of CFGs

Theorem (Normal Form of CFGs)If G is a CFG, L(G) \ {λ} 6= ∅, then there is a CFG G1 such thatL(G1) = L(G) \ {λ} and G1 has no λ-productions, unit productions, or uselesssymbols.

Proof.Proof outline:

Start by eliminating λ-productions.Eliminate unit productions. This does not introduce λ-productions.Eliminate useless symbols. This does not introduce any new production.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 14 / 26

Page 15: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Chomsky Normal Form

Definition (Chomsky Normal Form)A CFG grammar G = (V ,T ,P,S) that has no useless symbols and all productionsare in on of two forms:

A→ BC , A,B,C ∈ V ,A→ a, A ∈ V , a ∈ T ,

is said to be in Chomsky Normal Form (CNF).

To put a grammar to CNF, we need two additional steps:Bodies of length 2 or more consist only of variables.Break bodies of length 3 or more to bodies of two variables.

For every terminal a create a newvariable, say A,add one production rule A→ a,Use A in place of a everywhere aappears in a body of length 2 ormore.

For a productionA→ B1 . . .Bk introducek − 2 variables Ci

And productionsA→ B1C1,C1 → B2C2,. . . ,Ck−2 → Bk−1Bk .

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 15 / 26

Page 16: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

CNF

ExampleI → a|b|IA|IB|IZ |IUF → LER|a|b|Ia|IB|IZ |IUT → TMF |LER|a|b|IA|IB|IZ |IUE → EPT |TMF |LER|a|b|IA|IB|IZ |IUA→ aB → bZ → 0U → 1P → +M → ∗L→ (R →)

F → LC3|a|b|Ia|IB|IZ |IUT → TC2|LC3|a|b|IA|IB|IZ |IUE → EC1|TC2|LC3|a|b|IA|IB|IZ |IUC1 → PTC2 → MFC3 → ERI,A,B,Z ,U,P,M, L,R as before

Theorem (CNF)If G is a CFG, L(G) \ {λ} 6= ∅, then there is a grammar G1 inChomsky Normal Form such that L(G1) = L(G) \ {λ}.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 16 / 26

Page 17: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

The Size of the Parse Tree

Theorem (The Size of Parse Trees)Suppose we have a parse tree according the a CNF grammar G = (V ,T ,P,S)that yield a terminal string w. If the length of the longeeset path is n, then|w | ≤ 2n−1.

Proof.By induction on n, BASIS: |a| = 1 = 20, INDUCTION: 2n−2 + 2n−2 = 2n−1.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 17 / 26

Page 18: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Pumping Lemma for Context Free Languages

Theorem (Pumping Lemma for ContextFree Languages)Let L be a CFL. Then there existsconstants p, q ∈ N such that anyz ∈ L, |z | ≥ p can be written z = uvwxysubject to:|vwx | ≤ q.vx 6= λ.∀i ≥ 0, uv iwx iy ∈ L.

A1

A2

u v w x y

T 1

T 2

v w x

Proof Idea:

take the parse tree for zfind the longest paththere must be two equalvariablesthese variables define twosubtrees

the subtrees define partition ofz = uvwxywe can move the tree T 1

(i > 1)or replace T 1 by T 2 (i = 0)

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 18 / 26

Page 19: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Proof: |z | > p : z = uvwxy , |vwx | ≤ q, vx 6= λ,∀i ≥ 0uv iwx iy ∈ L

we take the grammar in Chomsky NF (for L = {λ} and ∅ proofseparatelly).Let |V | = k. We set p = 2k−1, q = 2k .For z ∈ L,|z | ≥ n, the parse tree has a path z of length > k

we denote the terminal of the longest path t

At least two of the last k variables on the path to t are equalwe take the couple A1,A2 closest to t (it defines subtrees T 1,T 2)the path from A1 to t is the longest in T 1 and the length is maximallyk + 1

the yield of T 1 is no longer than 2k (so |vwx | ≤ q)there are two paths from A1 (ChNF), one to T 2 other to the rest ofvx

ChNF not nullable, so vx 6= λ

derivation of the word (A1 ⇒∗ vA2x ,A2 ⇒ w)S ⇒∗ uA1y ⇒∗ uvA2xy ⇒∗ uvwxy

if we move A2 to A1

(i = 0)S ⇒∗ uA2y ⇒∗ uwy

if we move A1 to A2 (i = 2, 3, . . .)S ⇒∗ uA1y ⇒∗ uvA1xy ⇒∗uvvA2xxy ⇒∗ uvvwxxy

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 19 / 26

Page 20: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Applications of the Pomping Lemma for CFL’s

"Adversary game" as for regular languages:Pick a language L that is not CFL.Our ’adversary’ gets to pik n, which we do not know.We get to pick z , and we may use n as a parameter.Our adversary gets to berak z into uvwxy , subject |vwx | ≤ n and vx 6= λ.We ’win’ the game, if by picking i and showing uv iwx iy is not in L.

Lemma (Not CFL)Following languages are not CFL:{0n1n2n|n ≥ 1}{0i1j2i3j |i ≥ 1&j ≥ 1}{ww |w is in {0, 1}∗}

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 20 / 26

Page 21: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Pumping Lemma Ussage

Example (non CFL)Following language is not CFL{0n1n2n|n ≥ 1}

assume it were CFLwe get p, q from the PumpingLemmaselect k = max(p, q), then|0k1k2k | > pthe word is not longer then qwe pump at most two differentsymbolsthe equality of symbols is violated– CONTRADICTION.

Example (not a CFL)Following language is not CFL

{0i1j2k |0 ≤ i ≤ j ≤ k}

assume it were CFLwe get p, q from the PumpingLemmaselect n = max(p, q), then|0n1n2n| > pthe word is not longer then qwe pump at most two differentsymbolsin the case of a (or b), pump up –CONTRADICTION i ≤ j (or j ≤ k)if c (or b), pump down –CONTRADICTION j ≤ k (or i ≤ j)

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 21 / 26

Page 22: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Generalisations of the Pumping Lemma

Pumping lemma is only an implication.

Example (non CFL, that can be pumped)

L = {aibjckd l |i = 0 ∨ j = k = l} is not a CFL but it can be pumped.

i = 0 : bjckd l can be pumped in any letteri > 0 : aibncndn can be pumped in a∗

A solution?generalisations (Ogden lemma and others)

pumping marked symbolsclosure properties.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 22 / 26

Page 23: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Closure Properties of CFLs

Theorem (CFLs are closed onsubstitution)If L is a CFL over Σ, and s is asubstitution on Σ such that s(a) is aCFL for each a ∈ Σ, then s(L) is CFL.

Proof.Idea: Replace each a by the start symbol of a CFG for language s(a).First, rename variables to be unique in all G = (V ,T ,P,S),Ga = (Va,Ta,Pa,Sa), a ∈ Σ.We construct a new grammar G = (V ′,T ′,P ′,S) for s(L):

V ′ = V ∪⋃

a∈Σ Va

T ′ =⋃

a∈Σ Ta

P ′ =⋃

a∈Σ Pa ∪ {p ∈ P with all a replaced by Sa}.

G ′ generates the language s(L).

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 23 / 26

Page 24: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

CFL closed under

From Substitution theorem we can prove:

Theorem (CFL’s closed under union, concatenation, closure, homomorphism.)The CFL’s are slosed under union, concatenation, closure (∗), positive closure(+), homomorphism.

Theorem (CFL’s closed under reversal)

If L is a CFL, then so is LR .

Proof.Reverse all productions.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 24 / 26

Page 25: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

CFL’s and intersection

Example (CFLs are not closed on intersection)The langulageL = {0n1n2n|n ≥ 1} = {0n1n2i |n ≥ 1, i ≥ 1} ∩ {0i1n2n|n ≥ 1, i ≥ 1} is not CFL.

Theorem (CFLs are closed onintersection with a regular language)If L is a CFL and F is a regularlanguage, then L ∩ R is a CFL.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 25 / 26

Page 26: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Theorem (CFLs are not closed on complement)Let L, L1, L2 be CFLs, R a regular language.

L− R is a CFL. L− R = L− R, R is regular.L is not necessarily a CFL. L1 ∩ L2 = L1 ∩ L2.L1 − L2 is not necessarily a CFL. Σ∗ − L not always CFL.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 26 / 26

Page 27: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

CFLs are closed on inverse homomorphism

Theorem (CFLs are closed on inversehomomorphism)Let L be a CFL and h a homomorphism,then h−1(L) is a CFL.

Proof.After a is read, h(a) is placed in a ’buffer’.The symbols of h(a) are one at a time fed to the PDA being simulated.Only when the buffer is empty does the constructed PDA read another of itsinput symbols.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 27 / 26

Page 28: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Closure Properties

Language Regular Context-free Dyckunion YES YES NOintersection YES NO NO∪ with RL YES YES YEScomplement YES NO YEShomomorphism YES YES NOinverse hom. YES YES YES

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 28 / 26

Page 29: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Complexity of Converting among CFG’s and PDA’s

Conversions linear in the size of the input:CFG to a PDA.PDA by final state to a PDA by empty stack.PDA by empty stack to PDA by final state.

TheoremThere is an O(n3) algorithm that takes a PDA P whose repr3esentation naslength n and produces a DVG of length at most O(n3).

TheoremGiven a grammar G of length n, we can find an equivalent CNF grammar for G intime O(n2); the resulting grammar has length O(n2).

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 29 / 26

Page 30: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Testing Emptiness of CFL’s

LemmaTest whether the start symbol S of G isgenerating can be done in O(n) time.

Create an indexed list of all variables (left)Add links:

∀ variable a chain of all the positions in which that variable appears (full line).∀ production, a count of the number of positions holding variables whoseability to generate a terminal string has not yet been taken into account(dashed and dotted lines)Establishing B is generating follow full links and decrease the count by 1 foreach occurrence.If a count reaches 0, then we know the head variable is generating.Keep all generating variables on a stack and procceed them.

This algorithm is O(n).

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 30 / 26

Page 31: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Cocke-Younger-Kasami algorithm for testing a membershipin a CFL

Inefficient: exponential in |w | (check all derivation trees of appropriage depth ofCNF grammar for L.

Definition (CYG Algorithm, time O(n3))The input is CNF grammarG = (V ,T ,P,S) for language L and astring w = q1a2 . . . an ∈ T ∗.Produce triangular table (right),

horizontal axis is wXij is the set of variables A such thatA ∗⇒ ai ai+1 . . . aj .

Fill the table upwards.

Xij = {A→ BC ;B ∈ Xik ,C ∈ Xk+1,j}

X15

X14 X25

X13 X24 X35

X12 X23 X34 X45

X11 X22 X33 X44 X55

a1 a2 a3 a4 a5

BASIS:Xii = {A;A→ ai ∈ P}

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 31 / 26

Page 32: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

{S,A,C}- {S,A,C}- {B} {B}

{S,A} {B} {S,C} {S,A}{B} {A,C} {A,C} {B} {A,C}b a a b a

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 32 / 26

Page 33: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Undecidable CFL Problems (preview)

Is a given CFG ambiguous?Is a given CFL inherently ambiguous?Is the intersection of two CFL’s empty?Is a given CFL equal to Σ∗, where Σ is the alphabet of this language?

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 33 / 26

Page 34: Properties of context-Free Languagesktiml.mff.cuni.cz/~marta/automata8a.pdf · Properties of context-Free Languages WesimplifyCFL’s. Greibach Normal Form Chomsky Normal Form WeprovepumpinglemmaforCFL’s

Summary of Chapter 7

Eliminating Useless SymbolsEliminating λ and Unit– productionsChomsky Normal Form: no useless symbols, evry production body consistsof either two variable or one terminal.∀G ∈ CFG ; L(G) 6= ∅∃NFG : L(NFG) = L(G) \ {λ}.The Pumping Lemma∃n;∀z ∈ L, |z | ≥ n∃u, v ,w , x , y ; z = uvwxy suchthat: |vwx | ≤ n; vx 6= λ; ∀i ≥ 0, uv iwx iy ∈ L.Testing Empriness of a CFL possible also in O(|G |) time.Testing Membership in a CFL: Cocke-Younger-Kasami algorithmO(|w |3);w ∈ Σ∗ for a fixed CFL.

Automata and Grammars Normal Forms, Pumping Lemma 8 April 6, 2017 34 / 26