Upcoming SlideShare
×

# Theory of Computation: Lecture 37: Stack Machines, Chomsky Normal Form

1,499 views
1,419 views

Published on

1) Stack Machines
2) Acceptance by Empty Stack
3) Converting FSAs to Stack Machines
4) Converting CFGs to Stack Machines
5) Chomsky Normal Form

Published in: Science, Technology
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,499
On SlideShare
0
From Embeds
0
Number of Embeds
619
Actions
Shares
0
0
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Theory of Computation: Lecture 37: Stack Machines, Chomsky Normal Form

1. 1. CS 5000: Theory of Computation Lecture 37 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco
2. 2. Outline ● Review ● Stack Machines – Acceptance by Empty Stack – Converting Finite Automata to Stack Machines – Converting CFGs to Stack Machines ● Elimination of Useless Symbols from CFGs ● Chomsky Normal Formwww.youtube.com/vkedco
3. 3. Review: A Stack Machine Finite Control Stack Input Tapewww.youtube.com/vkedco
4. 4. Review: Stack Machine Move read pop push a c abc If the current input symbol is a and the symbol on top of the stack is c, read a (advance to the next symbol to the right of the current symbol), pop c off the stack, and push abc onto the stackwww.youtube.com/vkedco
5. 5. Stack Machine: Formal Definition A stack machine M is a 4 - tuple ( Γ , Σ , S, δ ) , where Γ is the stack alphabet; Σ is the input alphabet; S ∈ Γ is the initial alphabet symbol; δ ∈ ( ( Σ ∪ {ε } ) × Γ → P ( Γ * ) ), where P ( Γ * ) is the power set of Γ , i.e., the output is a set of * strings, because M is non - deterministic.www.youtube.com/vkedco
6. 6. Example read pop push ε S ab a S ef a S ε δ ( ε , S ) = { ab} δ ( a, S ) = {ε , ef }www.youtube.com/vkedco
7. 7. Stack Machine: Instantaneous Description ( x, y ) where x ∈ Σ * is the unread part of the input and y ∈ Γ * is the current stack contents, the leftmost character of y is on top of the stackwww.youtube.com/vkedco
8. 8. Stack Machine: Derivation  is a relation on instantaneous descriptions. 1. ( ax, Bz )  ( x, yz ) if and only if y ∈ δ ( a, B ) ; 2. ( x, Bz )  ( x, yz ) if and only if y ∈ δ ( ε , B )www.youtube.com/vkedco
9. 9. Stack Machine: Derivation *  is a relation on instantaneous descriptions. Let A and B be two instantaneous descriptions. Then * A  B if and only if there is a sequence of zero or more  transitions from A to B.www.youtube.com/vkedco
10. 10. Acceptance by the Empty Stack { L ( M ) = x ∈ Σ | ( x, S )  ( ε , ε * * )}www.youtube.com/vkedco
11. 11. Converting Finite State Automata to Stack Machineswww.youtube.com/vkedco
12. 12. Regular Languages & Stack Machines ● We can construct an FA for a regular language ● We can construct a regular grammar from an NFA ● Regular grammars are context-free grammars ● It is no surprise that we can construct stack machines from finite automatawww.youtube.com/vkedco
13. 13. Finite Automata To Stack Machine 0 1 q0 q1 1 0 1 0 q2 q3 0 1www.youtube.com/vkedco
14. 14. Finite Automata To Stack Machine δ ( 0, q0 ) = {q0 } read pop push δ (1, q0 ) = {q1} 0 q0 q0 1 q0 q1 δ ( 0, q1 ) = {q2 } 0 q1 q2 δ (1, q1 ) = {q3 } 1 q1 q3 δ ( 0, q2 ) = {q0 } 0 q2 q0 δ (1, q2 ) = {q1} 1 q2 q1 δ ( 0, q3 ) = {q2 } 0 q3 q2 δ (1, q3 ) = {q3 } 1 q3 q3 δ (ε , q2 ) = {ε } ε q2 ε δ (ε , q3 ) = {ε } ε q3 εwww.youtube.com/vkedco
15. 15. Converting CFGs To Stack Machineswww.youtube.com/vkedco
16. 16. Converting CFG To Stack Machine Theorem : If G = (V , Σ , S , P ) is a CFG, there is a stack machine M such that L( G ) = L( M ). Proof Sketch : M = (V ∪ Σ ,Σ , S , δ ) , where δ is defined as follows : 1. for all v ∈ V , δ ( ε , v ) = { x | ( v → x ) ∈ P}; 2. for all a ∈ Σ , δ ( a, a ) = {ε }.www.youtube.com/vkedco
17. 17. Example: CFG To Stack Machine ● Consider the following CFG: ● S  aSa ● S  bSb ● Sεwww.youtube.com/vkedco
18. 18. Example: CFG To Stack Machine read pop push ε S aSa ε S bSb ε S ε a a ε b b εwww.youtube.com/vkedco
19. 19. Elimination of Useless Symbols From CFGswww.youtube.com/vkedco
20. 20. Useless Symbols ● A symbol X is useful if there is a derivation S * αXβ * w for some α, β in (V U T)* and w is in T* ● A symbol X is useless if there is no such derivationwww.youtube.com/vkedco
21. 21. Useless Symbols ● S  AB | a ● Aa ● B is a useless symbol ● S, A are useful symbolswww.youtube.com/vkedco
22. 22. Elimination of Useless Symbols Lemma1 : Let a CFG G = (V , T , P, S ) and L( G ) ≠ ∅ . There exists an equivalent CFG G = (V , T , P , S ) such that for each A in V there is some w ∈ T for * which A ⇒ * w.www.youtube.com/vkedco
23. 23. Common Sense Interpretation ● Lemma 1 preserves only those variables that can generate terminal strings ● Another take: Lemma 1 preserves only those variables from which terminal strings can be derivedwww.youtube.com/vkedco
24. 24. Computation of V’ and P’ begin OLDVars = { }; NEWVars = {A | A  w for some w in T*} while OLDVars != NEWVars { OLDVars = NEWVars; NEWVars = OLDVars U { A | A  α, α in (T U OLDVars)* }; } V’ = NEWVars; end P’ = all productions whose symbols are in V’ U T;www.youtube.com/vkedco
25. 25. Elimination of Useless Symbols Lemma 2 : Given a CFG G = ( V , T , P, S ) , there exists a CFG G = ( V , T , P , S ) such that for each X in V ∪ T there exists α , β in ( V ∪ T ) and S ⇒ α Xβ . * * Proof : Place S in V . Keep adding variables and productions as follows. If A is placed in V and A → α 1 | α 2 | ... | α n are productions in P, then add all variables in α 1 ,α 2 ,..., α n to V and all terminals in α 1 , α 2 ,..., α n to T . Once V and T have been constructed, iterate over P and place into P only those productions that have symbols in ( V ∪ T ).www.youtube.com/vkedco
26. 26. Common Sense Interpretation Lemma 2 preserves only those symbols that can be reached from S via some derivationwww.youtube.com/vkedco
27. 27. Elimination of Useless Symbols: Theorem Theorem : Every non - empty CFL is generated by a CFG with no useless symbols. Proof : Apply the algorithm of Lemma 1 and then the one of Lemma 2.www.youtube.com/vkedco
28. 28. Example ● S  AB | a ● Aa ● Lemma 1, gives us S  a, A  a. ● Lemma 2, gives us S  a. ● The result grammar has no useless symbols: ● Sawww.youtube.com/vkedco
29. 29. Does the Order of Lemmas Matter? ● Yes! ● Let us apply Lemma 2 first and Lemma 1 second to this grammar: – S  AB | a – Aa ● Applying Lemma 2, we end up with the same grammar: – S  AB | a – Aa ● Applying Lemma 1, we end up with: – Sa – Aa ● But this grammar has A as a useless symbolwww.youtube.com/vkedco
30. 30. Example ● Does the order of Lemmas matter? Yes! ● Let us apply Lemma 2 first and Lemma 1 second – S  AB | a – Aa ● Applying Lemma 2, we end up with the same grammar: – S  AB | a – Aa ● Applying Lemma 1, we end up with – SA – Aa ● But in this grammar A is a useless symbolwww.youtube.com/vkedco
32. 32. CNF: Chomsky Normal Form A CFG is said to be in Chomsky Normal form if all of its productions are of the two forms : 1) A → BC ; 2) A → a; where A, B, C are variables and a is a terminal.www.youtube.com/vkedco
33. 33. CNF Theorem Let G be a grammar with no useless symbols and no ε- productions. There is a CNF grammar G’ such that L(G) = L(G’).www.youtube.com/vkedco
34. 34. CNF Theorem: Proof ● The set of new productions P’ for G’ is obtained in 3 stages ● Stage 1: Iterate through the productions P of grammar G and remove all productions of the form A  a or A  BC and place them into P’, because these productions are already in CNFwww.youtube.com/vkedco
35. 35. CNF Theorem: Proof ● Stage 2: For each production A  α, where α is a string over the set of terminals and non-terminals of length at least 2, do the following: – For each symbol s in α ● if s is a terminal, add C  s to P’; s – Rewrite A  α as A  α’ where α’ is obtained from α by replacing each s in α with Cswww.youtube.com/vkedco
36. 36. Example: CNF Conversion S  bA S  aB A  bAA A  aS Aa B  aBB B  bS Bbwww.youtube.com/vkedco
37. 37. CNF Conversion: Stage 1 ● These productions can be put into the new grammar as they are, because they already are in CNF: – Aa – Bbwww.youtube.com/vkedco
38. 38. CNF Conversion: Stage 2 ● For S  bA, – add Cb  b; – add S  CbA ● For S  aB, – add Ca  a; – add S  CaB. ● For A  bAA, – add A  CbAA. ● For A  aS, – add A  CaS. ● For B  aBB, – add B  CaBB. ● For B  bS, – add B  CbS.www.youtube.com/vkedco
39. 39. Grammar after Stages 1 & 2 1. S  CbA // rewritten at stage 2 2. S  CaB // rewritten at stage 2 3. A  CbAA // rewritten at stage 2 4. A  CaS // rewritten at stage 2 5. A  a // removed at stage 1 6. B  CaBB // rewritten at stage 2 7. B  CbS // rewritten at stage 2 8. B  b // removed at stage 1www.youtube.com/vkedco
40. 40. CNF Conversion: Stage 3 ● Add productions 1, 2, 4, 7 to P’, because they are in CNF ● Replace A  CbAA (production 3) with – A  CbD1; – D1 AA ● Replace B  CaBB (production 6) with – B  CaD2; – D2  BBwww.youtube.com/vkedco
41. 41. CNF Conversion: Result CNF Grammar ● S  CbA | CaB ● A  CaS | CbD1 | a ● B  CbS | CaD2 | b ● D1 AA ● D2  BB ● Ca  a ● Cb  bwww.youtube.com/vkedco
42. 42. CFL Membership Through CYK Algorithmwww.youtube.com/vkedco
43. 43. CFL Membership ● Problem: Given a CFG G = (V, T, P, S) and a string x in T*, determine if x is in L(G)? ● Cocke-Younger-Kasami (CYK) algorithm takes a CFG in CNF and a string and returns true or false, depending on whether x is or is not in L(G) ● The CYK algorithm runs in O(n3), where |x|=nwww.youtube.com/vkedco
44. 44. CYK Algorithm ● The input is a CNF grammar G with no ε-productions and a string x such that |x| ≥ 1 ● Let xij be the substring of x of length j beginning at position i. ● Since |x| = n, 1≤ i ≤ n and 1≤ j ≤ n ● In general, if we do 1-based array index counting, i.e., the first position in an array is 1, not 0, and the length of the substring is j, the last available position i at which the substring can begin is n–j+1 ● For example, if |x| = 4 and j = 2, the possible values for i in xi2 are 1, 2, and 3 = 4 – 2 + 1www.youtube.com/vkedco
45. 45. CYK Algorithm ● Let xij be the substring of x of length j beginning at position i ● If j = 1, then, for each 1≤ i ≤ n, we can check if xi1 can be derived directly from some variable of G ● In other words, we can determine if G has a production V  xi1www.youtube.com/vkedco
46. 46. CYK Algorithm: Basic Insight ● Let xij be the substring of x of length j beginning at position i ● If j>1, A * xij if and only if there is some production A  BC and some k, 1≤k<j, such that B* xik and C * x(i+k)(j-k) b a a b a Example: Consider the above string. if i = 1 and j = 5, then 1≤k<5. If k = 1, then B*x11 and C*x24. If k = 2, then B*x12 and C*x33. If k = 3, then B*x13 and C*x42www.youtube.com/vkedco If k = 4, then B*x14 and C*x51
47. 47. CYK Algorithm: Basic Insight A B C i i+k-1 i+k i+j j-k k Xijwww.youtube.com/vkedco