Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 :...

43
Chap 2 Context-Free Languages
  • 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 :...

Page 1: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

Chap 2

Context-Free Languages

Page 2: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

Context-free Grammars is not regular Context-free grammar :

eg. G1:

A0A1 substitution rulesAB production rulesB#

A,B : variables0,1,# : terminals

}0 : 10{ nnn

Page 3: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 4: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 5: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 6: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 7: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

Chomsky Normal FormEvery rule is of the form

a : terminalA,B,C : any variablesB,C may not be the start variableallow

aA

BCA

S

Page 8: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

Thm: Any context-free language is generated by a context-free grammar in Chomsky Normal Form

Page 9: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 10: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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 , , ,

Page 11: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 12: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules 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

Page 13: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules 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

Page 14: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

Pushdown Automata Finite automata

Pushdown automata

State control

a a b b input

State control

a a b b inputxyz

⋮ stack

Page 15: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 16: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 17: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 18: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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→ , $→

Page 19: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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→

Page 20: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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→ , $→

Page 21: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

Thm: A language is context-free iff some pushdown automaton recognizes it

Page 22: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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)

Page 23: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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→

Page 24: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 25: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 26: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

Proof conti: Claim : (Page112 & 113)

Apq generates x iff x can bring P from p with empty stack to q with empty stack.

Page 27: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 28: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

*

Page 29: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 30: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules 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

Page 31: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 32: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 33: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 34: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 35: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 36: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 37: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 38: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

Proof conti:

0p1111…1100….00001p

straddle the midpointuv0xy0z has the form 0p1i0j1p , where i and j cannot both be p

v x y

Page 39: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 40: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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. “

Page 41: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

Designing CFG:Eg: {0n1n : n≥0}∪{1n0n : n≥0}

S1→0S11| S2→1S20|

S→S1|S2

S1→0S11|

S2→1S20|

Page 42: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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

Page 43: Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.

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