Tutorial 05 CSC 3130 Formal Languages and Automata

  • Slides: 30
Download presentation
Tutorial 05 -- CSC 3130 : Formal Languages and Automata Theory Tu Shikui (sktu@cse.

Tutorial 05 -- CSC 3130 : Formal Languages and Automata Theory Tu Shikui (sktu@cse. cuhk. edu. hk) SHB 905, Office hour: Thursday 2: 30 pm-3: 30 pm 2008 -10 -06

Outline n Context-free Languages, Context-free grammars (CFG), Push-down Automata (PDA) n Grammar Transformations Remove

Outline n Context-free Languages, Context-free grammars (CFG), Push-down Automata (PDA) n Grammar Transformations Remove ε- production; n Remove unit-production; n Covert to Chomsky-Normal-Form (CNF) n n Cocke-Younger-Kasami (CYK) algorithm

Relations Context-free Languages L L = L(G) Context-free Grammars G L(G) = L(M) L

Relations Context-free Languages L L = L(G) Context-free Grammars G L(G) = L(M) L = L(M) Push-down Automata M

Example (I) n Given the following CFG Σ={a, b} S X | Y X

Example (I) n Given the following CFG Σ={a, b} S X | Y X a. Xb | a. X | a Y a. Yb | b n (1) L(G) = ? n (2) Design an equivalent PDA for it.

Example (I) --- solution: L(S) S X|Y X a. Xb | a. X |

Example (I) --- solution: L(S) S X|Y X a. Xb | a. X | a Y a. Yb | b Try to write some strings generated by it: S X a. Xb aa. Xbb aaaabb more a’s than b’s S Y a. Ybb aa. Ybbb aabbbb more b’s than a’s Observations: • Start from S, we can enter two States X & Y, and X, Y are “independent”; Ls = Lx U Ly • In X state, always more a are generated; Lx = { aibj; i>j } • In Y state, always more b are generated. Lx = { aibj; i<j } L(S) = { aibj; i≠j }

Example (I) --- solution: PDA L(S) = { aibj; i≠j } ={ a ib

Example (I) --- solution: PDA L(S) = { aibj; i≠j } ={ a ib j; i>j } U { a ib j; S X| Y X a. Xb | a. X Y a. Yb | Yb i<j } | a | b PDA = NFA + a stack (infinite memory) A possible way: “divide and conquer” Lx = { aibj; i>j } a, /A , /# q’ 0 , A/ b, A/ , / q’ 1 LY = { aibj; i<j } , A/ q’ 2 , #/ a, /A q’ 3 , /# , / Combine both … q 0 , / b, A/ q 1 b, #/# q 2 , #/ q 3

Example (II) n Given the following language: L = {0 i 1 j: i

Example (II) n Given the following language: L = {0 i 1 j: i ≤ j ≤ 2 i, i=0, 1, …}, n (1) design a CFG for it; n (2) design a PDA for it. S = {0, 1}

Example (II) -- solution: CFG L = {0 i 1 j: i ≤ j

Example (II) -- solution: CFG L = {0 i 1 j: i ≤ j ≤ 2 i, i=0, 1, …}, 1} S = {0, Consider two extreme cases: (a). if j = i, then L 1 = { 0 i 1 j: i=j }; (b). if j = 2 i, then L 2 = { 0 i 1 j: 2 i=j }. S 0 S 1 “red-rule” S ε S 0 S 11 S ε If i ≤ j ≤ 2 i , then randomly choose “redrule” or “blue-rule” in the generation. S 0 S 11 S ε “blue-rule”

Example (II) -- solution: CFG L = {0 i 1 j: i ≤ j

Example (II) -- solution: CFG L = {0 i 1 j: i ≤ j ≤ 2 i, i=0, 1, …}, S = {0, 1} Need to verify L = L(G) G= S 0 S 11 S ε 1). L(G) is a subset of L: The “red-rule” and “blue-rule” guarantee that in each derivation, the number of 1 s generated is one or two times larger than that of 0 s. 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 ≤ 2 i, we use “red-rule” (2 i - j) times and then “blue-rule” ( j - i ) times, i. e. , S =*=> 02 i-j. S 12 i-j =*=> 02 i-j 0 j-i. S 12(j-i)12 i-j ==> 0 i 1 j = w

Example (II) -- solution: PDA L = {0 i 1 j: i ≤ j

Example (II) -- solution: PDA L = {0 i 1 j: i ≤ j ≤ 2 i, i=0, 1, …}, S = {0, Similar idea: 1} “randomly choose two extreme cases” 1, X/ 0, /X , /# q 1 , / , #/ q 0 1, X/X 1, X/ q 2 q 3

Example (III) n Given the following language: L = { aibjckdl: i, j, k,

Example (III) n Given the following language: L = { aibjckdl: i, j, k, l=0, 1, …; i+k=j+l }, where the alphabet Σ= {a, b, c, d} n (1). Design a CFG for it; n (2). Design a PDA for it.

Example (III) – solution: CFG L = { aibjckdl: i, j, k, l=0, 1,

Example (III) – solution: CFG L = { aibjckdl: 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 = aibjckdl = anaj bj ckdkdn S a. Sd | XY w Three blocks come from the same template: an dn N x. Nx X a. Xb | ε Y c. Yd | ε S=*=>an. Sdn=*=> an. XYdn ajbj ckdk =*=>anajbj. Ydn =*=>anajbj ckbkdn aj bj ck dk = an+jbj ckbn+k (n+j) + k = j + (n+k)

Example (III) – solution: PDA L = { aibjckdl: i, j, k, l=0, 1,

Example (III) – solution: PDA L = { aibjckdl: 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? Action 1: Push an X, when a or c was read; Action 2: Pop an X (if any, otherwise push a Y), when b or d was read. Action 3: Pop an Y (if any, otherwise push an X), when a or c was read.

Example (III) – solution: PDA L = { aibjckdl: i, j, k, l=0, 1,

Example (III) – solution: PDA L = { aibjckdl: i, j, k, l=0, 1, …; i+k=j+l }, Action 1: Push an X, when a or c was read; Action 2: Pop an X (if any, otherwise push a Y), when b or d was read. Action 3: Pop an Y (if any, otherwise push an X), when a or c was read. b, X/ a, /X , /# q 1 c, #/X# c, X/XX , / q 2 b, #/Y# b, Y/YY , / q 3 c, Y/ , / d, X/ q 4 d, #/Y# d, Y/YY , #/ q 5

Outline n Context-free Languages, Context-free grammars (CFG), Push-down Automata (PDA) n Grammar Transformations Remove

Outline n Context-free Languages, Context-free grammars (CFG), Push-down Automata (PDA) n Grammar Transformations Remove ε- production; n Remove unit-production; n Covert to Chomsky-Normal-Form (CNF) n n Cocke-Younger-Kasami (CYK) algorithm

Remove ε-production n Example: Remove ε-productions of the following grammar G: S ABa. C

Remove ε-production n Example: Remove ε-productions of the following grammar G: S ABa. C A BC B b |ε C D |ε D d

Remove ε-production Nullable variables: A, B, C S ABa. C | Aa. C |

Remove ε-production Nullable variables: A, B, C S ABa. C | Aa. C | ABa | Aa | a A BC | B B b |ε C D |ε D d Add For A: S Ba. C For B: S Aa. C | a. C Delete B ε A C For C: S ABa | Aa | a A B For i = 1 to k For every production of the form A → Ni , add another production A → If Ni → is a production, remove it C ε

Remove unit-productions n Example: Remove the unit-productions of the following grammar: S S+T|T T

Remove unit-productions n Example: Remove the unit-productions of the following grammar: S S+T|T T T*F |F F (S) | a

Remove unit-productions Add S S+T | T*F | a | (S) T T*F |

Remove unit-productions Add S S+T | T*F | a | (S) T T*F | a | (S) F (S) | a Delete S T For T T*F For T F a For T F (S) S T*F S a S (S) T F For F a For F (S) S a S (S) Rule 1: A 1 → A 2 →. . . → Ak → A 1 delete it and replace everything with A 1 Rule 2: A 1 → A 2 →. . . → Ak → A 1 → , . . . , Ak →

Convert CFG to Chomsky-Normal. Form (CNF) n Example: Convert the following CFG to CNF:

Convert CFG to Chomsky-Normal. Form (CNF) n Example: Convert the following CFG to CNF: S 0 AB A 0 D | 1 AD B 0 D 1

Convert CFG to Chomsky-Normal. Form (CNF) A → Bc. DE replace terminals with new

Convert CFG to Chomsky-Normal. Form (CNF) A → Bc. DE replace terminals with new variables S 0 AB A 0 D | 1 AD B 0 D 1 A → BCDE C→c break up sequences with new variables A → BX 1 → CX 2 → DE C→c S XAB; X 0 A XD | YAD; Y 1

Convert CFG to Chomsky-Normal. Form (CNF) A → Bc. DE replace terminals with new

Convert CFG to Chomsky-Normal. Form (CNF) A → Bc. DE replace terminals with new variables S XAB X 0 A XD |YAD Y 1 B 0 D 1 A → BCDE C→c break up sequences with new variables S XN 1; N 1 AB A YN 2; N 2 AD A → BX 1 → CX 2 → DE C→c

Convert CFG to Chomsky-Normal. Form (CNF) A → Bc. DE replace terminals with new

Convert CFG to Chomsky-Normal. Form (CNF) A → Bc. DE replace terminals with new variables A → BCDE C→c S XN 1 AB A XD A YN 2 AD X 0 Y 1 B 0 D 1 break up sequences with new variables A → BX 1 → CX 2 → DE C→c

Outline n Context-free Languages, Context-free grammars (CFG), Push-down Automata (PDA) n Grammar Transformations Remove

Outline n Context-free Languages, Context-free grammars (CFG), Push-down Automata (PDA) n Grammar Transformations Remove ε- production; n Remove unit-production; n Covert to Chomsky-Normal-Form (CNF) n n Cocke-Younger-Kasami (CYK) algorithm

Cocke-Younger-Kasami (CYK) algorithm n Constructing the parse table n Input: w = a 1

Cocke-Younger-Kasami (CYK) algorithm n Constructing the parse table n Input: w = a 1 a 2…an ∈ Σ+ n Output: The parse table T for w such that tij contains A ⇔ A +⇒ aiai+1…ai+j-1 n Steps: n n n Step(1): cell(i, 1) = { A | A→ai ∈ 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

Cocke-Younger-Kasami (CYK) algorithm j=5 cell 11, cell 24 cell 12, cell 33 cell 13,

Cocke-Younger-Kasami (CYK) algorithm j=5 cell 11, cell 24 cell 12, cell 33 cell 13, cell 42 cell 14, cell 51 Cell (i, k), Cell (i+k, j-k) k = 1, …, j-1 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 31, cell 41 j=1 A 1 x 1 A 2 x 2 i=1 i=2 x 1 x 2 cell 41, cell 51 A 3 x 3 A 4 x 4 A 5 x 5 i=3 x 3 i=4 x 4 i=5 x 5

Cocke-Younger-Kasami (CYK) algorithm j=5 cell 11, cell 24: S, A cell 12, cell 33:

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 j=1 A 1 x 1 : A A 2 x 2 : S A 3 x 3 : A i=1 i=2 i=3 a S → AA | AS | b A → SA | AS | a b Test: w = abaab a cell 41, cell 51: S, A A 4 x 4 : A A 5 x 5 : S i=4 a i=5 b

Cocke-Younger-Kasami (CYK) algorithm Trace back to find a parse tree j=5 cell 11, cell

Cocke-Younger-Kasami (CYK) algorithm Trace back to find a parse tree 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 j=1 A 1 x 1 : A A 2 x 2 : S A 3 x 3 : A i=1 i=2 i=3 a S → AA | AS | b A → SA | AS | a b Test: w = abaab a cell 41, cell 51: S, A A 4 x 4 : A A 5 x 5 : S i=4 a i=5 b

Cocke-Younger-Kasami (CYK) algorithm We can find another parse tree. j=5 cell 11, cell 24:

Cocke-Younger-Kasami (CYK) algorithm We can find another parse tree. 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 j=1 A 1 x 1 : A A 2 x 2 : S A 3 x 3 : A i=1 i=2 i=3 a S → AA | AS | b A → SA | AS | a b Test: w = abaab a cell 41, cell 51: S, A A 4 x 4: A A 5 x 5 : S i=4 a i=5 b

End of this tutorial! Thanks for coming!

End of this tutorial! Thanks for coming!