Context Free Languages
description
Transcript of Context Free Languages
Context Free Languages
Lecture 13Naveen Zehra Quazilbash
Overview
• Some elementary questions about languages• Context-free Grammars• Left-most and right-most derivations
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.
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.
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.
3. Given L1 and L2, determine whether they are identical.
• Consider the language:
• If L1=L2, then ?
• If L1≠L2, then ?• Explore!!!
)()( 21213 LLLLL
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.
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 | ε
• 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| ε
3. Lnaguage: {w : w ϵ {a,b}* and na(w)=nb(w)}; that is equal number of a’s and b’s.
SaSb|bSa|SS| ε
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}
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
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.
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
Derivation Tree (3)
• Example:SABAAA|aBBB|b
• Derive aaabb:a) Draw Derivation Treeb) Give leftmost derivationc) Give rightmost derivation
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.
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.
ANNOUNCEMENT
QUIZ in next class!!