Positive Properties are closed under: Union of Context-Free - - PDF document

positive properties
SMART_READER_LITE
LIVE PREVIEW

Positive Properties are closed under: Union of Context-Free - - PDF document

Review Languages and Grammars Alphabets, strings, languages Regular Languages CS 301 - Lecture 18 Deterministic Finite and Nondeterministic Automata Equivalence of NFA and DFA Regular Expressions Properties of Context


slide-1
SLIDE 1

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

  • f

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

slide-2
SLIDE 2

2

Example

λ |

1 1

b aS S → λ | |

2 2 2

b bS a aS S →

Union

} {

1 n nb

a L = } {

2 R

ww L =

2 1 | S

S S → } { } {

R n n

ww b a L ∪ =

Language Grammar In general: The grammar of the union has new start variable and additional production 2 1 | S

S S →

For context-free languages with context-free grammars and start variables 2 1, L

L

2 1, G

G

2 1, S

S

2 1

L L ∪ S

Context-free languages are closed under: Concatenation 1

L

is context free 2

L

is context free 2 1L

L

is context-free

Concatenation

Example

λ |

1 1

b aS S → λ | |

2 2 2

b bS a aS S →

Concatenation

} {

1 n nb

a L = } {

2 R

ww L =

2 1S

S S → } }{ {

R n n

ww b a L =

Language Grammar

slide-3
SLIDE 3

3

In general: The grammar of the concatenation has new start variable and additional production 2 1S

S S →

For context-free languages with context-free grammars and start variables 2 1, L

L

2 1, G

G

2 1, S

S

2 1L

L S

Context-free languages are closed under: Star-operation

L is context free

*

L

is context-free

Star Operation

λ | aSb S → } {

n nb

a L = λ |

1 1

SS S → * } {

n nb

a L =

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

L G S * L

1

S λ |

1 1

SS S →

slide-4
SLIDE 4

4

Negative Properties

  • f

Context-Free Languages

Context-free languages are not closed under: intersection 1

L

is context free 2

L

is context free 2 1

L L ∩

not necessarily context-free

Intersection

Example

} {

1 m n n

c b a L = λ λ | | cC C aAb A AC S → → →

Context-free:

} {

2 m m n

c b a L = λ λ | | bBc B aA A AB S → → →

Context-free:

} {

2 1 n n n

c b a L L = ∩

NOT context-free Intersection Context-free languages are not closed under: complement

L

is context free

L

not necessarily context-free

Complement

slide-5
SLIDE 5

5 } {

2 1 2 1 n n n

c b a L L L L = ∩ = ∪

NOT context-free Example

} {

1 m n n

c b a L = λ λ | | cC C aAb A AC S → → →

Context-free:

} {

2 m m n

c b a L = λ λ | | bBc B aA A AB S → → →

Context-free: Complement

Intersection

  • f

Context-free languages and Regular Languages

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

L

context free 2

L

regular 2 1

L L ∩

context-free 1

L

for for 2

L

NPDA 1

M

DFA 2

M

Construct a new NPDA machine that accepts Machine Machine

M

2 1

L L ∩

context-free regular

M

simulates in parallel and 1

M

2

M

slide-6
SLIDE 6

6

1

M

2

M

1

q

2

q c b a → ,

transition

1

p

2

p a

transition NPDA DFA

1 1, p

q c b a → ,

transition

M

NPDA

2 2, p

q

1

M

2

M

1

q

2

q

c b → , λ transition

1

p

NPDA DFA

1 1, p

q

c b → , λ transition

M

NPDA

1 2, p

q 1

M

2

M

q

initial state

p

initial state NPDA DFA Initial state

M

NPDA

0, p

q

1

M

2

M

1

q

final state

1

p

final states NPDA DFA final states

M

NPDA

1 1, p

q

2

p

2 1, p

q

slide-7
SLIDE 7

7

Example: λ λ λ → , λ λ λ → , λ λ λ → , 1 , → λ b 1 , → λ a λ → 1 , d λ → 1 , c q

1

q

2

q

3

q

1

M

} } , { , } , { |, | | | : {

* 2 * 1 2 1 2 1 1

d c w b a w w w w w L ∈ ∈ = = NPDA

context-free

p

2

M

* 2

} , { c a L = c a, DFA

regular

Automaton for:

} : {

2 1

≥ = ∩ n c a L L

n n λ λ λ → , λ λ λ → , λ λ λ → , 1 , → λ a λ → 1 , c

0, p

q 1, p q

2, p

q

3, p

q

NPDA M

context-free

M

simulates in parallel and 1

M

2

M M accepts string w

if and only if accepts string and

w

1

M

accepts string

w

2

M ) ( ) ( ) (

2 1

M L M L M L ∩ =

In General:

slide-8
SLIDE 8

8

Therefore:

) ( ) (

2 1

M L M L ∩

is NPDA

M

is context-free 2 1

L L ∩

is context-free

Applications

  • f

Regular Closure

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

L

context free 2

L

regular 2 1

L L ∩

context-free Regular Closure

An Application of Regular Closure

Prove that:

} , 100 : { ≥ ≠ = n n b a L

n n is context-free

slide-9
SLIDE 9

9 } : { ≥ n b a

n n

We know: is context-free

} {

100 100 1

b a L =

is regular

} { } ) {(

100 100 * 1

b a b a L − + =

is regular We also know:

} { } ) {(

100 100 * 1

b a b a L − + =

regular

} {

n nb

a

context-free 1

} { L b a

n n

context-free

L n n b a L b a

n n n n

= ≥ ≠ = ∩ } , 100 : { } {

1 is context-free

(regular closure)

Another Application of Regular Closure

Prove that:

} : {

c b a

n n n w L = = =

is not context-free

slide-10
SLIDE 10

10 } : {

c b a

n n n w L = = = } { *} * * {

n n n

c b a c b a L = ∩

context-free regular context-free If is context-free Then

Impossible!!!

Therefore, is not context free

L

(regular closure)

Decidable Properties

  • f

Context-Free Languages

Membership Question: for context-free grammar find if string

G ) (G L w∈

Membership Algorithms: Parsers

  • Exhaustive search parser
  • CYK parsing algorithm

Empty Language Question: for context-free grammar find if

G ∅ = ) (G L

Algorithm:

S

  • 1. Remove useless variables
  • 2. Check if start variable is useless
slide-11
SLIDE 11

11

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

G ) (G L

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:

cBS C bb bB B a aCb A AB S → → → → | |

S A B C

Dependency graph Infinite language

cBS C bb bB B a aCb A AB S → → → → | | acbbSbbb acBSbB aCbB AB S ⇒ ⇒ ⇒ ⇒

i i

bbb S acbb bbb S acbb acbbSbbb S ) ( ) ( ) ( ) (

2 2 ∗ ∗ ∗

⇒ ⇒ ⇒

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