The Cocke-Younger-Kasami Algorithm

12
1 The Cocke-Younger-Kasami Al gorithm* Chung, Sei Kwang *Alfred Aho, Jeffrey Ullman 의 “ The Theory of Parsing, Translation, and Compiling” 의 의의의의 의의의의 의의의의의의의 .

Transcript of The Cocke-Younger-Kasami Algorithm

Page 1: The Cocke-Younger-Kasami Algorithm

1

The Cocke-Younger-Kasami Algorithm*

Chung, Sei Kwang

*Alfred Aho, Jeffrey Ullman 의 “ The Theory of Parsing, Translation, and Compiling” 과 인터넷을 참고하여 작성되었습니다 .

Page 2: The Cocke-Younger-Kasami Algorithm

2

Contents

Preliminaries Context Free Grammar Chomsky Normal Form Dynamic Programming

CYK algorithm Purpose of parsing Premise Constructing the parse table Left parsing from the parse table

Page 3: The Cocke-Younger-Kasami Algorithm

3

Preliminaries(1)

Context Free Grammar(1) Grammar

Notation ; G = (N, Σ, P, S) N ; a finite set of non-terminal symbols Σ ; a finite set of terminal symbols P ; a finite subset of

(N∪Σ)*N(N∪Σ)*×(N∪Σ)*@ Production : (α, β) ∈ P will be written α → β

S ; the start symbol in N

Page 4: The Cocke-Younger-Kasami Algorithm

4

Preliminaries(2)

Context Free Grammar(2) CFG

G ; if each production in P is of the form A → α ,

where A is in N and α is in (N∪Σ)*

Chomsky Normal Form Production can be 1 of 2 formats

A → α A → BC

@ e – production ; ex) 00A1 → 001 (∵A → e ∈ P )

Page 5: The Cocke-Younger-Kasami Algorithm

5

Preliminaries(3)

Dynamic Programming Optimal substructure

Solution of problem = Σ Solution of subproblem Overlapping subproblem

X = S1 + S2 S1 = T1 + T2 + T3 S2 = T2 + T3 + T4 T2, T3 overlapped

Recording solutions to reduce calculation Reuse the recorded solutions

Page 6: The Cocke-Younger-Kasami Algorithm

6

CYK algorithm(1)

Premise G = (N, Σ, P, S) ; a Chomsky normal form CFG

with no e-production The input string w = a1a2…an

Each ai ∈ Σ (1≤i ≤n) The element of the parse table, T ; tij

Purpose of parsing To determine whether string w is in L(G) Input string w is in L(G) ⇔ S is in t1n

Page 7: The Cocke-Younger-Kasami Algorithm

7

CYK algorithm(2)

Constructing the parse table(1) Input ; w = a1a2…an ∈ Σ+ Output ; The parse table T for w such that tij co

ntains A ⇔ A +⇒ aiai+1…ai+j-1

Method 1st, ti1 = {A|A→ai ∈ P, 1≤i≤n} 2nd, 1≤k<j, tij = {A|for some k, A→BC ∈ P, B is in tik,

C is in ti+k, j-k} 3rd, repeat 2nd step until 1≤i≤n, 1≤j≤n-i+1

Page 8: The Cocke-Younger-Kasami Algorithm

8

CYK algorithm(3)

Constructing the parse table(2) Example

Input string; abaab(n=5)

Productions; S→AA|AS|b A→SA|AS|a

Parse table →

5 A,S

4 A,S A,S

3 A,S S A,S

2 A,S A S A,S

1 A S A A S

j i

1 2 3 4 5

Page 9: The Cocke-Younger-Kasami Algorithm

9

CYK algorithm(4)

Left parsing from the parse table(1) Input ;

A Chomsky normal form CFG G = (N, Σ, P, S) Numbered productions Input string w The parse table

Output ; a left parse for w or the signal “error”

Page 10: The Cocke-Younger-Kasami Algorithm

10

CYK algorithm(5)

Left parsing from the parse table(2) Method ; A recursive routine gen(i,j,A); generat

e a left parse corresoding to the derivation A +⇒ aiai+1…ai+j-1

1st, if j = 1, the mth production in P is A→ai then output m

2nd, if j > 1, k(1≤k<j) is the smallest integer, A→BC ∈P then output m

Page 11: The Cocke-Younger-Kasami Algorithm

11

CYK algorithm(6)

Left parsing from the parse table(3) Example

Input ; w = abaab Numbered productions

1. S → AA 2. S → AS 3. S → b 4. A → SA 5. A → AS 6. A → a

Output ; 164356263

1: S → AA6: A → a4: A → SA3: S → b5: A → AS6: A → a2: S → AS6: A → a3: S → b

5 A,S

4 A,S A,S

3 A,S S A,S

2 A,S A S A,S

1 A S A A S

j i

1 2 3 4 5

Page 12: The Cocke-Younger-Kasami Algorithm

12

Thank you for listening.

경청해주셔서 감사합니다 .

설은 가족과 함께 행복하게 보내세요 .