1 / 18

Context Free Languages

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.

zeheb
Download Presentation

Context Free Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Context Free Languages Lecture 13 NaveenZehraQuazilbash

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

  3. 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.

  4. 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.

  5. 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.

  6. 3. Given L1 and L2, determine whether they are identical. • Consider the language: • If L1=L2, then ? • If L1≠L2, then ? • Explore!!!

  7. Context-free Grammars • Example: SAB AaA | ε BBb | ε • 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.

  8. 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: SaSb | ε

  9. Examples: • Language: {wwR: w ϵ {a,b}* } S aSa | bSb | ε • Language: {anbm: n≠m} S A|B AaA|aC - anbm, n>m BBb|Cb - anbm, n<m CaCb| ε

  10. Lnaguage: {w : w ϵ {a,b}* and na(w)=nb(w)}; that is equal number of a’s and b’s. SaSb|bSa|SS| ε

  11. Derivations • We may have different derivations for the same string. Figure out why? • Example: SDC DAB Aa|A| ε Bb|B| ε CcC| ε • Deriving abc: • S=>DC=>ABC=>AbC=>abC=>abc • S=>DC=>ABC=>ABc=>aBc=>abc Language: {anbmck: n,m,k≥0}

  12. 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

  13. 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.

  14. 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: SAB AAA|a BBB|b

  15. Derivation Tree (3) • Example: SAB AAA|a BBB|b • Derive aaabb: • Draw Derivation Tree • Give leftmost derivation • Give rightmost derivation

  16. 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.

  17. Derivation Tree (5) • Sometimes, there are different derivation trees for the same string. • E.g. for aaab. • E.g. for SA|aB Aaa Ba Deriving aa. • Similarly, different leftmost or rightmost derivations may give the same string.

  18. ANNOUNCEMENT QUIZ in next class!!

More Related