Presentation is loading. Please wait.

Presentation is loading. Please wait.

INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.

Similar presentations


Presentation on theme: "INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011."— Presentation transcript:

1 INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011

2 ε,ε → $ 0,ε → 0 1,0 → ε ε,$ → ε stringpoppush

3 A → 0A1 A → B B → # CONTEXT-FREE GRAMMARS A  0A1  00A11  00B11  00#11 A derives 00#11 in 4 steps. uVw yields uvw if (V → v) ∈ R.

4 PARSE TREES A  0A1  00A11  00B11  00#11 A B 01# A A 01

5 Definition. T is a parse tree for the derivation S ⇒ * w under grammar G = (V,Σ,R,S) if 1. The root of T has label S 2. The leaves of T have labels w i ∈ Σ 4. For each node with label v ∈ V and children with labels r i ∈ (V ∪ Σ), (v→r) ∈ R. 3. The non-leaf nodes have labels v ∈ V

6 → + → x → ( ) → a Build a parse tree for a + a x a ax+a a a+xa a

7 Definition: a string is derived ambiguously in a context-free grammar if it has two or more different parse trees Definition: a grammar is ambiguous if it generates some string ambiguously

8 〈 stmt 〉 → 〈 assign 〉 | 〈 if-then 〉 | 〈 if-then-else 〉 〈 if-then 〉 → if 〈 condition 〉 then 〈 stmt 〉 〈 if-then-else 〉 → if 〈 condition 〉 then 〈 stmt 〉 else 〈 stmt 〉 〈 assign 〉 → a = 1 | a = 2 if a==1 then if a==2 then a = 1 else a = 2 〈 condition 〉 → a==1 | a==2

9 THE CHOMSKY NORMAL FORM A context-free grammar is in Chomsky normal form if every rule is of the form: A → BC A → a S → ε B and C are not start variable a is a terminal S is the start variable S → aSb S → ε A → a T → SB S → AT | AB R → AT | AB | ε B → b

10 Theorem 2.9: Any context-free language can be generated by a context-free grammar in Chomsky normal form “We can transform any CFG into Chomsky normal form”

11 1.Add a new start variable S 0 and add the rule S 0 → S S → aSb R → aRb R → ε S 0 → S 2. Remove all A → ε rules (where A is not S 0 ) For each occurrence of A on right hand side of a rule, add a new rule with the occurrence deleted If we have the rule B → A, add B → ε, unless we have previously removed B → ε S → R 3. Remove unit rules A → B Whenever B → w appears, add the rule A → w unless this was a unit rule previously removed R → ab S → ε S → ab S 0 → ε S → aRb S 0 → aSb S 0 → ab S 0 → aRb

12 4. Convert all remaining rules into the proper form S 0 → aRb S 0 → A 1 A 2 A 1 → a A 2 → RA 3 A 3 → b S → aSb R → aRb R → ab S → ab S 0 → ε S → aRb S 0 → aSb S 0 → ab S 0 → aRb S 0 → ab S 0 → A 1 A 3 S 0 → aSb S 0 → A 1 A 4 A 4 → SA 3 S → aRb S → A 1 A 2 S → ab S → A 1 A 3 R → ab R → A 1 A 3 R → aRb R → A 1 A 2 S → aSb S → A 1 A 4

13 Convert the following into Chomsky normal form: A → BAB | B | ε B → 00 | ε S  SaSb | ε 1.Add a new start variable S 0 and rule S 0 → S 2. Remove all A → ε rules 3. Remove unit rules A → B 4. Convert all remaining rules into the proper form

14 THE CHOMSKY NORMAL FORM A context-free grammar is in Chomsky normal form if every rule is of the form: A → BC A → a S → ε B and C are not start variable a is a terminal S is the start variable Any variable A that is not the start variable can only generate strings of length > 0

15 Theorem: If G is in CNF, w  L(G) and |w| > 0, then any derivation of w in G has length ≤ 2|w| - 1 Proof (by induction on |w|): Base Case: If |w| = 1, then any derivation of w must have length 1 Inductive Step: Assume true for any string of length at most k ≥ 1, and let |w| = k+1 Since |w| > 1, derivation starts with S → AB So w = xy where A  x, |x| > 0 and B  y, |y| > 0 By the inductive hypothesis, the length of any derivation of w must be at most 1 + (2|x| - 1) + (2|y| - 1) = 2(|x| + |y|) - 1

16 CONTEXT FREE OR NOT? L ₁ = { xy | x,y ∈ {0,1}* and x=y} L ₂ = {xy | x,y ∈ {0,1}*, |x|=|y| and x ≠ y} NOT YES

17 THE CONTEXT-FREE PUMPING LEMMA Let L be a context-free language Then there exists P such that For every w  L with |w| ≥ P 1. |vy| > 0 there exist uvxyz=w, where: 3. For every i ≥ 0, uv i xy i z  L 2. |vxy| ≤ P

18 EXAMPLES 1.|vy| > 0 2.|vxy| ≤ P 3.uv i xy i z  L for any i ≥ 0 there exist uvxyz=w, where: Example: L = { w ∈ {0,1}* | w = w R }. w = 0; u,v,x,y,z = ? w = 010; u,v,x,y,z=? Example: L = { w ∈ {a,b}* | #a > #b in w}. w = a; u,v,x,y,z = ? w = aab; u,v,x,y,z=?

19 Idea: If w is long enough, then any parse tree for w must have a path that contains a variable more than once T R R uvxzy T uz R R v y R R vxy

20 If the height of a parse tree is h, the length of the string generated is at most: Formal Proof: Let b be the maximum number of symbols on the right-hand side of a rule bhbh Let |V| be the number of variables in G Define P = b |V|+2 Let w be a string of length at least P Let T be the parse tree for w with the smallest number of nodes. T must have height at least |V|+2

21 Let T be the parse tree for w with the smallest number of nodes. T must have height at least |V|+2 The longest path in T must have ≥ |V|+1 variables Select R to be the variable that repeats among the lowest |V|+1 variables T R R uvxzy T uz R R v y R R vxy 1. |vy| > 0 2. |vxy| ≤ P

22 NEGATING THE PUMPING LEMMA Let L be a context-free language. Then there exists P such that For every w  L with |w| ≥ P 1. |vy| > 0 there exist uvxyz=w, where: 3. For every i ≥ 0, uv i xy i z  L 2. |vxy| ≤ P For every P, There is a For every There is an If  L is not context-free

23 USING THE PUMPING LEMMA Prove L = {ww | w ∈ {0,1}*} is not context-free Assume L is context-free. Then there is a pumping length P. No matter what P is, the string s = 0 P 1 P 0 P 1 P has |s| ≥ P and s ∈ L. So there should be uvxyz=s with: 1) |vy| > 0, 2) |vxy| ≤ P, 3) ∀ i, uv i xy i z ∈ L. s = 00…0011…1100…0011…11 P P P P

24 USING THE PUMPING LEMMA Prove L = {ww | w ∈ {0,1}*} is not context-free Assume L is context-free. Then there is a pumping length P. No matter what P is, the string s = 0 P 1 P 0 P 1 P has |s|≥P and s ∈ L. So there should be uvxyz=s with: 1) |vy| > 0, 2) |vxy| ≤P, 3) ∀ i, uv i xy i z ∈ L. s = 00…0011…1100…0011…11 P P P P vxy cannot be only in the first half, since pumping down would move a 0 to the end of the first half. vxy cannot be only in the last half, since pumping up would move a 0 to the end of the first half. vxy Then pumping down must remove at least one 1 or one zero, e.g. uxz = 0 P 1 i 0 j 1 P where i  p or j  p. So uxz  L, no matter how They are chosen; so L cannot be context-free!

25 USING THE PUMPING LEMMA Prove L = {w#w R | w ∈ {0,1}* and #1s = #0s} is not context-free Assume L is context-free. Then there is a pumping length P. No matter what P is, the string s = 0 P 1 P #1 P 0 P has |s| ≥ P and s ∈ L. So there should be uvxyz=s with: 1) |vy| > 0, 2) |vxy| ≤ P, 3) ∀ i, uv i xy i z ∈ L. s = 00…0011…11#11…1100…00 P P P P

26 USING THE PUMPING LEMMA Prove L = {w#w R | w ∈ {0,1}* and #1s = #0s} is not context-free Assume L is context-free. Then there is a pumping length P. No matter what P is, the string s = 0 P 1 P #1 P 0 P has |s| ¸ P and s 2 L. So there should be uvxyz=s with: 1) |vy| > 0, 2) |vxy| ≤ P, 3) ∀ i, uv i xy i z ∈ L. s = 00…0011…11#11…1100…00 P P P P vxy cannot be only in either half, since pumping would make one side of # longer than the other. # cannot be in v or y since pumping would add too many #s. So it must be in x. vxy since # is in x and |vxy|≤ P, neither v nor y can have 0s, and at least one must have a 1. Then uv 2 xy 2 z has more 1s than 0s and is not in L.


Download ppt "INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011."

Similar presentations


Ads by Google