Download presentation
Presentation is loading. Please wait.
1
Chap 2 Context-Free Languages
2
Context-free Grammars is not regular Context-free grammar : eg. G 1 : A 0A1substitution rules A Bproduction 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 : 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 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
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. Replacewith A i : new variables u i : variable or terminal symbol If u i is terminal, add U i u i ■ u is a string of variables and terminals
11
Eg. Convert the following CFG to Chomsky Normal Form ◆ ◆ Remove B→ ◆ Remove A→ S → ASA|aB A → B|S B → b| S 0 → S S → ASA|aB A → B|S B → b| S 0 → S S → ASA|aB|a A → B|S| B → b S 0 → S S → ASA|aB|a|SA|AS|S A → B|S B → b
12
Eg conti. ◆ Remove unit rule S→ S ◆ Remove S 0 → S ◆ Remove A→ B ◆ Remove A→ S S 0 → S S → ASA|aB|a|SA|AS A → B|S B → b S 0 → ASA|aB|a|SA|AS S → ASA|aB|a|SA|AS A → B|S B → b S 0 → ASA|aB|a|SA|AS S → ASA|aB|a|SA|AS A → b|S B → b S 0 → ASA|aB|a|SA|AS S → ASA|aB|a|SA|AS A → b|ASA|aB|a|SA|AS B → b
13
Eg conti. ◆ S 0 → AA 1 |UB|a|SA|AS S → AA 1 |UB|a|SA|AS A → b| AA 1 |UB|a|SA|AS B → b A 1 → SA U → a
14
Pushdown Automata Finite automata Pushdown automata State control a a b b input State control a a b b input x y z ⋮ stack
15
Def: A pushdown automata is a 6-tuple (Q, , , , q 0, 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. q 0 ∈ Q is the start state 6. F ⊆ Q is the set of accept state
16
A PDA M= (Q, , , , q 0, F) accepts input w=w 1 w 2 …w m, where each w i ∈ and sequences of states r 0, r 1,…, r m ∈ Q and strings s 0, s 1,…, s m ∈ * exist that satisfy the next 3 conditions: (s i : stack contents) 1. r 0 =q 0 and s 0 = 2. For i=0,…,m-1, we have (r i+1,b) ∈ (r i, w i+1,a),where s i =at and s i+1 =bt for some a,b ∈ and t ∈ * 3. r m ∈ F atat ⋯ w i+1 ⋯ ⋯ r i btbt ⋯ w i+2 ⋯ ⋯ r i+1
17
Eg: M 1 =(Q, , , , q 1, F ), where Q={q 1, q 2, q 3, q 4 } ={0,1} ={0,$} F={q 1,q 4 } Input:01 Stack:0$ 0$ 0$ q1q1 (q 2,$) q2q2 (q 2,0) (q3,)(q3,) Q3Q3 (q3,)(q3,) (q 4, ) q4q4
18
Eg conti. a,b → c : reading a from the input it may replace b on the top of the stack with c q1q1 q3q3 q2q2 q4q4 , → $0, → 0 1, 0 → , $ →
19
Eg: Show a pushdown automaton that recognizes the language q1q1 q3q3 q4q4 q2q2 q5q5 q6q6 q7q7 , → $ , → b, a → , $ → c, → a, → a , → b, → , → , $ → c, a →
20
Eg: Give a PDA recognizing the language w R means w written backwards q1q1 q3q3 q2q2 q4q4 , → $0, → 0 1, → 1 , → 0, 0 → 1, 1 → , $ →
21
Thm: A language is context-free iff some pushdown automaton recognizes it
22
Proof: (1) If a language is context free, then some PDA recognizes it q start q loop q accept , → S$ , A → w for rule A → w a, a → for terminal a , $ → q r a,s → xyz ⇒ q q1q1 q2q2 r ,→x,→x ,→y,→y a,s→za,s→z (q,a,s)=(r,xyz)
23
Proof conti: Q={ q start,q loop,q accept } ∪ E (q start, , )={ (q loop,S$) } (q loop, , A )={ (q loop, w) | A → w is a rule in R } (q loop, , $ )={ (q accept, ) } eg: construct a PDA from the following CFG G S → aTb | b T → Ta| q start q loop q accept , → S$ , $ → ,S → b ,T → a,a → b,b → , S → b , → T , → a , T → a , → T S$S$ aTb$aTb$ Tb$Tb$ b$b$ S → aTb → ab T →
24
Proof conti: (2)If a pushdown automaton recognizes some language, then it is context free Say that P=(Q, , , , q 0, {q accept }) and construct G. The variables of G are {A pq : 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 A pq → aA rs b in G For each p,q,r ∈ Q, put A pq → A pr A rq in G For each p ∈ Q, put A pp → in G
25
Proof conti: Suppose P has the following 3 features: It has a single accept state, q accept 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 state prq Generated by A pr Generated by A rq A pq → A pr A r q Stack height state p r q Generated by A rs A pq → aA rs b s ab
26
Proof conti: Claim : (Page112 & 113) A pq 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, uv i xy i z ∈ A 2.|vy|>0 3.|vxy| ≤ p
28
Proof: T R R uvzxy T R R uvzy xvy R R x R xvy R
29
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 ≤ b h |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 ≤b≤b
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 ■ ≥ |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
31
Eg: Use the pumping lemma to show that the language B={a n b n c n |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=a p b p c p ∈ 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 v 2 x y 2 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 v 2 x y 2 z will destroy the order of a n b n c n (ab) 2 ■
34
Eg: Let C={a i b j c k : 0 ≤ i ≤ j ≤ k}. Use the pumping lemma to show that C is not a CFL Proof: p : pumping length Choose s = a p b p c p = uvxyz
35
Proof conti: case 1: When both v and y contain only one type of alphabet u v 2 x y 2 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 uv 2 xy 2 z 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 = 0 p 1 p 0 p 1 p = uvxyz ~~~ ≤ p
38
Proof conti: 0 p 1111…1100….00001 p straddle the midpoint uv 0 xy 0 z has the form 0 p 1 i 0 j 1 p, where i and j cannot both be p vxy
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 w i w i+1 … w j CFL
40
D= “ On input w=w 1 w 2 … w n (page 241) 1.If w= and S → is a rule, accept 2.For i=1 to n 3For each variable A 4Test whether A → b is a rule, where b=w i 5if 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: {0 n 1 n : n ≥ 0} ∪ {1 n 0 n : n ≥ 0} S 1 → 0S 1 1| S 2 → 1S 2 0| S → S 1 |S 2 S 1 → 0S 1 1| S 2 → 1S 2 0|
42
Eg: Convert any DFA into an equivalent CFG Make a variable R i for each state q i of the DFA Add the rule R i → aR j if (q i,a)=q j Add the rule R i → if q i is an accept state Make R 0 the start variable
43
Ambiguity: → + | x |( )|a a+a × a
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.