Download presentation
Presentation is loading. Please wait.
1
Context-free Languages Chapter 2
6
Ambiguity
7
Chomsky normal form Study Example 2.10 in the book
13
Pushdown Automata
14
PDA Example
17
What language is this?
18
{w | w has exactly one more b than a’s where Σ = {a, b}} 1 2 a, ɛ a b, ɛ b a, b ɛ b, a ɛ ɛ, b ɛ
19
{w | w has exactly one more b than a’s where Σ = {a, b}} 1 2 a, ɛ a b, ɛ b a, b ɛ b, a ɛ ɛ, b ɛ 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?
20
{w | w has exactly one more b than a’s where Σ = {a, b}} 1 2 a, ɛ a b, ɛ b a, b ɛ b, a ɛ ɛ, b ɛ Dilemma 2: What if my input string is bbb?
21
1 3 a, ɛ a b, ɛ b a, b ɛ b, a ɛ ɛ, b ɛ Dilemma 2: What if my input string is bbb? 20 ɛ, $ ɛ ɛ, ɛ $
22
{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?
23
{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?
24
{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 */
25
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 1.For each i >= 0, s i = uv i xy i z A 2.|vy| > 0 3.|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
26
Consider this CFL A = {w | w has exactly one more b than a’s where Σ = {a, b}} s = a p b p+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 1.s i = uv i xy i z A 2.|vy| > 0 3.|vxy| <= p
27
Adversary wins s = a p b p+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. s i = uv i xy i z A for all i Every time we pump up v=a, we also pump up y=b. 1.s i = uv i xy i z A 2.|vy| > 0 3.|vxy| <= p
28
Adversary wins s = a p b p+1 p = 2, s.t., s = aabbb The adversary can choose: u = a, v = a, x = ɛ, y = b, z = bb 1. s i = uv i xy i z 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. 1.s i = uv i xy i z A 2.|vy| > 0 3.|vxy| <= p
29
Consider a language that might actually be Context Free B = {ww | w in {0,1}*} Consider s = 0 p 1 0 p 1
30
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
31
Non-Determinism in PDAs Consider A = {ww R | w = {a, b}* } B = {wcw R | w = {a, b}* }
32
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.
33
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.
34
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 ˧
35
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).
36
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.
37
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. {ww R } requires O(n 2 ). Because you have to guess where to start popping at n possible locations. Implementing some non-deterministic PDAs require O(2 n ) or O(n!) LR(k) grammars yield non-deterministic PDAs that require n*k = O(n) computation. Cool!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.