30
Tutorial 05 -- CSC3130 : Formal Languages and Automata Theory Tu Shikui ([email protected]) SHB 905, Office hour: Thursday 2:30p m-3:30pm 2008-10-06
• date post

21-Dec-2015
• Category

## Documents

• view

239

0

### Transcript of Tutorial 05 -- CSC3130 : Formal Languages and Automata Theory Tu Shikui ( [email protected] ) SHB...

Tutorial 05-- CSC3130 : Formal Languages

and Automata Theory

Tu Shikui ([email protected])

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; Remove unit-production; Covert to Chomsky-Normal-Form (CNF)

Cocke-Younger-Kasami (CYK) algorithm

Relations

Context-free Languages L

Context-free Grammars G

Push-down Automata M

L = L(G)

L = L(M) L(G) = L(M)

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}

Example (I) --- solution: L(S)

S X | Y

X aXb | aX | a

Y aYb | Yb | b

Try to write some strings generated by it:

SXaXbaaXbbaaaXbbaaaabb

SYaYbaYbbaaYbbbaabbbb

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 = { aibj; i>j }

Lx = { aibj; i<j }

L(S) =

{ aibj; i≠j }

Example (I) --- solution: PDA

S X | Y

X aXb | aX | a

Y aYb | Yb | b

L(S) = { aibj; i≠j }

PDA = NFA + a stack (infinite memory)

= { aibj; i>j } U { aibj; i<j }

A possible way: “divide and conquer”

Lx = { aibj; i>j }

a,/A

q0,/# ,/

q1

b,A/

b,#/#q2

b,#/#

q3

,#/

LY = { aibj; i<j }

q’0,/# ,/

q’1,A/

q’2

,A/

q’3

,#/

a,/A b,A/

, /

Combine both …

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,…}, = {0, 1}

Example (II) -- solution: CFG

L = {0i1j: i ≤ j ≤ 2i, i=0,1,…}, = {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 ε

If i ≤ j ≤ 2i , then randomly choose “red-rule” or “blue-rule” in the generation.

“red-rule” “blue-rule”

S 0S1

S 0S11

S ε

Example (II) -- solution: CFG

L = {0i1j: 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 = 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

Example (II) -- solution: PDA

L = {0i1j: i ≤ j ≤ 2i, i=0,1,…}, = {0, 1}

Similar idea: “randomly choose two extreme cases”

q0

,/#

0,/X,/ q1

q2

,#/1,X/

1,X/X 1,X/q3

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}

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 w

an dn

ajbj ckdk

aj bj ck dk

Three blocks come from the same template:

N x N x

S aSd | XY

X aXb | ε

Y cYd | ε

S=*=>anSdn=*=> anXYdn

=*=>anajbjYdn

=*=>anajbj ckbkdn

= an+jbj ckbn+k

(n+j) + k = j + (n+k)

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?

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.

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.

,/# q5q1

a,/X

,/

b,#/Y#

q2,/

c,X/XX

q3,/ q4

,/

b,X/

b,Y/YY

c,#/X#

c,Y/

d,X/

d,#/Y#

d,Y/YY

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

Remove ε-production

Example:

Remove ε-productions of the following grammar G:

S ABaCA BCB b |εC D |εD d

Remove ε-production

S ABaC | BaC | AaC | aC | ABa | Ba | Aa | aA BC | C | BB b |εC D |εD d

Nullable variables: A, B, C

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 → Ni,

add another production A → If Ni → is a production, remove it

Remove unit-productions

Example:

Remove the unit-productions of the following grammar:

S S + T | T

T T * F | F

F (S) | a

Remove unit-productions

For T T*F S T*FFor 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 aFor F (S) S (S)

A1 → A2 → ... → Ak → A1

A1 → A2 → ... → Ak → A1 → ,... , Ak →

delete it and replace everything with A1

Rule 1:Rule 2:

Convert CFG to Chomsky-Normal-Form (CNF) Example:

Convert the following CFG to CNF:

S 0AB

A 0D | 1AD

B 0

D 1

Convert CFG to Chomsky-Normal-Form (CNF)

A → BcDEreplace terminalswith new variables

A → BCDEC → c

break upsequenceswith new variables

A → BX1

X1 → CX2

X2 → DEC → c

S 0AB

A 0D | 1AD

B 0

D 1

S XAB; X 0

A XD | YAD; Y 1

Convert CFG to Chomsky-Normal-Form (CNF)

A → BcDEreplace terminalswith new variables

A → BCDEC → c

break upsequenceswith new variables

A → BX1

X1 → CX2

X2 → DEC → c

S XAB

X 0

Y 1

B 0

D 1

S XN1; N1 AB

A YN2; N2 AD

Convert CFG to Chomsky-Normal-Form (CNF)

A → BcDEreplace terminalswith new variables

A → BCDEC → c

break upsequenceswith new variables

A → BX1

X1 → CX2

X2 → DEC → c

S XN1

N1 AB

A XD

A YN2

X 0

Y 1

B 0

D 1

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

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 + a⇒ iai+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

Cocke-Younger-Kasami (CYK) algorithm

j=5 cell11, cell24

cell12, cell33

cell13, cell42

cell14, cell51

j=4 cell11, cell23

cell12, cell32

cell13, cell41

cell21, cell33

cell22, cell42

cell23, cell51

j=3 cell11, cell22

cell12, cell31

cell21, cell32

cell22, cell41

cell31, cell42

cell32, cell51

j=2 cell11, cell21 cell21, cell31 cell31, cell41 cell41, cell51

j=1 A1 x1 A2 x2 A3 x3 A4 x4 A5 x5

i=1 i=2 i=3 i=4 i=5x1 x2 x3 x4 x5

Cell (i,k), Cell (i+k, j-k)k = 1, …, j-1

Cocke-Younger-Kasami (CYK) algorithm

j=5 cell11, cell24: S, A

cell12, cell33: S, A

cell13, cell42: S, A

cell14, cell51: S, A

j=4 cell11, cell23: S, A

cell12, cell32: S, A

cell13, cell41: S, A

cell21, cell33: A

cell22, cell42: S, A

cell23, cell51: --

j=3 cell11, cell22 : S

cell12, cell31: A, S

cell21, cell32: --

cell22, cell41: S

cell31,cell42:S,A

cell32, cell51: --

j=2 cell11, cell21: S, A cell21, cell31: A cell31, cell41: S cell41, cell51:

S,A

j=1 A1 x1: A A2 x2 : S A3 x3 : A A4 x4 :A A5 x5 :S

i=1 i=2 i=3 i=4 i=5a b a a b

S → AA | AS | bA → SA | AS | a

Test: w = abaab

Cocke-Younger-Kasami (CYK) algorithm

j=5 cell11, cell24: S, A

cell12, cell33: S, A

cell13, cell42: S, A

cell14, cell51: S, A

j=4 cell11, cell23: S, A

cell12, cell32: S, A

cell13, cell41: S, A

cell21, cell33: A

cell22, cell42: S, A

cell23, cell51: --

j=3 cell11, cell22 : S

cell12, cell31: A, S

cell21, cell32: --

cell22, cell41: S

cell31,cell42:S,A

cell32, cell51: --

j=2 cell11, cell21: S, A cell21, cell31: A cell31, cell41: S cell41, cell51:

S,A

j=1 A1 x1: A A2 x2 : S A3 x3 : A A4 x4 :A A5 x5 :S

i=1 i=2 i=3 i=4 i=5a b a a b

S → AA | AS | bA → SA | AS | a

Test: w = abaab

Trace back to find a parse tree

Cocke-Younger-Kasami (CYK) algorithm

j=5 cell11, cell24: S, A

cell12, cell33: S, A

cell13, cell42: S, A

cell14, cell51: S, A

j=4 cell11, cell23: S, A

cell12, cell32: S, A

cell13, cell41: S, A

cell21, cell33: A

cell22, cell42: S, A

cell23, cell51: --

j=3 cell11, cell22 : S

cell12, cell31: A, S

cell21, cell32: --

cell22, cell41: S

cell31,cell42:S,A

cell32, cell51: --

j=2 cell11, cell21: S, A cell21, cell31: A cell31, cell41: S cell41, cell51:

S,A

j=1 A1 x1: A A2 x2 : S A3 x3 : A A4 x4: A A5 x5 :S

i=1 i=2 i=3 i=4 i=5a b a a b

S → AA | AS | bA → SA | AS | a

Test: w = abaab

We can find another parse tree.

End of this tutorial!Thanks for coming!