7 . pushdown automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07am/chap07.pdf · 제7장....

23
7. Pushdown Automata 학습목표 CFLaccept하는 오토메타인 pda를 이해 npdadpda의 차이를 compiler의 측면에서 학습 CFL pda의 변환에 대한 연습 필요

Upload: lekien

Post on 23-Apr-2018

269 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

제 7장. Pushdown Automata

학습목표

CFL을 accept하는오토메타인 pda를이해npda와 dpda의차이를 compiler의측면에서학습

CFL pda의변환에대한연습필요

Page 2: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

개요

FA의한계 극도로제한된메모리

unlimited counting capability+ matching a sequence of symbols in reverse order

STACK!

- Nondeterministic pda

- CFL npda변환

- dpda와 deterministic CFL

- deterministic CFL를위한문법과파싱

Page 3: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

Nondeterministic Pushdown Automata

QFz

QqQQ

QFzqQM

⊆∈∈

Γ×→×∪×

=⋅

symbolstartstack:Γ

ofsubsetsfiniteΓ}){Σ(:alphabetstack:Γ

Σ

npda:),,,,ΓΣ,,(Def.

0

*

0

λδ

δ

)},{()0,,()},{()1,,()},{()1,,()}11,{()1,,()},{()0,,(

)},(),10,{()0,,()ex

32

22

21

11

30

310

λλδλδλδ

δλλδ

λδ

qqqbqqbqqaqqq

qqaq

======

λ -transition

L = {anbn : n>=0} + {a}

FA와비교하여기본정의를숙지할것!

Page 4: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

},),,,(),,(|{)( *0

* Γ∈∈Σ∈= uFpupzwqwML M λ

• (q, w, u) : instantaneous description of pda

• move

(q1, aw, bx) (q2, w, yx) iff (q2, y) ∈ δ (q1, a, b)

cf)

• Def. language accepted by M

state unread inputstack contents

irrelevant

Language Accepted by PDA

예1) L={w : na(w) = nb(w)}

예2) L={wwR}

a 0 push, 1 popb 0 pop, 1 push

middle?)}11,{()1,,()},{()1,,(

)},{()0,,()}00,{()0,,(

)}1,{(),,()}0,{(),,(

)},{(),,(

00

00

00

00

00

00

0

qbqqaq

qbqqaq

zqzbqzqzaq

zqzq f

==

==

==

=

δλδ

λδδ

δδ

λδ

)},{(),,(

)},{(),,()},{(),,(

)},{(),,()},{(),,(

)},{(),,()},{(),,()},{(),,()},{(),,()},{(),,()},{(),,(

21

11

11

10

10

00

00

00

00

00

00

zqzq

qbbqqaaq

bqbqaqaq

bzqzbqazqzaqbbqbbqabqbaqbaqabqaaqaaq

=

==

==

======

λδ

λδλδ

λδλδ

δδδδδδ

Page 5: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

- 4 (a), (d), (g), (j) : CFL npda구하기. 좀많은가?

- 14 : 모범답안을참고하면서…

NPDA : Exercises 7.1

Page 6: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

CFL npda

CFL npda

Basic idea : npda carrying out a leftmost derivation of strings

variables in right part → stack

terminals in left part → input read

i) start symbol → stack

ii) ∀A→ ax λxaA ⎯→⎯inputstack

Greibach NF

Page 7: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

CFL npda : 예1

bBbBAaaSAS →→→⇒ ,,|GNF

}),({),,()i 10 Szqzq =λδ

aaSbbS |)ex →

FSqqqQ },,{ 210=⇒

⎜⎜⎝

⎛==

=

}),({),,()},({),,(

)},(),,({),,()ii

11

11

111

λδδ

λδ

qBbqBqAbq

qSAqSaq

)},({),,()iii 21 λλδ qzq =

Page 8: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

CFL npda : 정리

VzqzqzVTqqqMMLLML

ff ∉∪==∋∃∀⋅

where}){,,,},{,}),,,({)pf)(npda,CFLThm.

010 δ

)},{(),,()iiiwhenever),,(),()ii

)},{(),,()i

1

11

10

zqzqauAAaquq

Szqzq

f=→∈

=

λδδ

λδ

),,(),,(),,(),,(

:inductionby

),,(),(:

)(anyaccepts:)()(

110

*

111111

2121

2121*

zqzqSzwqzwq

wS

AbqBBqBbBAAABbBaaa

AAAaaaS

GLwMMLGL

f

kk

mkn

mn

λλ

δ

∈→⇒⇒

∈⊆

LL

LLL

LL

end of derivation

stack

no. steps in derivation

Page 9: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

CFL npda : 증명

)},{(),,(add,if

,

),,(),,(

),,(),,(),,(),,(

),,(),,(

),,(),,()(:)()(

0

2111

32

2331221

21

132111

1211

11

0

zqzqL

aaaSuaS

uaAzuuaaqzAuaaq

AuuzuaaaqSzaaq

zqSzaaaqzqSzwq

uqzwqMLwGLML

f

n

nn

nn

n

f

=∈

⇒→

=

∈⊆

λδλ

λλ

λ

LL

LL

LL

L

let

stack contents = unmatched part of sentential form

Page 10: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

CFL npda : 예2

cCbB

abBaABCAaAS

→→→→

||)ex )},{(),,( 10 Szqzq =λδ

)},{(),,( 1 zqzq f=λδ

)},{(),,( 11 AqSaq =δ)},(),,{(),,( 111 λδ qABCqAaq =

)},{(),,()},{(),,()},{(),,(

11

11

11

λδλδ

δ

qCcqqBbqBqAbq

===

aaabcw =aASSzaaabcqzaaabcq ⇒),,(),,( 10

),,( 1 Azaabcq

aaABCABCzabcq ⇒),,( 1

aaaBCBCzbcq ⇒),,( 1

Page 11: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

CFL npda : 일반적인경우

abCxAabCxABxABxA

,withitreplace,stackfromremove:inputno,withitreplace,stackfromremove:

CFGgeneral)cf

→→

aaabCCzcq ⇒),,( 1

aaabczq ⇒),,( 1 λ

),,( zq f λ

Page 12: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

CFG npda : 기본아이디어

symbolstart:)(goesreads

stackfrom:erasesnpdaiff)(

ofgrammarfind1increase1decrease:stack

),(),(where},,,{),,()ii

emptyisstackiffstatefinalsingle)inpda:assumption

0

21

f

f

ji

ji

jiji

ni

zqqqq

vAvAqq

BCqcorqccccAaq

q

===

λδ L

stack contents = variable part of sentential form

processed input = terminal prefix of sentential formsentential form representing stack contents

internal state

②①

Page 13: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

CFG npda : 기본아이디어

)(variablestart

invaluespossibleallontake,where))(()(

)(stransitionoftypesdifferentgoesreadsremovesnpdaiff)(

0

0

*0

f

lk

klljki

ji

f

f

zqq

QqqCqqBqqaAqq

aAqq

qqwzwzqq

→−

⇒⇒

- Example 7.8 (7.7) : p. 191

Page 14: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

CFL npda : 정리

wzqq f*

0 )( ⇒

),(),,(),(),,(BCqAaq

qAaq

ji

ji

=

=

δ

λδ

sentential formleftmost qi : current state middle symbols : stack content

npdaCFL)ex

aBCAaA

→→

stateinternalcontentsstackformsententialforvariable+

=

lkklljki

ji

qqCqqBqqaAqqaAqq

,instatespossibleallwith))(()()(→

variablesuseless)cf

Page 15: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

CFG npda : 정리

uAqqXvqAXuvq

cqqVTPSTVGqzqQM

LMMLL

jiji

ji

f

*

0

)(),,(),,(

)}{(,with),,,(}){,,,,,,(

CFL:,npda:for)(ifThm.

⇒⇔

=Σ=

=⎯→⎯ΓΣ=

=⋅

δ

pf) Show by induction

Page 16: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

CFL npda : Exercises 7.2

3 : CFL npda변환연습

5 : Greibach NF을일단변환하고…

15 : npda CFG 변환연습

Page 17: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

Deterministic Pushdown Automata & DCFL

movepossibleonemostat

)movenextthedeterminesstackoftop(

Σforempty),,(emptynotis),,(.2onemostatcontains),,(.1Γ}{Σ,

}ΓΣ{Def.

transition

0

⎩⎨⎧

∈∀→

∈∪∈∈∀∋=⋅

Q

λδλδ

δcbcqbq

baqb,λaQq

Fz,,qδ,,,Q,M

Deterministic pda의핵심은각순간에선택의여지가없음을다시한번상기할것.

Page 18: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

dpda & DCFL

npdadpda197.)ex

),()0,,(),()1,,(),()1,,()11,()1,,(

)10,()0,,(:ex)

)(dpdaiffDCFL:Def.

02

22

21

11

10

=====

=∋∃⋅

pww

qqqbqqbqqaqqaqba

MLLML

R

nn

λλδλδλδ

δδ

이언어는대표적인 CFL이지만, dpda로는처리할수없음을이해합시다.

Page 19: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

dpda & DCFL

CFL:}0|{ˆCFLticdeterminis:if)pf ≥∪=→ ncbaLLL nnn

nistic?nondetermi}|{CFL:

CFL:}0|{

CFL:}0|{)ex

212121

22

1

SSSPPPLLLnbaL

nbaLnn

nn

→∪∪=∪=≥=

≥=

an bn

λ λ

bn

cn

M

CF)not:ˆbut(CFL:ˆ LL∴

nistricnondetermi:L∴

),,ˆ(),,ˆ( 1uqucq jn

i λ

),,(),,(),,( 12

0 uqubqzbaq jn

inn λ

),,(),,( 0 uqzbaq inn λ

좀헷갈리게정리되어있지만, 핵심은 L이 deterministic이라고가정하면이미알려진사실(anbncn이 not CFL)이위배됨을보인다.

Page 20: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

dpda & DCFL : Exercises 7.3

- 1 : example 7.8의아류문제

- 8 : 답은 deterministic인데…

Page 21: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

Grammars for DCFL

• no backtracking A→ ay

• parsing top-down

→ LL grammar : input scanned left → right

leftmost derivation

)2(|)ex LLabaSbS→

symbols1ofaheadlook:)( −− kkLL

DCFL은실제 PL에서널리사용하는언어이므로특히효율적인파싱이중요함

Page 22: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

)(not:||)ex kLLabaSbSSS→

SSSaabbabaSbSaabb

→→

LLaSbSS nearly:| λ→⇒

λ→→

SbaSbSSaabab

endor:

LLaSbSSaSbSS

:|0

stringempty

λ→→⇒

upbottomgrammarticdeterminisgeneralmore:grammar

−→ LR

Grammars for DCFL : 예

좀더 powerful하기는한데, 내용이 automata의범주를벗어나니컴파일러에서다룹시다

Page 23: 7 . Pushdown Automata - sclab.yonsei.ac.krsclab.yonsei.ac.kr/courses/07AM/chap07.pdf · 제7장. Pushdown Automata 학습목표 CFL을accept하는오토메타인pda를이해 npda와dpda의차이를compiler의측면에서학습

Grammars for DCFL : Exercise 7.4

- 8 : 생각만…

- 9 (a) : 생각만…