180 likes | 304 Views
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.
E N D
Context Free Languages Lecture 13 NaveenZehraQuazilbash
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. • Given L, determine whether it is finite. • 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!!!
Context-free Grammars • Example: SAB AaA | ε 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: • Language: {wwR: w ϵ {a,b}* } S aSa | bSb | ε • Language: {anbm: n≠m} S A|B AaA|aC - anbm, n>m BBb|Cb - anbm, n<m CaCb| ε
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: SDC DAB Aa|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: SAB AAA|a BBB|b
Derivation Tree (3) • Example: SAB AAA|a BBB|b • Derive aaabb: • Draw Derivation Tree • Give leftmost derivation • Give rightmost derivation
Derivation Tree (4) • Leftmost derivation: • S=>AB=>AAB=>AAAB=>aAAB=>aaAB=>aaaB=>aaaBB=>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|aB Aaa Ba Deriving aa. • Similarly, different leftmost or rightmost derivations may give the same string.
ANNOUNCEMENT QUIZ in next class!!