1 / 43

Chap 2

Chap 2. Context-Free Languages. Context-free Grammars. is not regular Context-free grammar : eg. G 1 : A 0A1 substitution rules AB production rules B# A,B : variables 0,1,# : terminals. Derivation: generate 000#111 Parse tree:. Def : (context-free grammar)

jbenjamin
Download Presentation

Chap 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chap 2 Context-Free Languages

  2. Context-free Grammars • is not regular • Context-free grammar : eg. G1: A0A1 substitution rules AB production rules B# A,B : variables 0,1,# : terminals

  3. Derivation: generate 000#111 • Parse tree:

  4. Def: (context-free grammar) A context-free grammar is a 4-tuple (V,,R,S) 1. V: a finite set called the variables 2. : a finite set, disjoint from V, called the terminals 3. R: a finite set of rules 4. S: start symbol A Language of the grammar is

  5. Eg : • Eg : rules

  6. 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 ambiguous eg:

  7. Chomsky Normal Form Every rule is of the form a : terminal A,B,C : any variables B,C may not be the start variable allow

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

  9. Proof: 1. Add a new start symbol : original start symbol 2. 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

  10. u is a string of variables and terminals 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 Uiui■

  11. Eg. Convert the following CFG to Chomsky Normal Form ◆ ◆Remove B→ ◆ Remove A→ S→ASA|aB A→B|S B→b| S0→S S→ASA|aB A→B|S B→b| S0→S S→ASA|aB|a A→B|S| B→b S0→S S→ASA|aB|a|SA|AS|S A→B|S B→b

  12. Eg conti. ◆Remove unit rule S→S◆ Remove S0→S ◆Remove A→B ◆ Remove A→S S0→S S→ASA|aB|a|SA|AS A→B|S B→b S0→ASA|aB|a|SA|AS S→ASA|aB|a|SA|AS A→B|S B→b S0→ASA|aB|a|SA|AS S→ASA|aB|a|SA|AS A→b|S B→b S0→ASA|aB|a|SA|AS S→ASA|aB|a|SA|AS A→b|ASA|aB|a|SA|AS B→b

  13. Eg conti. ◆ S0→AA1|UB|a|SA|AS S→AA1|UB|a|SA|AS A→b| AA1|UB|a|SA|AS B→b A1→SA U→a

  14. Pushdown Automata • Finite automata • Pushdown automata State control a a b b input State control a a b b x input y z stack ⋮

  15. 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 states 2. : the input alphabet 3. : the stack alphabet 4. : Q× ×  → P(Q×), the transition function 5. q0∈Q is the start state 6. F⊆Q is the set of accept state

  16. 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 ri ri+1 ⋯wi+1⋯⋯ ⋯wi+2⋯⋯ a t b t

  17. Eg: M1=(Q, , , , q1, F ), where Q={q1, q2, q3, q4} ={0,1} ={0,$} F={q1,q4}

  18. Eg conti. a,b→c : reading a from the input it may replace b on the top of the stack with c , → $ 0, → 0 q1 q2 1, 0→ q4 q3 , $→ 1, 0→

  19. Eg: Show a pushdown automaton that recognizes the language b, a→ c, → q1 q3 , $→ q4 , → , → $ q2 , → q5 q6 q7 , → , $→ b, → c, a→ a, → a

  20. Eg: Give a PDA recognizing the language wR means w written backwards , → $ 0, → 0 1, → 1 q1 q2 , → 0, 0→ 1, 1→ q4 q3 , $→

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

  22. qstart Proof: (1) If a language is context free, then some PDA recognizes it , →S$ , A→w for rule A→w a, a→ for terminal a qloop , $→ qaccept q a,s→z  (q,a,s)=(r,xyz) q q1 ⇒ ,→y a,s→xyz q2 r r ,→x

  23. 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 G S→aTb | b T→Ta| qstart , S→b , →T , →a , →S$ , T→a , →T qloop S→aTb→ab T→ , $→ ,S→b ,T→ a,a→ b,b→ a T b $ T b $ S $ b $ qaccept

  24. Proof conti: (2)If a pushdown automaton recognizes some language, then it is context free Say 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

  25. 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 Apq→AprArq Input string p r q state Generated by Arq Generated by Apr Stack height Apq→aArsb p q r s state a Generated by Ars b

  26. Proof conti: • Claim : (Page112 & 113) Apq generates x iff x can bring P from p with empty stack to q with empty stack. ■

  27. 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∈A 2.|vy|>0 3.|vxy|≤p

  28. T T R R R R Proof: R u v x y z u v y z v x y R R R x v x y

  29. ≤b Proof conti: Let G be a CFG for CFL A b: 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 G Let p=b|V|+2 > b|V|+1 ⇒ Any string of length ≥ p must have a parse tree of height ≥ |V|+2

  30. Proof conti: s: a string in A with length ≥ p Let  be a parse tree of s Suppose  has the smallest number of nodes ⇒ has height ≥ |V|+2 (2) |v y|>0 (3) |v x y|≤ p if |v y|=0 ■ R ≥|V|+2 R R |v x y| ≤ b|V|+1 <b|V|+2 =p R R ⇒ R R ≤|V|+1 R  x  x v x y

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

  32. Proof conti: By condition (2) , v or y is not empty case 1:When v and y contain only one type of alphabet u v2 x y2 z a a a b a c b b b c c c

  33. Proof conti: case 2:When either v or y contain more than one type of symbol u v2 x y2 zwill destroy the order of anbncn (ab)2 ■

  34. Eg: Let C={aibjck : 0≤ i≤ j≤ k}. Use the pumping lemma to show that C is not a CFL Proof: p : pumping length Choose s = apbpcp = uvxyz

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

  36. 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 ■

  37. Eg: Use the pumping lemma to show that D={ww : w∈{0,1}* } is not a CFL Proof: p : pumping length Choose s = 0p1p0p1p = uvxyz ~~~ ≤ p

  38. Proof conti: 0p1111…1100….00001p straddle the midpoint uv0xy0z has the form 0p1i0j1p , where i and j cannot both be p v x y

  39. Using dynamic programming to test if w∈ L Let 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

  40. D=“On input w=w1w2…wn (page 241) 1.If w= and S→ is a rule, accept 2.For i=1 to n 3 For each variable A 4 Test whether A→b is a rule, where b=wi 5 if so, place A in Table(i,i) 6.For l=2 to n 7. For i=1 to n-l+1 8. j=i+l-1 9. For k=i to j-1 10. For each rule A→BC 11. If Table(i, k) contains B and Table(k+1, j) contains C Put A in Table(i, j) 12.If S is in Table(1,n), ACCEPT; otherwise Reject. “

  41. Designing CFG: Eg: {0n1n : n≥0}∪{1n0n : n≥0} S2→1S20| S1→0S11| S→S1|S2 S1→0S11| S2→1S20|

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

  43. Ambiguity: <EXPR>→<EXPR>+<EXPR>|<EXPR>x<EXPR>|(<EXPR>)|a a+a×a

More Related