5 . context-free languages - yonsei universitysclab.yonsei.ac.kr/courses/07am/chap05.pdf ·...
TRANSCRIPT
학습목표
정규언어의한계를극복할수있는 Context-Free 언어를Context-Free Grammar를통해이해하고 Parsing의원리
숙지
제 5장. Context-Free Languages
개요
• all languages ≠ regular
예)
• CF언어와 CF문법
–정의
– Parsing : see if a given string is derivable from a given grammar
–응용 : PL design, efficient compilers construction
LLnbaL nn
∉∈→≥=
)(()))((()),((}0:{
PL requires something beyond RL
nested structure
정규언어보다복잡한언어의필요성과처리방법숙지할수있는일이많아진만큼모호성/어려움증가
형식언어중에서가장응용이활발히진행되는 language category
Context-Free Grammars
• Restrictions in RG
left side : single variable
right side : special form CFG
CFLRLcf))(,where
aresproductionallifFreeContext:),,,(:1
⊂∪∈∈
→−=
∗TVxVAxA
PSTVG정의
⎯⎯⎯ →⎯relaxing
①
②
RG와무제약문법사이에서제약을완화시키며능력개선!
proper subset
왜 context-free라하나?sentential form에나타난변수는상황에관계없이 rewrite가능
=
→→→==
)(:derivation
},,{with),},,{},({CFG:1
GL
SbSbSaSaSPPSbaS λ예
aabbaaaabSbaaaaSaaaSaS ⇒⇒⇒⇒}},{:{ *bawwwR ∈
=→→→→=
)(CFG:},,,{with:2
GLAbbAaBaaBbAabBSPG λ예
}0:)()({ ≥nbabbabbaaab nn
CFG : 예
context-free이자 linear
RG < linear G < CFG
CFG가생성하는언어의예
CFG : 예예
?freecontextis}:{:3 −≠= mnbaL mn예
⎩⎨⎧
→→→→
∴
<
→→→>
bbBBaaAAbaSSBSASS
mniibaSS
aaAAASSmni
|,||,|
reasoningsimilarusecan:)|
|,:)
1111
11
1
λ
λ
:}||{with:4 λSSaSbSPG →=예
grammarfreecontext −→
linearnot but CF
)()()(),)(().}ofprefixanyis where)()(),()(:},{{ *
gewvvnvnwnwnbawL baba ≥=∈=
조금복잡한경우를볼까요?
set of all properly nested parenthesis structures for PL
• CFG : a derivation may involve sentential forms with more than one variable
choice in the order where variables are replaced
=
→→→→
→
==
)(
.5.3.4.2
.1
with),},,{},,,({)
GL
BABbBaaAA
ABS
PPSbaSBAG
λλ
예
}0,0:{ 2 ≥≥ mnba mn
orderSpecific35241
54321
⎪⎭
⎪⎬⎫
⇒⇒⇒⇒⇒
⇒⇒⇒⇒⇒
aabaaAbaaABbABbABS
aabaaBbaaBaaABABS
CFG : Derivation (1)
뭔가약속된순서가있어야겠군!
CFG : Derivation (2)
정의 2: • leftmost derivation if leftmost variable is first replaced
• rightmost derivation if rightmost variable is first replaced
λ| ,,: ABbBbAaABS →→→예
abbbbabbbbBabbBbbBabBbBaABS ⇒⇒⇒⇒⇒:leftmost
abbbbabbBbbabBbaAaABS ⇒⇒⇒⇒⇒:rightmost
그래서순서를정한것이…
그런데 2차원구조를 1차원적으로보이자니감이잘안오는군!
• Derivation tree
– ordered tree where nodes are labeled with the left sides of productions and where the children of a node represent its corresponding right side
A
Bba cA
CFG : Derivation Trees (1)트리구조로봅시다!
abABcA→)예
CFG : Derivation Trees (2)
= partial derivation tree
• yield : reading leaves from left→right, omitting λdepth-first manner
+
}{leafevery)2
λ∪∪
′
TV siblingsnohasleaf)5:,,children,vertex)4
:vertexinteriorevery)3}{:leafevery)2
:root)1treederivationCFG:),,,(3
1
λ
λ
naaVAV
TS
PSTVG
L∈
∪
=정의
naaaAP
L21
containmust→
}| ,,{with: λABbBbAaABSPG →→→=예
그림 5.2 (partial derivation tree), 그림 5.3 (derivation tree)
CFG : Sentential Forms & Derivation Trees
• Relation btw. sentential forms & derivation trees
.offormsententialisofyieldtreederivationpartial:
.isyieldwhoseoftreederivation)(:),,,(:Thm.
Gtt
wGGLwCFGPSTVG
GG →∃∈∀
↓↑=
CFG : Homework (Exercises 5.1)
• 7 (b), (d) : 주어진언어의 CFG 구하기연습-1
• 8 (b), (d) : 주어진언어의 CFG 구하기연습-2
• 11 (10) : CFG 구하기문제하나더
• 15 (14) : 또다른 CFG 구하기문제
• 23 (22) : 메타언어의 CFG는어떨지…
Parsing and Ambiguity
• Grammar usage
– generative aspect : derivable strings from grammar
– analytical aspect : whether w∈L(G)? → derivation of w
cf) parsing : finding a sequence of productions by which w∈L(G) is derived
• Exhaustive search parsing method (top-down parsing)
– systematically construct all possible derivations
– see whether any of them match w
생성기로서의문법이아닌Accepter로서의문법의이용
좀무식해보이지만…
Parsing : 예
SbSaSaSbSSSSSSS
⇒⇒⇒⇒⇒)1
abSabaabbaaSbb
aSSbaSbS
⇒⇒⇒
⇒⇒)2
abbSaS ⇒⇒× )3
λ⇒× S)4
aabbwbSaaSbSSS =→ ,|||ex) λ
Parsing : 효율적인방법
• flaws
– tedious
– never terminates for strings not in L(G) : nontermination
→ restriction on the grammar
i) A → λ : decrease length of successive sentential forms
ii) A → Bpower와무관
rounds||in terminatesESP w
baabbSaaSbSSS ||||) →예
Parsing : 효율적인방법증명
rounds||2||||
symbolsterminalofnumber/formsententialoflengthincreasesstepeach)possible.isparsingnoorofparsingaproduceeithercanESPThen
,withoutCFG:),,,(Thm.
www
pfw
BAAPSTVG
⇒
→→= λ
required are methodsparsingefficientpracticalnot|||||| ||22
→−+++= wPPPM L
• Upper bounds on total no. of sentential forms
casesspecialimportant,but,restrictedfor||:?algorithmparsingtimelinear
||in)(anyparsesthatalgorithmanCFGThm. 3
wwGLw
→∈∃∀
PaAVxTaVAaxA
ifPSTV
inoncemostatoccurs),(,,wherearesproductionitsall
grammar)(sgrammarsimple:),,,(CFG:∗∈∈∈→
−=정의
grammarsbydescribedbecanPLoffeaturesmanycf):|||
:||)
−→→
caSSbSSaSScbSSaSS예
• G: s-grammar → w∈L(G) can be parsed in |w|
steps||thanmoreno22121211
21
wAABBaaAAAaS
aaaw
mm
n
LLL
L
⇒⇒=
grammars −×
Parsing : Simple Grammar
문법에약간의제약을가하니엄청나게효율적인파싱이가능해지는군!
• Ambiguity : several different derivation trees may exist
treesderivationdistinct2leastathasthat)( ambiguous:CFG:
GLwifG
∈∃정의
)4.5 (ambiguous:||) 그림예 λSSaSbS →
Parsing : Ambiguity
똑같은 aabb를생성하는두개의 derivation tree가능
Tip!형식언어에서 string(즉, 문장)의구조는 string을생성하기위하여적용되는 rule과그순서에따라결정된다. 따라서어떤 string x를 여러가지방법으로생성할수있다면, 이는 x의구조를여러가지로해석할수있음을뜻한다.
Parsing : Ambiguity의해결
G
cbacbaIEEEEEEIEP
cbaTIEVPETVG
′
⎜⎜⎝
⎛⇒
∗+⇒
→∗+→→=∗+=
==
parsingoneonlythatsogrammartherewriteorrulePrecedence5.5) (:ambiguous
}||),(||,{})(,,,,,,{
},{with),,,()
그림
예
• Rewriting grammar in an equivalent unambiguous form
예) PL’s statement has only one interpretation.
}||),(,,,,,{
}{
cbaIEFFTTTEEIFFTTEP
IF,T,E,V
→→∗→+→→→→=
=
(그림 5.6)
CFG의 ambiguity, equality no general algorithm
Parsing : Inherent Ambiguity
21
222
111
21
|||
||
bygeneratedis where
:}{}{)
SSSLbBcB
BaSSL
aAbAAcSS
L
LLLcbacbaL mmnmnn
→
⎜⎜⎝
⎛→→
⎜⎜⎝
⎛→→
∪=∪=
λ
λ
예
ambiguousinherently:ambiguousisgeneratesthatgrammareverysunambiguou:grammarsunambiguouan,CFL::
LLLL
→→∃정의
21 orsderivationdistincttwohas:ambiguous
SSSScba nnn
⇒⇒Tip! 임의의 CFG가 ambiguous한지여부를밝히거나, 임의의 ambiguous 한CFG를 unambiguous 한 CFG로 전환하는일반화된 algorithm은없다. 그러나특정한주어진 CFG 가 ambiguous 한지여부를밝히거나, 이를 unambiguous 한 CFG로전환하는것은가능하다.
Parsing : Ambiguity 해결연습
G3 : S → bS⏐Sb⏐c
1) 언어는? {bicbj | i, j ≥ 0}
2) Ambiguous한가?
이언어의 string은 S → bS를먼저적용하여좌측에있는 b를생성한다음, 우측의 b를생성하거나, 이와역순으로생성할수있다. 따라서 ambiguous 하다.
SbS
b S
c
Sb S
bS
c
3) 모호성을제거하는방법은?
좌측의 b를먼저생성한다음우측의 b를생성하도록 rule을수정
Ambiguous G3 : S → bS⏐Sb⏐cUnambiguous G4 : S → bS⏐A A → Ab⏐c
Parsing : Homework (Exercises 5.2)
• 1 : 주어진언어의 s-grammar 찾기기초문제
• 13 : 언어의 ambiguity 보이기문제
CFG와 PL
• application of formal language theory
– PL definition / interpreter, compiler construction
• convention for specifying grammars for PL : Backus-Naur Form
예) <expression> ::= <term> | <expression> + <term>
<term> ::= <factor> | <term> * <factor>
예) s-grammar : easily / efficiently parsed (due to keywords)
<if_stmt> ::= if <expression> <then_clause> < else_cause>
→ LL / LR grammars
• Syntax vs. Semantics
CFG와 PL: Homework (Exercises 5.3)
•이장의내용은따로연습까지할만큼깊이있게다루지않았지만…
•굳이한번해보자면한두문제를선택해서풀어볼것.
다음이야기 CFG의변환과정규화
•문법을변환하는방법들
• Useful substitution rule
• Removing λ-productions / unit-productions / useless productions
•정규형식
• Chomsky normal form : 우측항의변수의수제한
• Greibach normal form : 우측항의변수와 terminal의위치제한
• CFG를위한간단한 Parsing방법 : CYK algorithm
• membership algorithm : 주어진 string이문법에맞는지?