380 likes | 529 Views
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.
E N D
Context-free Languages Chapter 2
Chomsky normal form Study Example 2.10 in the book
{w | w has exactly one more b than a’s where Σ = {a, b}} a, ɛ a b, ɛ b a, b ɛ b, a ɛ 1 ɛ, b ɛ 2
{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?
{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?
Dilemma 2: What if my input string is bbb? a, ɛ a b, ɛ b ɛ, ɛ $ a, b ɛ b, a ɛ 1 2 0 ɛ, b ɛ 3 ɛ, $ ɛ
{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?
{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?
{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 */
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
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
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= uvixyizA for all i Every time we pump up v=a, we also pump up y=b.
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= uvixyizA 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.
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
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
Non-Determinism in PDAs • Consider A = {wwR| w = {a, b}* } B = {wcwR| w = {a, b}* }
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.
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.
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 ˧
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).
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.
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!