Context Free Languages

18
Context Free Languages Lecture 13 Naveen Zehra Quazilbash

description

Context Free Languages. Lecture 13 Naveen Zehra Quazilbash. Overview. Some elementary questions about languages Context-free Grammars Left-most and right-most derivations. Elementary questions about languages. Given a string w and language L, determine whether w ϵ L. - PowerPoint PPT Presentation

Transcript of Context Free Languages

Page 1: Context Free Languages

Context Free Languages

Lecture 13Naveen Zehra Quazilbash

Page 2: Context Free Languages

Overview

• Some elementary questions about languages• Context-free Grammars• Left-most and right-most derivations

Page 3: Context Free Languages

Elementary questions about languages

1. Given a string w and language L, determine whether w ϵ L.

2. Given L, determine whether it is finite.3. Given L1 and L2, determine whether they are

identical.

Page 4: Context Free Languages

1. Given a string w and language L, determine whether w ϵ L.

• If L is described by a DFA, check whether it accepts w.

• If it is described by an NFA, regular expression or regular language, then first construct a DFA.

Page 5: Context Free Languages

2. Given L, determine whether it is finite.

• Construct a DFA.• Find all states that belong to some cycle.• Determine if any of them is on a path from the

initial to final state.• A state “s” is on a path from the initial to the

final state if:– There is a path from the initial state to s.– There is a path from s to a final state.

Page 6: Context Free Languages

3. Given L1 and L2, determine whether they are identical.

• Consider the language:

• If L1=L2, then ?

• If L1≠L2, then ?• Explore!!!

)()( 21213 LLLLL

Page 7: Context Free Languages

Context-free Grammars• Example:

SABAaA | εBBb | ε

• Language: {anbm: n,m≥ 0}• Left side of each production is a variable;• No restriction on the right side.• Reason for the term “context-free”: replacement of a

variable does not depend on the surrounding symbols (“context”). E.g. ….B…., regardless of the symbols around B.

Page 8: Context Free Languages

Context-free Grammars (2)

• Note that every linear grammar is context-free; in particular, every regular grammar is context-free.

• A language is context-free if it can be described by a context-free grammar. E.g. (anbn: n≥ 0) is context-free:SaSb | ε

Page 9: Context Free Languages

• Examples:1. Language: {wwR : w ϵ {a,b}* }

S aSa | bSb | ε2. Language: {anbm: n≠m}

S A|BAaA|aC - anbm , n>mBBb|Cb - anbm , n<mCaCb| ε

Page 10: Context Free Languages

3. Lnaguage: {w : w ϵ {a,b}* and na(w)=nb(w)}; that is equal number of a’s and b’s.

SaSb|bSa|SS| ε

Page 11: Context Free Languages

Derivations• We may have different derivations for the same string.

Figure out why?• Example:

SDCDABAa|A| εBb|B| εCcC| ε

• Deriving abc:– S=>DC=>ABC=>AbC=>abC=>abc– S=>DC=>ABC=>ABc=>aBc=>abc

Language: {anbmck: n,m,k≥0}

Page 12: Context Free Languages

Derivations

• Leftmost derivation:– Always replace the leftmost variable first.– E.g. S=>DC=>ABC=>aBC=>abC=>abc

• Rightmost derivation:– Always replace the rightmost variable first.– E.g. S=>DC=>Dc=>ABc=>Abc=>abc

Page 13: Context Free Languages

Derivation Tree

• It shows the derivation of a string as a tree• The root is S (start symbol) .• Each non-leaf node is a variable.• Each leaf node is an alphabet symbol or ε.• Each non-leaf node is the left side of some

production and its children are the corresponding right side.

Page 14: Context Free Languages

Derivation Tree (2)

• A derivation tree is an alternative technique to express the derivation of a string.

• Each tree can be converted into a leftmost or rightmost derivation.

• Example:SABAAA|aBBB|b

Page 15: Context Free Languages

Derivation Tree (3)

• Example:SABAAA|aBBB|b

• Derive aaabb:a) Draw Derivation Treeb) Give leftmost derivationc) Give rightmost derivation

Page 16: Context Free Languages

Derivation Tree (4)

• Leftmost derivation:– S=>AB=>AAB=>AAAB=>aAAB=>aaAB=>aaaB=>aaa

BB=>aaabB=>aaabb.• Rightmost derivation:– S=>AB=>ABB=>ABb=>Abb=>AAbb=>Aabb=>AAabb=>Aaabb=>aaabb.

Page 17: Context Free Languages

Derivation Tree (5)

• Sometimes, there are different derivation trees for the same string.

• E.g. for aaab.• E.g. for

SA|aBAaaBaDeriving aa.

• Similarly, different leftmost or rightmost derivations may give the same string.

Page 18: Context Free Languages

ANNOUNCEMENT

QUIZ in next class!!