context-free languages chapter 2. ambiguity

37
Context-free Languages Chapter 2

Upload: sybil-ellis

Post on 02-Jan-2016

235 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Context-free Languages Chapter 2. Ambiguity

Context-free Languages

Chapter 2

Page 2: Context-free Languages Chapter 2. Ambiguity
Page 3: Context-free Languages Chapter 2. Ambiguity
Page 4: Context-free Languages Chapter 2. Ambiguity
Page 5: Context-free Languages Chapter 2. Ambiguity
Page 6: Context-free Languages Chapter 2. Ambiguity

Ambiguity

Page 7: Context-free Languages Chapter 2. Ambiguity

Chomsky normal form

Study Example 2.10 in the book

Page 8: Context-free Languages Chapter 2. Ambiguity
Page 9: Context-free Languages Chapter 2. Ambiguity
Page 10: Context-free Languages Chapter 2. Ambiguity
Page 11: Context-free Languages Chapter 2. Ambiguity
Page 12: Context-free Languages Chapter 2. Ambiguity
Page 13: Context-free Languages Chapter 2. Ambiguity

Pushdown Automata

Page 14: Context-free Languages Chapter 2. Ambiguity

PDA Example

Page 15: Context-free Languages Chapter 2. Ambiguity

PDA Example

Page 16: Context-free Languages Chapter 2. Ambiguity
Page 17: Context-free Languages Chapter 2. Ambiguity

What language is this?

Page 18: Context-free Languages Chapter 2. Ambiguity

{w | w has exactly one more b than a’s where Σ = {a, b}}

1

2

a, ɛ ab, ɛ ba, b ɛb, a ɛ

ɛ, b ɛ

Page 19: Context-free Languages Chapter 2. Ambiguity

{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?

Page 20: Context-free Languages Chapter 2. Ambiguity

{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?

Page 21: Context-free Languages Chapter 2. Ambiguity

1

3

a, ɛ ab, ɛ ba, b ɛb, a ɛ

ɛ, b ɛ

Dilemma 2: What if my input string is bbb?

2

0

ɛ, $ ɛ

ɛ, ɛ $

Page 22: Context-free Languages Chapter 2. Ambiguity

{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?

Page 23: Context-free Languages Chapter 2. Ambiguity

{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?

Page 24: Context-free Languages Chapter 2. Ambiguity

{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 */

Page 25: Context-free Languages Chapter 2. Ambiguity

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

Page 26: Context-free Languages Chapter 2. Ambiguity

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

Page 27: Context-free Languages Chapter 2. Ambiguity

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

Page 28: Context-free Languages Chapter 2. Ambiguity

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

Page 29: Context-free Languages Chapter 2. Ambiguity

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

Page 30: Context-free Languages Chapter 2. Ambiguity

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

Page 31: Context-free Languages Chapter 2. Ambiguity

Non-Determinism in PDAs

• Consider A = {wwR | w = {a, b}* } B = {wcwR| w = {a, b}* }

Page 32: Context-free Languages Chapter 2. Ambiguity

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.

Page 33: Context-free Languages Chapter 2. Ambiguity

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.

Page 34: Context-free Languages Chapter 2. Ambiguity

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 ˧

Page 35: Context-free Languages Chapter 2. Ambiguity

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

Page 36: Context-free Languages Chapter 2. Ambiguity

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.

Page 37: Context-free Languages Chapter 2. Ambiguity

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!