1 / 37

Context-free Languages

Context-free Languages. Chapter 2. Ambiguity. Chomsky normal form. Study Example 2.10 in the book. Pushdown Automata. PDA Example. PDA Example. What language is this?. {w | w has exactly one more b than a’s where Σ = {a, b}}. a, ɛ  a. b, ɛ  b. a, b  ɛ. b, a  ɛ. 1.

crescent
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 Chapter 2

  2. Ambiguity

  3. Chomsky normal form Study Example 2.10 in the book

  4. Pushdown Automata

  5. PDA Example

  6. PDA Example

  7. What language is this?

  8. {w | w has exactly one more b than a’s where Σ = {a, b}} a, ɛ  a b, ɛ  b a, b  ɛ b, a  ɛ 1 ɛ, b  ɛ 2

  9. {w | w has exactly one more b than a’s where Σ = {a, b}} a, ɛ  a b, ɛ  b a, b  ɛ b, a  ɛ 1 ɛ, b  ɛ 2 Dilemma 1: I am in State 1; The current input symbol is b and there is a b on the stack. What do you do?

  10. {w | w has exactly one more b than a’s where Σ = {a, b}} a, ɛ  a b, ɛ  b a, b  ɛ b, a  ɛ 1 ɛ, b  ɛ 2 Dilemma 2: What if my input string is bbb?

  11. Dilemma 2: What if my input string is bbb? a, ɛ  a b, ɛ  b ɛ, ɛ  $ a, b  ɛ b, a  ɛ 1 2 0 ɛ, b  ɛ 3 ɛ, $  ɛ

  12. {w | w has exactly one more b than a’s where Σ = {a, b}} Possible Answer 1: S Sab | aSb | abS | Sba | bSa | baS | b How do you create bbaaabb?

  13. {w | w has exactly one more b than a’s where Σ = {a, b}} Possible Answer 2: S SaSbS | SbSaS | b What is the problem with this?

  14. {w | w has exactly one more b than a’s where Σ = {a, b}} Possible Answer 3: S XbX | XbX // one b surrounded by X/* X can be empty or it must contain an equal number of a’s and b’s */ X  XaXbX | XbXaX | ɛ /* We need ab and ba with X’s nested anywhere inbetween */

  15. Pumping Lemma for CFL • Can prove that a language A is NOT context free, i.e., no PDA can accept and no CFG can generate. Pumping Lemma says: s = uvxyz • For each i >= 0, si = uvixyiz A • |vy| > 0 • |vxy| <= p Assume A is regular Pick string s in A • s A s has to be big enough to reach a loop in the PDA • |s| >= p p is the number of PDA transitions needed to visit the same state twice, i.e., take a loop

  16. Consider this CFL A = {w | w has exactly one more b than a’s where Σ= {a, b}} s = apbp+1 s is clearly in A Consider p = 2, s.t., s = aabbb, which we know is a reasonably small p. The adversary can choose: u = a, v = a, x = ɛ, y = b, z = bb si= uvixyiz A |vy| > 0 |vxy| <= p

  17. Adversary wins si= uvixyiz A |vy| > 0 |vxy| <= p s = apbp+1 p = 2, s.t., s = aabbb The adversary can choose: u = a, v = a, x = ɛ, y = b, z = bb 2. vy = ab, i.e, |vy| > 0 3. vxy = ab, i.e., |vxy| >= 2 1. si= uvixyizA for all i Every time we pump up v=a, we also pump up y=b.

  18. Adversary wins si= uvixyiz A |vy| > 0 |vxy| <= p s = apbp+1 p = 2, s.t., s = aabbb The adversary can choose: u = a, v = a, x = ɛ, y = b, z = bb 1. si= uvixyizA for all i Every time we pump up v=a, we also pump up y=b. This proves nothing because perhaps we could have picked a better s where the adversary would not win.

  19. Consider a language that might actually be Context Free • B = {ww | w in {0,1}*} • Consider s = 0p 1 0p 1 • Consider s = 0p 1 0p 1

  20. Non-Determinism in PDAs • All non-deterministic Finite State Automata’s can be transformed into deterministic ones (see proof of Theorem 1.39 on p55) • Many non-deterministic Pushdown Automata’s CANNOT be transformed into deterministic ones (see pp 130-135) • Non-determinism is necessary to recognize many Context-free Languages

  21. Non-Determinism in PDAs • Consider A = {wwR| w = {a, b}* } B = {wcwR| w = {a, b}* }

  22. Non-Deterministic Grammars • R  S | T • S  aSb | ab • T  aTbb | abb • Consider the string aabb and aabbbb • There is no way to know which rules were used unless we analyze the whole string. • This indicated non-determinism.

  23. DK-Test • Essentially, a more algorithm for determining if a grammar is non-deterministic. • We will not be covering this as it requires two weeks of explanation.

  24. Is this non-deterministic • S  E ˧ • E  E + T | T • T  T x a | a • Consider the string a + a x a + a ˧ • Consider the string a x a + a x a ˧

  25. LR(1) Grammars • S  E ˧ • E  E + T | T • T  T x a | a • Consider the string a + a x a + a ˧ • By scanning left to right (LR) and by looking ahead one symbol (1), we can determine which rule was used. (Called a forced handle).

  26. LR(k) Grammars • Grammars where you can determine which rules were used by scanning a string left to right and look ahead a constant (K) number of symbols • LR(2) would require looking ahead two symbols. • Almost all programming languages can be defined and generated using LR(k) Grammars.

  27. Big Picture • Deterministic PDAs require O(n) computation because for each symbol you deterministically move to another state and perhaps push or pop the stack. • Implementing Non-deterministic PDAs requires a lot more computation. {wwR} requires O(n2). Because you have to guess where to start popping at n possible locations. • Implementing some non-deterministic PDAs require O(2n) or O(n!) • LR(k) grammars yield non-deterministic PDAs that require n*k = O(n) computation. Cool!

More Related