Download presentation
Presentation is loading. Please wait.
1
Tutorial 05 -- CSC3130 : Formal Languages and Automata Theory Tu Shikui ( sktu@cse.cuhk.edu.hk ) SHB 905, Office hour: Thursday 2:30pm-3:30pm 2008-10-06
2
Outline Context-free Languages, Context-free grammars (CFG), Push-down Automata (PDA) Grammar Transformations Remove ε- production; Remove unit-production; Covert to Chomsky-Normal-Form (CNF) Cocke-Younger-Kasami (CYK) algorithm
3
Relations Context-free Languages L Context-free Grammars G Push-down Automata M L = L(G) L = L(M) L(G) = L(M)
4
Example (I) Given the following CFG S X | Y X aXb | aX | a Y aYb | Yb | b (1) L(G) = ? (2) Design an equivalent PDA for it. Σ={a, b}
5
Example (I) --- solution: L(S) S X | Y X aXb | aX | a Y aYb | Yb | b Try to write some strings generated by it: S X aXb aaXbb aaaXbb aaaabb S Y aYb aYbb aaYbbb aabbbb more a’s than b’s more b’s than a’s Observations: Start from S, we can enter two States X & Y, and X, Y are “independent”; In X state, always more a are generated; In Y state, always more b are generated. Ls = Lx U Ly Lx = { a i b j ; i>j } Lx = { a i b j ; i<j } L(S) = { a i b j ; i≠j }
6
Example (I) --- solution: PDA S X | Y X aXb | aX | a Y aYb | Yb | b L(S) = { a i b j ; i≠j } PDA = NFA + a stack (infinite memory) = { a i b j ; i>j } U { a i b j ; i<j } A possible way: “divide and conquer” L x = { a i b j ; i>j } a, /A q0q0 , /# ,/,/ q1q1 b,A/ b,#/# q2q2 q3q3 ,#/ L Y = { a i b j ; i<j } q’ 0 , /# ,/,/ q’ 1 ,A/ q’ 2 ,A/ q’ 3 ,#/ a, /Ab,A/ , / Combine both …
7
Example (II) Given the following language: (1) design a CFG for it; (2) design a PDA for it. L = {0 i 1 j : i ≤ j ≤ 2i, i=0,1,…}, = {0, 1}
8
Example (II) -- solution: CFG L = {0 i 1 j : i ≤ j ≤ 2i, i=0,1,…}, = {0, 1} Consider two extreme cases: (a). if j = i, then L 1 = { 0 i 1 j : i=j }; (b). if j = 2i, then L 2 = { 0 i 1 j : 2i=j }. S 0S1 S ε S 0S11 S ε If i ≤ j ≤ 2i, then randomly choose “red- rule” or “blue-rule” in the generation. “red-rule” “blue-rule” S 0S1 S 0S11 S ε
9
Example (II) -- solution: CFG L = {0 i 1 j : i ≤ j ≤ 2i, i=0,1,…}, = {0, 1} S 0S1 S 0S11 S ε Need to verify L = L(G) G = 1). L(G) is a subset of L: The “red-rule” and “blue-rule” guarantee that in each derivation, the number of 1s generated is one or two times larger than that of 0s. So, L(G) is a subset of L. 2). L is a subset of L(G): For any w = 0 i 1 j, i ≤ j ≤ 2i, we use “red-rule” ( 2i - j ) times and then “blue-rule” ( j - i ) times, i.e., S =*=> 0 2i-j S1 2i-j =*=> 0 2i-j 0 j-i S1 2(j-i) 1 2i-j ==> 0 i 1 j = w
10
Example (II) -- solution: PDA L = {0 i 1 j : i ≤ j ≤ 2i, i=0,1,…}, = {0, 1} Similar idea: “randomly choose two extreme cases” q0q0 , /# 0, /X ,/,/ q1q1 q2q2 ,#/ 1,X/ 1,X/X 1,X/ q3q3
11
Example (III) Given the following language: (1). Design a CFG for it; (2). Design a PDA for it. L = { a i b j c k d l : i,j,k,l=0,1,…; i+k=j+l }, where the alphabet Σ= {a, b, c, d}
12
Example (III) – solution: CFG L = { a i b j c k d l : i,j,k,l=0,1,…; i+k=j+l }, Note that i + k = j + l ==> | i – j | = | l – k |. Assume n = i – j = l – k > 0, then i = n + j, l = n + k, and w = a i b j c k d l = a n a j b j c k d k d n w anan dndn ajbjajbj ckdkckdk ajaj bjbj ckck dkdk Three blocks come from the same template: N x N x S aSd | XY X aXb | ε Y cYd | ε S=*=>a n Sd n =*=> a n XYd n =*=>a n a j b j Yd n =*=>a n a j b j c k b k d n = a n+j b j c k b n+k (n+j) + k = j + (n+k)
13
Example (III) – solution: PDA L = { a i b j c k d l : i,j,k,l=0,1,…; i+k=j+l }, Main idea: (1)use X to record an a or c; use Y to record an b or d. (2)Compare #X and #Y: by cancellation. How to realize the comparison by cancellation? Action1 : Push an X, when a or c was read; Action2 : Pop an X (if any, otherwise push a Y), when b or d was read. Action3 : Pop an Y (if any, otherwise push an X), when a or c was read.
14
Example (III) – solution: PDA L = { a i b j c k d l : i,j,k,l=0,1,…; i+k=j+l }, Action1 : Push an X, when a or c was read; Action2 : Pop an X (if any, otherwise push a Y), when b or d was read. Action3 : Pop an Y (if any, otherwise push an X), when a or c was read. , /# q5q5 q1q1 a, /X ,/,/ b,#/Y# q2q2 ,/,/ c,X/XX q3q3 ,/,/ q4q4 ,/,/ b,X/ b,Y/YY c,#/X# c,Y/ d,X/ d,#/Y# d,Y/YY
15
Outline Context-free Languages, Context-free grammars (CFG), Push-down Automata (PDA) Grammar Transformations Remove ε- production; Remove unit-production; Covert to Chomsky-Normal-Form (CNF) Cocke-Younger-Kasami (CYK) algorithm
16
Remove ε-production Example: Remove ε-productions of the following grammar G: S ABaC A BC B b | ε C D | ε D d
17
Remove ε-production S ABaC | BaC | AaC | aC | ABa | Ba | Aa | a A BC | C | B B b | ε C D | ε D d Nullable variables: A, B, C AddDelete For A:S BaC For B:S AaC | aC A C B ε For C:S ABa | Ba | Aa | a A B C ε For i = 1 to k For every production of the form A → N i , add another production A → If N i → is a production, remove it
18
Remove unit-productions Example: Remove the unit-productions of the following grammar: S S + T | T T T * F | F F (S) | a
19
Remove unit-productions Add Delete S T For T T*F S T*F For T F a S a S S+T | T | T*F | a | (S) T T*F | F | a | (S) F (S) | a For T F (S) S (S) T F For F a S a For F (S) S (S) A 1 → A 2 →... → A k → A 1 A 1 → A 2 →... → A k → A 1 → ,..., A k → delete it and replace everything with A 1 Rule 1: Rule 2:
20
Convert CFG to Chomsky-Normal- Form (CNF) Example: Convert the following CFG to CNF: S 0AB A 0D | 1AD B 0 D 1
21
Convert CFG to Chomsky-Normal- Form (CNF) A → BcDE replace terminals with new variables A → BCDE C → c break up sequences with new variables A → BX 1 X 1 → CX 2 X 2 → DE C → c S 0AB A 0D | 1AD B 0 D 1 S XAB; X 0 A XD | YAD; Y 1
22
Convert CFG to Chomsky-Normal- Form (CNF) A → BcDE replace terminals with new variables A → BCDE C → c break up sequences with new variables A → BX 1 X 1 → CX 2 X 2 → DE C → c S XAB X 0 A XD |YAD Y 1 B 0 D 1 S XN 1 ; N 1 AB A YN 2 ; N 2 AD
23
Convert CFG to Chomsky-Normal- Form (CNF) A → BcDE replace terminals with new variables A → BCDE C → c break up sequences with new variables A → BX 1 X 1 → CX 2 X 2 → DE C → c S XN 1 N 1 AB A XD A YN 2 N 2 AD X 0 Y 1 B 0 D 1
24
Outline Context-free Languages, Context-free grammars (CFG), Push-down Automata (PDA) Grammar Transformations Remove ε- production; Remove unit-production; Covert to Chomsky-Normal-Form (CNF) Cocke-Younger-Kasami (CYK) algorithm
25
Constructing the parse table Input : w = a 1 a 2 …a n ∈ Σ+ Output : The parse table T for w such that t ij contains A ⇔ A + ⇒ a i a i+1 …a i+j-1 Steps: Step(1): cell ( i,1 ) = { A | A→a i ∈ P, 1≤i≤n } Step(2): for 1 ≤ k < j, cell ( i,j ) = { A | for some k, A→BC ∈ P, B is in cell ( i,k ), C is in cell ( i+k, j-k ) } Step(3): repeat Step(2) for 1≤ i≤ n, 1≤ j ≤ n-i+1
26
Cocke-Younger-Kasami (CYK) algorithm j=5 cell 11, cell 24 cell 12, cell 33 cell 13, cell 42 cell 14, cell 51 j=4 cell 11, cell 23 cell 12, cell 32 cell 13, cell 41 cell 21, cell 33 cell 22, cell 42 cell 23, cell 51 j=3 cell 11, cell 22 cell 12, cell 31 cell 21, cell 32 cell 22, cell 41 cell 31, cell 42 cell 32, cell 51 j=2 cell 11, cell 21 cell 21, cell 31 cell 31, cell 41 cell 41, cell 51 j=1 A1 x1A1 x1 A2 x2A2 x2 A3 x3A3 x3 A4 x4A4 x4 A5 x5A5 x5 i=1i=2i=3i=4i=5 x1x1 x2x2 x3x3 x4x4 x5x5 Cell (i,k), Cell (i+k, j-k) k = 1, …, j-1
27
Cocke-Younger-Kasami (CYK) algorithm j=5 cell 11, cell 24: S, A cell 12, cell 33: S, A cell 13, cell 42: S, A cell 14, cell 51: S, A j=4 cell 11, cell 23: S, A cell 12, cell 32: S, A cell 13, cell 41: S, A cell 21, cell 33: A cell 22, cell 42: S, A cell 23, cell 51: -- j=3 cell 11, cell 22 : S cell 12, cell 31: A, S cell 21, cell 32: -- cell 22, cell 41: S cell 31, cell 42: S,A cell 32, cell 51: -- j=2 cell 11, cell 21: S, A cell 21, cell 31: A cell 31, cell 41: S cell 41, cell 51: S,A j=1 A 1 x 1 : AA 2 x 2 : SA 3 x 3 : AA 4 x 4 :AA 5 x 5 :S i=1i=2i=3i=4i=5 abaab S → AA | AS | b A → SA | AS | a Test: w = abaab
28
Cocke-Younger-Kasami (CYK) algorithm j=5 cell 11, cell 24: S, A cell 12, cell 33: S, A cell 13, cell 42: S, A cell 14, cell 51: S, A j=4 cell 11, cell 23: S, A cell 12, cell 32: S, A cell 13, cell 41: S, A cell 21, cell 33: A cell 22, cell 42: S, A cell 23, cell 51: -- j=3 cell 11, cell 22 : S cell 12, cell 31: A, S cell 21, cell 32: -- cell 22, cell 41: S cell 31, cell 42: S,A cell 32, cell 51: -- j=2 cell 11, cell 21: S, A cell 21, cell 31: A cell 31, cell 41: S cell 41, cell 51: S,A j=1 A 1 x 1 : AA 2 x 2 : SA 3 x 3 : AA 4 x 4 :AA 5 x 5 :S i=1i=2i=3i=4i=5 abaab S → AA | AS | b A → SA | AS | a Test: w = abaab Trace back to find a parse tree
29
Cocke-Younger-Kasami (CYK) algorithm j=5 cell 11, cell 24: S, A cell 12, cell 33: S, A cell 13, cell 42: S, A cell 14, cell 51: S, A j=4 cell 11, cell 23: S, A cell 12, cell 32: S, A cell 13, cell 41: S, A cell 21, cell 33: A cell 22, cell 42: S, A cell 23, cell 51: -- j=3 cell 11, cell 22 : S cell 12, cell 31: A, S cell 21, cell 32: -- cell 22, cell 41: S cell 31, cell 42: S,A cell 32, cell 51: -- j=2 cell 11, cell 21: S, A cell 21, cell 31: A cell 31, cell 41: S cell 41, cell 51: S,A j=1 A 1 x 1 : AA 2 x 2 : SA 3 x 3 : AA 4 x 4 : AA 5 x 5 :S i=1i=2i=3i=4i=5 abaab S → AA | AS | b A → SA | AS | a Test: w = abaab We can find another parse tree.
30
End of this tutorial! Thanks for coming!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.