1 / 30

Tutorial 05 -- CSC3130 : Formal Languages and Automata Theory

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. Outline. Context-free Languages, Context-free grammars (CFG), Push-down Automata (PDA) Grammar Transformations Remove ε - production;

duc
Download Presentation

Tutorial 05 -- CSC3130 : Formal Languages and Automata Theory

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. 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 L = L(G) Context-free Languages L Context-free Grammars G L(G) = L(M) L = L(M) Push-down Automata 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 L(S) = { aibj; i≠j } Lx = { aibj; i>j } Lx = { aibj; i<j }

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

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

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

  9. Example (II) -- solution: CFG L = {0i1j: i ≤ j ≤ 2i, i=0,1,…}, S = {0, 1} ? Need to verify L = L(G) G = S  0S1 S  0S11S  ε 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 = 0i1j, i ≤ j ≤ 2i, we use “red-rule” (2i - j) times and then “blue-rule” ( j - i ) times, i.e., S =*=> 02i-jS12i-j =*=> 02i-j0j-iS12(j-i)12i-j==> 0i1j = w

  10. 1,X/e 0,e/X q1 e,e/e e,#/e e,e/# 1,X/X 1,X/e q0 q2 q3 Example (II) -- solution: PDA L = {0i1j: i ≤ j ≤ 2i, i=0,1,…}, S = {0, 1} Similar idea: “randomly choose two extreme cases”

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

  12. w an dn ajbj ckdk 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 = anajbj ckdkdn S  aSd | XY X  aXb |ε Y  cYd|ε Three blocks come from the same template: N  x N x S=*=>anSdn=*=> anXYdn =*=>anajbjYdn =*=>anajbj ckbkdn aj bj ck dk = an+jbj ckbn+k (n+j) + k = j + (n+k)

  13. Example (III) – solution: PDA L = { aibjckdl: i,j,k,l=0,1,…; i+k=j+l }, • Main idea: • use X to record an a or c; use Y to record an b or d. • 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. c,#/X# b,X/e a,e/X d,X/e c,X/XX q1 q3 q4 q2 e,e/e e,e/e e,e/e e,#/e e,e/# b,#/Y# c,Y/e d,#/Y# q5 b,Y/YY d,Y/YY Example (III) – solution: PDA L = { aibjckdl: 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.

  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. For A: S  BaC For B: S  AaC | aC A  C For C: S  ABa | Ba | Aa | a A  B Remove ε-production Nullable variables: A, B, C Add Delete S ABaC | BaC | AaC | aC | ABa | Ba | Aa | a A  BC | C | B B  b |ε C  D |ε D  d B  ε C  ε • For i = 1 to k • For every production of the form A → Ni, • add another production A →  • If Ni →  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. For T T*F S  T*F For T F  a S  a For T F  (S) S  (S) For F  a S  a For F  (S) S  (S) Remove unit-productions Delete Add S  T S S+T | T | T*F | a | (S) T  T*F | F | a | (S) F  (S) | a T  F A1 → A2 → ... → Ak→ A1 delete it and replace everything with A1 Rule 1: A1 → A2 → ... → Ak→  Rule 2: A1 → ,... , Ak → 

  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. S  XAB; X  0 A  XD | YAD; Y  1 Convert CFG to Chomsky-Normal-Form (CNF) A → BcDE A → BX1 X1→ CX2 X2→ DE A → BCDE C → c break up sequences with new variables replace terminals with new variables C → c S  0AB A  0D | 1AD B  0 D  1

  22. S  XN1; N1  AB A  YN2; N2  AD Convert CFG to Chomsky-Normal-Form (CNF) A → BcDE A → BX1 X1→ CX2 X2→ DE A → BCDE C → c break up sequences with new variables replace terminals with new variables C → c S  XAB X  0 A  XD |YAD Y  1 B  0 D  1

  23. Convert CFG to Chomsky-Normal-Form (CNF) A → BcDE A → BX1 X1→ CX2 X2→ DE A → BCDE C → c break up sequences with new variables replace terminals with new variables C → c S  XN1 N1  AB A  XD A  YN2 N2  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. Cocke-Younger-Kasami (CYK) algorithm • Constructing the parse table • Input:w = a1a2…an ∈ Σ+ • Output: The parse table T for w such that tij contains A ⇔ A +⇒ aiai+1…ai+j-1 • Steps: • 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

  26. Cocke-Younger-Kasami (CYK) algorithm Cell (i,k), Cell (i+k, j-k) k = 1, …, j-1 x1 x2 x3 x4 x5

  27. Cocke-Younger-Kasami (CYK) algorithm • S → AA | AS | b • A → SA | AS | a Test: w = abaab a b a a b

  28. Cocke-Younger-Kasami (CYK) algorithm Trace back to find a parse tree • S → AA | AS | b • A → SA | AS | a Test: w = abaab a b a a b

  29. Cocke-Younger-Kasami (CYK) algorithm We can find another parse tree. • S → AA | AS | b • A → SA | AS | a Test: w = abaab a b a a b

  30. End of this tutorial!Thanks for coming!

More Related