제6장. Simplification of Context-Free Grammars and Normal...

27
6. Simplification of Context-Free Grammars and Normal Forms 학습목표 CFG의 변환을 통한 단순화 정규화 이해 단순 변환 테크닉의 연마 필요

Transcript of 제6장. Simplification of Context-Free Grammars and Normal...

Page 1: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

제 6장. Simplification of Context-Free Grammars and Normal Forms

학습목표

CFG의변환을통한단순화및정규화이해단순변환테크닉의연마필요

Page 2: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

개요

}|{ˆ),,,(}{

ˆ},{ˆ)ˆ,ˆ,,ˆ(ˆ,:

language free -·

0

00

λλ

λ

SSPPPSTVGL

SSSVVPSTVGCFLL

→∪==−

=∪==

* 문법을변환하는방법들- Useful substitution rule- Removing λ-productions / unit-productions / useless productions

* 정규형식- Chomsky normal form : 우측항의변수의수제한- Greibach normal form : 우측항의변수와 terminal의위치제한

* CFG를위한간단한 Parsing방법 : CYK algorithm- membership algorithm : 주어진 string이문법에맞는지?

Production의우측에허용된자유를약간희생하여큰효과를보자는얘기!

L-{λ}로얻어진결론이쉽게 L로적용될수있다!

Page 3: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Methods for Transforming Grammars① A Useful Substitution Rule

)()ˆ(

|||add)( delete

ˆwith)ˆ,,,(ˆ)(|||,

with:),,,(6.1 Thm

21221211

21

2121

GLGL

xyxxyxxyxAxBxA

PPSTVG

BAyyyBBxxAPCFGPSTVG

n

n

=⇒

⎜⎜⎝

⎛→

∗→

=

≠→→=

L

L

간단하지만한번증명해볼까요?

simplification = removal of certain typesof undesirable productions수의감소와는무관!

Page 4: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Substitution Rule : 증명

)ˆ()(

applied is production the timesof no.on induction by

:

(*) involves this))(involvenotdoesthis)

such that)(.

Similarly

ˆ*

2211ˆ21ˆ*

2211221121*

ˆ*

*

GLwGLw

wS

uxyxuAuuSG

uxyxuuBxxuAuuSG

iiwSi

wSGLwpf

G

jGG

jGGG

G

G

∈←→∈∴

⇒⇒

⇒⇒⇒

⇒⇒∗

⇒∈

무지간단하죠! 그럼예를한번들어봅시다.

Page 5: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Substitution Rule : 예

⎜⎜⎝

⎛→→

babbABabbcababbAcaaAaA

||||

⇒⎜⎜⎝

⎛→→

babbABabBcaaAaA

|||)예

⎜⎜⎝

⎛or

?B

aaabbc

sproductionuseless→

aaabbcaaabBcaaAA ⇒⇒⇒

aaabbcaaAA ⇒⇒

이런건그냥없애버려도되겠죠?

Page 6: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Methods for Transforming Grammars

useless)*)(,with

such that)( oneleastatuseful::),,,(

1.6 ·

**

cfTVyxwxAyS

GLwiffVACFGPSTVG

∪∈⇒⇒

∈∃∈=

정의

aAAAaSbS

→→ ||) λ예

symbolstart fromed not reachQ

② Removing Useless Productions

string terminalnoQ bABaAAAS

→→→

λ|

어떤 derivation에도공헌하지못하는production의제거

Page 7: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Useless Productions : 예

CSB, A, not but

⎜⎜⎜⎜⎜

→→→→

aCbCaaBaA

CAaSS ||)ex

aAAaSSP

aTASVPSTVG

→→=

===∴

,|ˆ},{ˆ},,{ˆwith)ˆ,,ˆ,ˆ(ˆ

⎜⎜⎜

→→→

aaBaAaS|AS

string terminala lead tocan that variables)i

startfrom reached cannot be that variablesii)graph dependency cf)

xDyCiff →C D

S A B

조금복잡한경우에는그래프를그려보자!

그냥넘어갈순없으니간단히증명

Page 8: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Useless Productions : 증명

ns productio /s variablessany uselewithout )ˆ,,ˆ,ˆ(ˆ:),,,(

2.6Thm·

PSTVG

CFGPSTVG

=∃

=

ons producti /s variable theremovefrom reached cannot be that s variableallfind

for graph dependency abledraw vari : ˆ) 11

SGGGii →

)(in are symbolssuch that)3 to add

with production hasV toadded s variablemore no untilrepeat )2

)1

such that}{such that),,,(:ˆ)

11

1

1i21

1

1

1

**1111

TVPPVA

TVxxxxAPVA

VGG

TwAAVPSTVGGGipf

n

∪←→

∪∈→∋∈∀

←→

∈⇒==→

L

φ

Page 9: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Methods for Transforming Grammars③ Removing λ-Productions

λ|,) 111 baSSbaSS →→예

⎜⎜

−→

nullable:such that

production:

6.2 ·

* λ

λλ

AA

A

정의

no λ

⎜⎜⎝

⎛→→

→abbaSSabbaSS

||

11

1

}1|{ ≥= nbaL nn

또, 증명을해놔야안심!

Page 10: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

λ-Productions : 증명

nscombinatio possible allin with variables nullable replacingby thoseall as wellas ˆ put to

,1,· into put in where

toadded are ariablesfurther v no untilrepeat )2 intoA put ,1)

s variablenullable all·pf)

sproduction- no having ˆ equivalent

)(innotwith:

6.3Thm·

21

121

λ

λ

λ

λ

PP

TVxmxxxAVBVAAAAAB

VVA

V

G

GLCFGG

im

NNnn

N

N

N

∪∈≥→∀→→∀

→∀

L

LL

조금복잡한예를가지고작동과정을이해해봅시다

Page 11: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

λ-Productions : 예

⎜⎜⎜⎜⎜⎜⎜⎜

→→→→

dDDCbB

BCCBAaBaAaaCABa

AaCBaCABaCS

|||||||||

⎜⎜⎜⎜⎜⎜

→→→→→

dDDCbBBCAABaCS

λλ|

|

)예

CBA ,,s variablenullable

자, 이제마지막고개를넘어봅니다.

Page 12: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Methods for Transforming Grammars④ Removing Unit-Productions

sproduction-unitany without )ˆ,,ˆ,ˆ(ˆequivalent

sproduction-without :),,,(

6.4Thm·

production-unit :,,6.3 ·

PSTVG

CFGPSTVG

VBABA

=∃

=

∈→

λ

정의

nn yyyByyyAPBA

PP

BABABA

AA

||| where||| .ˆ toadd ,

of sproduction-unit-non all ˆ intoput

graph dependency with such that find : ·

removedobviously : ·pf)

2121

*

LL →→∀

⇒∀→

증명도중요하지만예를들어이해합시다.

Page 13: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

⎜⎜⎜

→→→

∴⇒bcbbaBbcbbaA

AabbbcaS

||||

|||

Unit-Productions : 예

⎜⎜⎜

→→→

bbBbcaA

AaS|

⎜⎜⎜

→→→

BbcaAbbAB

BAaS

||||)예

S BA

useless⎜⎜⎜

→→→

bcaBbbA

bbbcaS

|

||+

Page 14: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

문법의변환 : 총정리

sproduction useless 3)sproduction-unit 2)

sproduction- 1)process removal of sequence·

sproduction-unitor s,production- s,production uselessany havenot does & L generatessuch that

without :

6.5Thm

λ

λ

λCFG

CFLL∃

순서가중요함!!

Page 15: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

문법의변환 : Homework (Exercises 6.1)

- 6 : useless production의제거연습

- 8 : 문법의변환총정리연습

- 24 (23) : 응용문제

Page 16: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Normal Forms

① Chomsky NF : restriction on length of right sides= no more than 2 symbols

bSAAaASS

| |

→→

6.7 Examplep.165)

,NFChomsky:·

정의

aABCACFG

→→

aaSAAAASASS

| |

→→

CNF

No CNF

Page 17: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Chomsky Normal FormCNFin )ˆ,,ˆ,ˆ(ˆ equivalent),(with ),,,(Thm· PSTVGGLPSTVCFG =∃∉=∀ λ

aBaxBCVxxCCCCA

TaBnPaAxn

TVxxxxAPSTVGGipf

aiai

iiin

a

in

→⎜⎜⎝

⎛==∈=→

∈∀≥−→⎯→⎯==−

∪∈→>=→

· if if ·

:2 toput terminal:1

,1length whoses terminalall remove:),,,())

21

11

21

111

L

L

6.8 Example )

?)()ˆ(·

2

ˆ intoput 2

2 toside-right oflength reduce:)ˆ,,ˆ,ˆ(ˆ)

12

221

11

1

GLGL

DCD

DCDDCAn

Pn

PSTVGGii

nnn

=

→→>−

⎯→⎯=−

=→

−−

M

Page 18: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Chomsky Normal Form: 예

Rule A → aBCDbE가 있다고 하자. 이 rule의 우측에 있는 string 을생성하는일련의 CNF rule을다음과같이만든다. A → A1B1 A1 → a // B1은 아래와 같이BCDbE를 derive 한다.B1 → BC1 // C1은 CDbE를 derive 하도록한다.C1 → CD1 // D1은 DbE를 derive 하도록한다.D1 → DE1 // E1은 bE를 derive 하도록한다.E1 → F1E F1 → b //마지막으로 E1은 bE를 derive 하도록한다.

Page 19: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Chomsky Normal Form: 예예

S → AaBCb A → abb B → aC C → aCb | ac예:

S → AA1 A1 → A2A3 A2 → a A3 → BA4 A4 → CA5 A5 → b

A → B1B2 B1→ a B2 → B3B4 B3 → b B4 → b

B → C1C C1 → a

C → D1D2 | E1E2 D1 → a D2 → CD3 D3 → b E1 → a E2 → c

Page 20: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Greibach Normal Form

② Greibach NF : restrictions on positions of T and V

10.6,9.6 Examples)),(

),NFGreibach:

정의

aAgrammarscfVxTaax,A

CFG−∈∈→

⋅∗

책을한번봅시다

Page 21: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Greibach Normal Form: 변환규칙S t e p 1 . 다음 예와 같이 새로운 n o n t e r m i n a l기호 (예에서 N )를도입하여다음과같이 front-recursion rule을다른 rule로바꾼다.

A → CDN | CD N → BN | B A → AB | CD 예:

Step 2. Rule의우측이 nonterminal, 예를들면 B로시작하고, B rule이 terminal 기호로 시작하는 경우를 찾아 그 r u l e의 B를 B r u l e의 우측

string으로대치한다.

예: A → BbC | aA

B → cDA | aE

. . . .

A → cDAbC | aEbC | aA

B → cDA | aE

. . . .

Step 3. Rule의 우측 첫 번째에 위치하지 않은 terminal기호는 다음과

같은방법으로 새로운 nonterminal기호로바꾼다.

예: A → cDAbC A → cDANC N → b

Page 22: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Greibach Normal Form: 예

S → SA | BeA

A → AS | a

B → b

S → BeAC | BeA C → AC | AA→ aD | a D → SD | SB → b

Step 1:

S → beAC | beA C → aDC | aC | aD | aA → aD | a D → SD | SB → b

Step 2:

S → beAC | beA C → aDC | aC | aD | aA → aD | a D → beACD | beAD | beAC | beAB → b

Step 2:

CFG:

S → bEAC | bEA C → aDC | aC | aD | aA → aD | a D → bEACD | bEAD | bEAC | bEAB → b E → e

Step 3:

Page 23: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Normal Forms : Homework (Exercises 6.2)

4 : Chomsky normal form으로의변환연습

10 : Greibach normal form으로의변환연습

Page 24: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

Membership Algorithm for CFG

}:{

where)( 1

ijij

jiij

n

wAVAV

aawVSiffGLw

⇒∈=

=∈∈

L

• CYK algorithm: O(|w|3) for membership and parsing

– originators: J. Cocke, D. H. Younger, T. Kasami

– grammar in Chomsky normal form

– basic idea: breaking problem into a sequence of smaller ones

• Compute Vij

– Compute Vii :

– Compute Vij :

},with,:{,

with

contains

,1}1,,1,{

,1

jkikjiik

ij

jkik

ij

iii

VCVBBCAAVwCwB

BCAiffwA

aAGiffVA

+−+∈

+

∗∗

∈∈→=∴

⇒⇒

→∃⇒

→∈

LU

Page 25: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

bABBaabbbwaBBA

ABS

|?|

)

→=→

→예

}{}{}{}{}{)1

5544332211

5544332211

BVBVBVAVAVbwbwbwawaw

==========

}{},{},,:{)2

2211

221112

AVAVVCVBBCAAV

===∈∈→=

Q

φ

},,{},,{},{

},,{},{},,{}{},{

15

2514

352413

4534

BSVBSVAV

BSVAVBSVAVAV

===

=====

)(GLw∈∴

AA→ ?

AB→ ?}{},{},{},,:{

3322

332223

BVAVBSVCVBBCAAV

===∈∈→=

Q

Page 26: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

CYK 알고리즘의계산량

•계산량

)(termsmostatofsets2

)1( 3nnVnnij Ο=×

+

Page 27: 제6장. Simplification of Context-Free Grammars and Normal …sclab.yonsei.ac.kr/courses/07AM/chap06.pdf · 2007-04-12 · Microsoft PowerPoint - chap06.ppt Author: mycom Created

CYK 알고리즘 : Homework (Exercises 6.3)

• 1 : 좀지루하지만한번해봅시다