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

Slides:



Advertisements
Similar presentations
Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death Nathan Brunelle Department of Computer.
Advertisements

C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
CS21 Decidability and Tractability
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
PDAs => CFGs Sipser 2.2 (pages ). Last time…
CFGs and PDAs Sipser 2 (pages ). Last time…
PDAs => CFGs Sipser 2.2 (pages ). Last time…
Lecture 15UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 15.
CS5371 Theory of Computation
CS 302: Discrete Math II A Review. An alphabet Σ is a finite set (e.g., Σ = {0,1}) A string over Σ is a finite-length sequence of elements of Σ For x.
Introduction to the Theory of Computation John Paxton Montana State University Summer 2003.
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 11, 2006.
Lecture Note of 12/22 jinnjy. Outline Chomsky Normal Form and CYK Algorithm Pumping Lemma for Context-Free Languages Closure Properties of CFL.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2009 with acknowledgement.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
CS5371 Theory of Computation Lecture 9: Automata Theory VII (Pumping Lemma, Non-CFL)
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
CS 3240: Languages and Computation Pushdown Automata & CF Grammars NOTE: THESE ARE ONLY PARTIAL SLIDES RELATED TO WEEKS 9 AND 10. PLEASE REFER TO THE TEXTBOOK.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
1 Computer Language Theory Chapter 2: Context-Free Languages.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
CSCI 2670 Introduction to Theory of Computing September 21, 2005.
CSE 3813 Introduction to Formal Languages and Automata Chapter 8 Properties of Context-free Languages These class notes are based on material from our.
نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88.
CSCI 2670 Introduction to Theory of Computing September 20, 2005.
The Pumping Lemma for Context Free Grammars. Chomsky Normal Form Chomsky Normal Form (CNF) is a simple and useful form of a CFG Every rule of a CNF grammar.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Pushdown Automata CS 130: Theory of Computation HMU textbook, Chap 6.
Pushdown Automata (PDAs)
Context-free Languages
Context-free Languages Chapter 2. Ambiguity.
CSCI 2670 Introduction to Theory of Computing September 22, 2005.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Pushdown Automata Chapters Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free.
Cs3102: Theory of Computation Class 8: Non-Context-Free Languages Spring 2010 University of Virginia David Evans.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Non-CF Languages The language L = { a n b n c n | n  0 } does not appear to be context-free. Informal: A PDA can compare #a’s with #b’s. But by the time.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
CSCI 2670 Introduction to Theory of Computing September 23, 2004.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Chapter 8 Properties of Context-free Languages These class notes are based on material from our textbook, An Introduction to Formal Languages and Automata,
January 20, 2016CS21 Lecture 71 CS21 Decidability and Tractability Lecture 7 January 20, 2016.
Lecture 14UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 14.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
 2004 SDU Lecture8 NON-Context-free languages.  2004 SDU 2 Are all languages context free? Ans: No. # of PDAs on  < # of languages on  Pumping lemma:
CS 154 Formal Languages and Computability March 15 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
Theory of Languages and Automata By: Mojtaba Khezrian.
Complexity and Computability Theory I Lecture #12 Instructor: Rina Zviel-Girshin Lea Epstein.
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
Lecture 11  2004 SDU Lecture7 Pushdown Automaton.
CSCI 2670 Introduction to Theory of Computing September 22, 2004.
Context-Free Grammars: an overview
Context-free Languages
CHAPTER 2 Context-Free Languages
Context-Free Grammars
فصل دوم Context-Free Languages
Chapter 2 Context-Free Language - 01
CS21 Decidability and Tractability
CS21 Decidability and Tractability
Presentation transcript:

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,B : variables 0,1,# : terminals

Derivation: generate 000#111 Parse tree:

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

Eg : rules

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:

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

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

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

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

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

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

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

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

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

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

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

Eg: Show a pushdown automaton that recognizes the language q1q1 q3q3 q4q4 q2q2 q5q5 q6q6 q7q7 ,  → $ ,  →  b, a →  , $ →  c,  →  a,  → a ,  →  b,  →  ,  → , $ →  c, a → 

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

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

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 → 

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

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

Proof conti: Claim : (Page112 & 113) A pq 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, uv i xy i z ∈ A 2.|vy|>0 3.|vxy| ≤ p

Proof: T R R uvzxy T R R uvzy xvy R R x R xvy R

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

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

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

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

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 ■

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

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

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 ■

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

Proof conti: 0 p 1111…1100… 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

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

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

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

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

Ambiguity: → + | x |( )|a a+a × a