Positive Properties Context-Free languages

11
1 CS 301 - Lecture 18 Properties of Context Free Grammars Fall 2008 Review Languages and Grammars Alphabets, strings, languages Regular Languages Deterministic Finite and Nondeterministic Automata Equivalence of NFA and DFA Regular Expressions Regular Grammars Properties of Regular Languages Languages that are not regular and the pumping lemma Context Free Languages Context Free Grammars Derivations: leftmost, rightmost and derivation trees Parsing and ambiguity Simplifications and Normal Forms Nondeterministic Pushdown Automata Pushdown Automata and Context Free Grammars Deterministic Pushdown Automata Pumping Lemma for Context Free Grammars Today: Closure and Decidable Properties for Context Free Languages Positive Properties of Context-Free languages Context-free languages are closed under: Union 1 L is context free 2 L is context free 2 1 L L is context-free Union

Transcript of Positive Properties Context-Free languages

Page 1: Positive Properties Context-Free languages

1

CS 301 - Lecture 18 Properties of Context Free

Grammars Fall 2008

Review •  Languages and Grammars

–  Alphabets, strings, languages •  Regular Languages

–  Deterministic Finite and Nondeterministic Automata –  Equivalence of NFA and DFA –  Regular Expressions –  Regular Grammars –  Properties of Regular Languages –  Languages that are not regular and the pumping lemma

•  Context Free Languages –  Context Free Grammars –  Derivations: leftmost, rightmost and derivation trees –  Parsing and ambiguity –  Simplifications and Normal Forms –  Nondeterministic Pushdown Automata –  Pushdown Automata and Context Free Grammars –  Deterministic Pushdown Automata –  Pumping Lemma for Context Free Grammars

•  Today: –  Closure and Decidable Properties for Context Free Languages

Positive Properties of

Context-Free languages

Context-free languages are closed under: Union

1L is context free

2L is context free 21 LL ∪

is context-free

Union

Page 2: Positive Properties Context-Free languages

2

Example

λ|11 baSS →

λ|| 222 bbSaaSS →

Union

}{1nnbaL =

}{2RwwL =

21 | SSS→}{}{ Rnn wwbaL ∪=

Language Grammar

In general:

The grammar of the union has new start variable and additional production 21 | SSS→

For context-free languages with context-free grammars and start variables

21, LL

21, GG21, SS

21 LL ∪S

Context-free languages are closed under: Concatenation

1L is context free

2L is context free 21LL

is context-free

Concatenation Example

λ|11 baSS →

λ|| 222 bbSaaSS →

Concatenation

}{1nnbaL =

}{2RwwL =

21SSS→}}{{ Rnn wwbaL =

Language Grammar

Page 3: Positive Properties Context-Free languages

3

In general:

The grammar of the concatenation has new start variable and additional production 21SSS→

For context-free languages with context-free grammars and start variables

21, LL

21, GG21, SS

21LLS

Context-free languages are closed under: Star-operation

L is context free *L is context-free

Star Operation

λ|aSbS→}{ nnbaL =

λ|11 SSS →*}{ nnbaL =

Example

Language Grammar

Star Operation

In general:

The grammar of the star operation has new start variable and additional production

For context-free language with context-free grammar and start variable

LGS

*L1S

λ|11 SSS →

Page 4: Positive Properties Context-Free languages

4

Negative Properties of

Context-Free Languages

Context-free languages are not closed under: intersection

1L is context free

2L is context free

21 LL ∩

not necessarily context-free

Intersection

Example

}{1mnn cbaL =

λ

λ

||

cCCaAbAACS

Context-free:

}{2mmn cbaL =

λ

λ

||

bBcBaAAABS

→Context-free:

}{21nnn cbaLL =∩ NOT context-free

Intersection

Context-free languages are not closed under: complement

L is context free L not necessarily context-free

Complement

Page 5: Positive Properties Context-Free languages

5

}{2121nnn cbaLLLL =∩=∪

NOT context-free

Example

}{1mnn cbaL =

λ

λ

||

cCCaAbAACS

Context-free:

}{2mmn cbaL =

λ

λ

||

bBcBaAAABS

→Context-free:

Complement

Intersection of

Context-free languages and

Regular Languages

The intersection of a context-free language and a regular language is a context-free language

1L context free

2L regular 21 LL ∩

context-free

1Lfor for 2LNPDA

1MDFA

2M

Construct a new NPDA machine that accepts

Machine Machine

M21 LL ∩

context-free regular

M simulates in parallel and 1M 2M

Page 6: Positive Properties Context-Free languages

6

1M 2M

1q 2qcba →,

transition 1p 2p

a

transition

NPDA DFA

11, pq cba →,

transition

MNPDA

22, pq

1M 2M

1q 2qcb→,λ

transition 1p

NPDA DFA

11, pq cb→,λ

transition

MNPDA

12, pq

1M 2M

0q

initial state 0p

initial state

NPDA DFA

Initial state

MNPDA

00, pq

1M 2M

1q

final state

1p

final states

NPDA DFA

final states

MNPDA

11, pq

2p

21, pq

Page 7: Positive Properties Context-Free languages

7

Example:

λλλ →, λλλ →, λλλ →,

1, →λb1, →λa

λ→1,dλ→1,c

0q 1q 2q 3q

1M

}},{,},{|,|||:{ *2

*121211 dcwbawwwwwL ∈∈==

NPDA

context-free

0p

2M

*2 },{ caL =

ca,

DFA

regular

Automaton for: }0:{21 ≥=∩ ncaLL nn

λλλ →, λλλ →, λλλ →,

1, →λa λ→1,c

00, pq 01, pq 02, pq 03, pq

NPDA M

context-free

M simulates in parallel and 1M 2M

M accepts string w if and only if

accepts string and w1Maccepts string w2M

)()()( 21 MLMLML ∩=

In General:

Page 8: Positive Properties Context-Free languages

8

Therefore:

)()( 21 MLML ∩

is NPDA M

is context-free

21 LL ∩ is context-free

Applications of

Regular Closure

The intersection of a context-free language and a regular language is a context-free language

1L context free

2L regular 21 LL ∩

context-free

Regular Closure

An Application of Regular Closure

Prove that: }0,100:{ ≥≠= nnbaL nn

is context-free

Page 9: Positive Properties Context-Free languages

9

}0:{ ≥nba nn

We know:

is context-free }{ 1001001 baL = is regular

}{}){( 100100*1 babaL −+= is regular

We also know:

}{}){( 100100*1 babaL −+=

regular }{ nnba

context-free

1}{ Lba nn ∩ context-free

LnnbaLba nnnn =≥≠=∩ }0,100:{}{ 1is context-free

(regular closure)

Another Application of Regular Closure

Prove that: }:{ cba nnnwL ===

is not context-free

Page 10: Positive Properties Context-Free languages

10

}:{ cba nnnwL ===

}{*}**{ nnn cbacbaL =∩

context-free regular context-free

If is context-free

Then

Impossible!!!

Therefore, is not context free L

(regular closure) Decidable Properties

of Context-Free Languages

Membership Question:

for context-free grammar find if string

G)(GLw∈

Membership Algorithms: Parsers

•  Exhaustive search parser

•  CYK parsing algorithm

Empty Language Question:

for context-free grammar find if

G∅=)(GL

Algorithm:

S

1.  Remove useless variables

2. Check if start variable is useless

Page 11: Positive Properties Context-Free languages

11

Infinite Language Question: for context-free grammar find if is infinite

G)(GL

Algorithm:

1. Remove useless variables

2. Remove unit and productions

3. Create dependency graph for variables

4. If there is a loop in the dependency graph then the language is infinite

λ

Example:

cBSCbbbBBaaCbA

ABS

||

S

A

B

C

Dependency graph Infinite language

cBSCbbbBBaaCbA

ABS

||

acbbSbbbacBSbBaCbBABS ⇒⇒⇒⇒

ii bbbSacbb

bbbSacbbacbbSbbbS

)()(

)()( 22

∗∗

⇒⇒

What’s Next •  Read

–  Linz Chapter 1,2.1, 2.2, 2.3, (skip 2.4), 3, 4, 5, 6.1, 6.2, (skip 6.3), 7.1, 7.2, 7.3, (skip 7.4), 8, and 9.1

–  JFLAP Chapter 1, 2.1, (skip 2.2), 3, 4, 5, 6, 7, (skip 8), 9.1, 9.2 •  Next Lecture Topics From 9.1

–  Turing Machines •  Quiz 3 in Recitation on Wednesday 11/12

–  Covers Linz 7.1, 7.2, 7.3, (skip 7.4), 8, and JFLAP 5,6,7 –  Closed book, but you may bring one sheet of 8.5 x 11 inch paper with any notes you

like. –  Quiz will take the full hour

•  Homework –  Homework Due Today –  New Homework Available Friday –  New Homework Due Next Thursday