1 / 26

INHERENT LIMITATIONS OF COMPUTER PROGRAMS

CSci 4011. INHERENT LIMITATIONS OF COMPUTER PROGRAMS. string. pop. push. ε , ε → $. 0, ε → 0. 1,0 → ε. ε ,$ → ε. 1,0 → ε. CONTEXT-FREE GRAMMARS. A → 0 A 1. A → B. B → #. A.  0A1.  00A11.  00B11.  00#11. uVw yields uvw if (V → v) 2 R.

bisa
Download Presentation

INHERENT LIMITATIONS OF COMPUTER PROGRAMS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSci 4011 INHERENT LIMITATIONS OF COMPUTER PROGRAMS

  2. string pop push ε,ε → $ 0,ε→ 0 1,0→ε ε,$ → ε 1,0→ε

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

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

  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 wi∈ Σ 3. The non-leaf nodes have labels v ∈ V 4. For each node with label v ∈ V and children with labels ri∈ (V∪Σ), (v→r) ∈ R.

  6. <EXPR> <EXPR> <EXPR> <EXPR> <EXPR> <EXPR> <EXPR> <EXPR> <EXPR> <EXPR> a + a x a a + a x a <EXPR> → <EXPR> + <EXPR> <EXPR> → <EXPR> x <EXPR> <EXPR> → ( <EXPR> ) <EXPR> → a Build a parse tree for a + a x 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 〈condition〉 → a==1| a==2 if a==1 then if a==2 then a = 1 else a = 2

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

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

  12. 4. Convert all remaining rules into the proper form S → aSb R → aRb S0 → aRb S → aRb R → ab S0 → A1A2 S → A1A2 S → ab A1 → a S → ab S → aRb A2 → RA3 S → A1A3 S0 → aSb A3 → b R → ab S0→ ε S0 → ab R → A1A3 S0 → ab S0 → A1A3 R → aRb S0 → aRb S0 → aSb R → A1A2 S0 → A1A4 S → aSb A4 → SA3 S → A1A4

  13. Convert the following into Chomsky normal form: A → BAB | B | ε B → 00 | ε S  SaSb | ε • Add a new start variable S0 and rule S0 → 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 B and C are not start variable A → a a is a terminal S → ε 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} NOT YES L₂ = {xy | x,y ∈ {0,1}*, |x|=|y| and x ≠ y}

  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 there exist uvxyz=w, where: 1. |vy| > 0 2. |vxy| ≤ P 3. For every i ≥ 0, uvixyiz  L

  18. EXAMPLES there exist uvxyz=w, where: • |vy| > 0 • |vxy| ≤ P • uvixyiz  L for any i ≥ 0 Example: L = { w ∈ {0,1}* | w = wR}. 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. T T R R R R R R y v u z u v x y z v x y Idea: If w is long enough, then any parse tree for w must have a path that contains a variable more than once

  20. Formal Proof: Let b be the maximum number of symbols on the right-hand side of a rule If the height of a parse tree is h, the length of the string generated is at most: bh 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. T T R R R R R R y v u z u v x y z v x y The longest path in T must have ≥ |V|+1 variables Select R to be the variable that repeats among the lowest |V|+1 variables 1. |vy| > 0 2. |vxy| ≤ P Let T be the parse tree for w with the smallest number of nodes. T must have height at least |V|+2

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

  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 = 0P1P0P1P has |s| ≥ P and s∈ L. So there should be uvxyz=s with: 1) |vy| > 0, 2) |vxy| ≤ P, 3) ∀i, uvixyiz∈ L. P P P P s = 00…0011…1100…0011…11

  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 = 0P1P0P1P has |s|≥P and s∈ L. vxy cannot be only in the first half, since pumping down would move a 0 to the end of the first half. Then pumping down must remove at least one 1 or one zero, e.g. uxz = 0P1i0j1P where i p or j  p. So uxz L, no matter how They are chosen; so L cannot be context-free! vxy cannot be only in the last half, since pumping up would move a 0 to the end of the first half. So there should be uvxyz=s with: 1) |vy| > 0, 2) |vxy| ≤P, 3) ∀i, uvixyiz∈L. P P P P s = 00…0011…1100…0011…11 vxy

  25. USING THE PUMPING LEMMA Prove L = {w#wR | 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 = 0P1P#1P0P has |s| ≥ P and s∈L. So there should be uvxyz=s with: 1) |vy| > 0, 2) |vxy| ≤ P, 3)∀i, uvixyiz∈ L. P P P P s = 00…0011…11#11…1100…00

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

More Related