Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 :...
-
date post
19-Dec-2015 -
Category
Documents
-
view
224 -
download
0
Transcript of Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 :...
Chap 2
Context-Free Languages
Context-free Grammars is not regular Context-free grammar :
eg. G1:
A0A1 substitution rulesAB production rulesB#
A,B : variables0,1,# : terminals
}0 : 10{ nnn
Derivation:generate 000#111
Parse tree:
#111000 B111000 111A000 00A11 0A1 A
}0:1 #0{)( 1 nGL nnA
0 0 0 1 1 1
A
#
B
A
A
Def: (context-free grammar)A context-free grammar is a 4-tuple (V,,R,S)1. V: a finite set called the variables2. : a finite set, disjoint from V, called the terminals3. R: a finite set of rules4. S: start symbol
A Language of the grammar is }:{ ** wSw
Eg :
Eg :
ε|SS|aSbS :R
S)R,b},{a,({S},G
rules
) , ( , , a,
FACTOR,TERM,EXPR V
EXPRR,,V, G
a EXPRFACTOR
FACTOR FACTORTERMTERM
TERM TERMEXPREXPR :R
aaa )(
< E X P R >
< T E R M >
< F AC T O R >
< T E R M >
< E X P R >
< F AC T O R >
< T E R M >
< E X P R >
< F AC T O R >
< F AC T O R >
< T E R M >
( a + a ) X a
Def: A string w is derived ambiguously in
context-free grammar G if it has 2 or more leftmost derivations
Grammar G is ambiguous if it generates some string ambiguously
A language that can only be generated by ambiguous grammars is called inherently ambiguouseg:
}or 210{ kjjikji
Chomsky Normal FormEvery rule is of the form
a : terminalA,B,C : any variablesB,C may not be the start variableallow
aA
BCA
S
Thm: Any context-free language is generated by a context-free grammar in Chomsky Normal Form
Proof:1. Add a new start symbol
: original start symbol2. Remove -rule A, A is NOT the start symbol
If is a rule, then add If is a rule, then add
Do so for each occurrence of A If is a rule, we add unless it had
been removed
Repeat these steps until all rules not involving the start variable
0S
S
uAvR
R
uAvAwRuvwRuAvwRuvAwR and , ,
AR
uvR
Proof cont.:3. Handle all unit rules
Remove a unit rule For , we add unless it was a removed unit rule. Repeat until all unit rules are removed
4. Replace with Ai : new variables
ui : variable or terminal symbol
If ui is terminal, add Uiui ■
BAuB uA
u is a string of variables and terminals
3 , 21 kuuuA kkkk uuAAuAAuA 1222111 , , ,
Eg. Convert the following CFG to Chomsky Normal Form
◆
◆Remove B→ ◆ Remove A→
S→ASA|aBA→B|SB→b|
S0→S
S→ASA|aBA→B|SB→b|
S0→S
S→ASA|aB|aA→B|S|B→b
S0→S
S→ASA|aB|a|SA|AS|SA→B|SB→b
Eg conti.◆Remove unit rule S→S◆ Remove S0→S
◆Remove A→B ◆ Remove A→S
S0→S
S→ASA|aB|a|SA|ASA→B|SB→b
S0→ASA|aB|a|SA|ASS→ASA|aB|a|SA|ASA→B|SB→b
S0→ASA|aB|a|SA|ASS→ASA|aB|a|SA|ASA→b|SB→b
S0→ASA|aB|a|SA|AS
S→ASA|aB|a|SA|ASA→b|ASA|aB|a|SA|ASB→b
Eg conti.◆ S0→AA1|UB|a|SA|AS
S→AA1|UB|a|SA|AS
A→b| AA1|UB|a|SA|ASB→bA1→SA
U→a
Pushdown Automata Finite automata
Pushdown automata
State control
a a b b input
State control
a a b b inputxyz
⋮ stack
Def:A pushdown automata is a 6-tuple (Q, , , , q0, F) where Q, , and F are all finite sets, and
1. Q: the set of states2. : the input alphabet3. : the stack alphabet4. : Q× × → P(Q×), the transition function
5. q0∈Q is the start state
6. F⊆Q is the set of accept state
A PDA M= (Q, , , , q0, F) accepts input w=w1w2 …wm, where each wi∈ and sequences of states r0, r1,…, rm ∈Q and strings s0, s1,…, sm∈* exist that satisfy the next 3 conditions: (si : stack contents)
1. r0=q0 and s0= 2. For i=0,…,m-1, we have (ri+1,b) ∈ (ri, wi+1,a),where
si=at and si+1 =bt for some a,b ∈ and t ∈*
3. rm∈ F
a
t
⋯ wi+1 ⋯ ⋯
ri
b
t
⋯ wi+2 ⋯ ⋯
ri+1
Eg: M1=(Q, , , , q1, F ), where Q={q1, q2, q3, q4}
={0,1}={0,$}F={q1,q4}
Input: 0 1 Stack: 0 $ 0 $ 0 $
q1 (q2,$)
q2 (q2,0) (q3,)
Q3 (q3,) (q4,)
q4
Eg conti.a,b→c : reading a from the input it may replace b on the top of the stack with c
q1
q3
q2
q4
, → $ 0, → 0
1, 0→
1, 0→ , $→
Eg: Show a pushdown automaton that recognizes the language kijikjicba kji or and 0,, :
q1 q3 q4
q2 q5 q6 q7
, → $, →
b, a→
, $→
c, →
a, → a
, →
b, →
, → , $→
c, a→
Eg: Give a PDA recognizing the language
wR means w written backwards
*}1,0{: wwwR
q1
q3
q2
q4
, → $ 0, → 0
1, → 1
, →
0, 0→
1, 1→ , $→
Thm: A language is context-free iff some pushdown automaton recognizes it
Proof:(1) If a language is context free, then some PDA
recognizes it
qstart
qloop
qaccept
, → S$
, A→w for rule A→w
a, a→ for terminal a
, $→
q
r
a,s→xyz
⇒
qq1
q2
r ,→ x
,→ y
a,s→z (q,a,s)=(r,xyz)
Proof conti:Q={ qstart,qloop,qaccept } ∪ E
(qstart, , )={ (qloop,S$) }
(qloop, , A )={ (qloop, w) | A→w is a rule in R }
(qloop, , $ )={ (qaccept, ) }
eg: construct a PDA from the following CFG GS→aTb | b
T→Ta|
qstart
qloop
qaccept
, → S$
, $→ ,S→b
,T→
a,a→
b,b→
, S→b , → T , → a
, T→a , → T
S
$
a
T
b
$
T
b
$
b
$
S→aTb→ab
T→
Proof conti:(2)If a pushdown automaton recognizes some
language, then it is context freeSay that P=(Q, , , , q0, {qaccept}) and construct G. The variables of G are {Apq : p,q∈Q}The start variable is
For each p,q,r,s ∈ Q , t∈, and a,b ∈
If (r,t) ∈ (p,a, ) and (q, ) ∈ (s,b,t)
Put Apq→ aArsb in G For each p,q,r ∈ Q , put Apq→ AprArq in G For each p ∈ Q , put App→ in G
accept0 q,qA
Proof conti:Suppose P has the following 3 features:
It has a single accept state, qaccept
It empties its stack before accepting Each transition either pushes a symbol onto the
stack or pops one off the stack, but does not do both at the same time.
Stack
height
Input string
statep r q
Generated by Apr
Generated by Arq
Apq→AprAr
q
Stack
height
statep
rq
Generated by Ars
Apq→aArs
bs
a b
Proof conti: Claim : (Page112 & 113)
Apq generates x iff x can bring P from p with empty stack to q with empty stack.
■
Non-Context-Free Languages Thm: (Pumping lemma for context-free
languages)If A is context free, then there is a number p, where if s∈A of length at least p, then s may be divided into 5 pieces s=uvxyz satisfying the conditions:1.For each i≥0, uvixyiz∈A2.|vy|>03.|vxy|≤p
Proof:T
R
R
u v zx y
T
R
R
u v zy
xv y
R
R
x
R
xv y
R
xR*
vRyR
*
Proof conti:Let G be a CFG for CFL Ab: the max number of symbols in the right-hand side of a rule (b≥2)
Thus, if a parse tree has height h, then the string generated has length ≤ bh |V|: the number of variables in GLet p=b|V|+2 > b|V|+1 ⇒ Any string of length ≥ p must have a parse tree of height ≥ |V|+2
≤ b
Proof conti:s: a string in A with length ≥ pLet be a parse tree of sSuppose has the smallest number of nodes⇒ has height ≥ |V|+2
(2) |v y|>0 (3) |v x y|≤ pif |v y|=0
■
≥|V|+2
R
R
R
x
R ⇒ R
x
R
R
R
yxv
≤|V|+1
|v x y|
≤ b|V|+1
<b|V|+2 =p
Eg: Use the pumping lemma to show that the language B={anbncn |n≥0} is not context free
Proof:Assume B is a CFL and obtain a contradiction.Let p be the pumping length for B.Select s=apbpcp ∈ B
=uvxyz
Proof conti:By condition (2) , v or y is not emptycase 1: When v and y contain only one type of
alphabetu v2 x y2 z a a a b a c b b b c c c
Proof conti:case 2: When either v or y contain more than one type of symbol
u v2 x y2 z will destroy the order of anbncn (ab)2
■
Eg: Let C={aibjck : 0≤ i≤ j≤ k}. Use the pumping lemma to show that C is not a CFL
Proof:p : pumping lengthChoose s = apbpcp
= uvxyz
Proof conti:case 1: When both v and y contain only one type of alphabet
u v2 x y2 z a a a b a c b b b c ⇒ ⇒ There are fewer b than a c c ⇒ ⇒ There are fewer c than b
and a
zxyuv 00
cbzxyuv 00
cccc
Proof conti:case 2: When either v or y contain more than one type of symbol
uv2xy2z will not contain the symbols in the correct order
■
Eg: Use the pumping lemma to show that D={ww : w∈{0,1}* } is not a CFL
Proof:p : pumping lengthChoose s = 0p1p0p1p
= uvxyz~~~
≤ p
Proof conti:
0p1111…1100….00001p
straddle the midpointuv0xy0z has the form 0p1i0j1p , where i and j cannot both be p
v x y
Using dynamic programming to test if w∈ LLet L be generated by CFG G in Chomsky normal form.For i≤ j, Table(i,j) contains the collection of variables that generates the substring wiwi+1…wj
CFL
D=“On input w=w1w2…wn (page 241)1.If w= and S→ is a rule, accept2.For i=1 to n3 For each variable A4 Test whether A→b is a rule, where b=wi 5 if so, place A in Table(i,i)6.For l=2 to n7. For i=1 to n-l+18. j=i+l-19. For k=i to j-110. For each rule A→BC11. If Table(i, k) contains B and
Table(k+1, j) contains CPut A in Table(i, j)
12.If S is in Table(1,n), ACCEPT;otherwise Reject. “
Designing CFG:Eg: {0n1n : n≥0}∪{1n0n : n≥0}
S1→0S11| S2→1S20|
S→S1|S2
S1→0S11|
S2→1S20|
Eg: Convert any DFA into an equivalent CFG
Make a variable Ri for each state qi of the DFA
Add the rule Ri→aRj if (qi,a)=qj
Add the rule Ri→ if qi is an accept state
Make R0 the start variable
Ambiguity:<EXPR>→<EXPR>+<EXPR>|<EXPR>x<EXPR>|(<EXPR>)|a
a+a×a< E X P R >
< E X P R >
< E X P R > < E X P R >
< E X P R >
+ a aa x
< E X P R >
< E X P R > < E X P R >
x aa
< E X P R >
< E X P R >
+a