context-free languages chapter 2. ambiguity
TRANSCRIPT
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}}
1
2
a, ɛ ab, ɛ ba, b ɛb, a ɛ
ɛ, b ɛ
{w | w has exactly one more b than a’s where Σ = {a, b}}
1
2
a, ɛ ab, ɛ ba, 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?
{w | w has exactly one more b than a’s where Σ = {a, b}}
1
2
a, ɛ ab, ɛ ba, b ɛb, a ɛ
ɛ, b ɛ
Dilemma 2: What if my input string is bbb?
1
3
a, ɛ ab, ɛ ba, b ɛb, a ɛ
ɛ, b ɛ
Dilemma 2: What if my input string is bbb?
2
0
ɛ, $ ɛ
ɛ, ɛ $
{w | w has exactly one more b than a’s where Σ = {a, b}}
Possible Answer 1:S Sab | aSb | abS | Sba | bSa | baS | bHow 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 = uvxyz1. For each i >= 0,
si = uvixyiz A2. |vy| > 03. |vxy| <= p
Assume A is regularPick string s in A• s As has to be big enough to reach a loop in the PDA• |s| >= pp is the number of PDA transitions needed to visit the same state twice, i.e., take a loop
Consider this CFLA = {w | w has exactly one more b than a’s where Σ = {a, b}}
s = ap bp+1
s is clearly in AConsider 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. si = uvixyiz A2. |vy| > 03. |vxy| <= p
Adversary winss = ap bp+1
p = 2, s.t., s = aabbbThe adversary can choose:u = a, v = a, x = ɛ, y = b, z = bb2. vy = ab, i.e, |vy| > 03. 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.
1. si = uvixyiz A2. |vy| > 03. |vxy| <= p
Adversary winss = ap bp+1
p = 2, s.t., s = aabbbThe adversary can choose:u = a, v = a, x = ɛ, y = b, z = bb1. 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.
1. si = uvixyiz A2. |vy| > 03. |vxy| <= p
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!