CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.

Slides:



Advertisements
Similar presentations
Context-Free and Noncontext-Free Languages
Advertisements

Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death Nathan Brunelle Department of Computer.
Chapter 5 Pushdown Automata
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
CFGs and PDAs Sipser 2 (pages ). Long long ago…
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
Pushdown Automata Part II: PDAs and CFG Chapter 12.
CS21 Decidability and Tractability
CFGs and PDAs Sipser 2 (pages ). Last time…
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
CS5371 Theory of Computation
Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.
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.
104 Closure Properties of Regular Languages Regular languages are closed under many set operations. Let L 1 and L 2 be regular languages. (1) L 1  L 2.
79 Regular Expression Regular expressions over an alphabet  are defined recursively as follows. (1) Ø, which denotes the empty set, is a regular expression.
Normal forms for Context-Free Grammars
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
Fall 2006Costas Busch - RPI1 PDAs Accept Context-Free Languages.
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL.
Finite State Machines Data Structures and Algorithms for Information Processing 1.
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2010.
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.
Pushdown Automata (PDA) Intro
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Context-Free Grammars Normal Forms Chapter 11. Normal Forms A normal form F for a set C of data objects is a form, i.e., a set of syntactically valid.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Introduction to CS Theory Lecture 3 – Regular Languages Piotr Faliszewski
Pushdown Automata (PDAs)
Grammars CPSC 5135.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Theory of Computation, Feodor F. Dragan, Kent State University 1 TheoryofComputation Spring, 2015 (Feodor F. Dragan) Department of Computer Science Kent.
Pushdown Automata Chapters Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free.
Context-Free and Noncontext-Free Languages Chapter 13 1.
Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1  A 2 is context free as well. Proof: Assume that the two grammars are.
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.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
CS 203: Introduction to Formal Languages and Automata
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
Context-Free and Noncontext-Free Languages Chapter 13.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 9 Mälardalen University 2006.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Chapter 8 Properties of Context-free Languages These class notes are based on material from our textbook, An Introduction to Formal Languages and Automata,
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen.
Lecture 6: Context-Free Languages
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
CS 154 Formal Languages and Computability March 15 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
Costas Busch - LSU1 PDAs Accept Context-Free Languages.
Theory of Languages and Automata By: Mojtaba Khezrian.
Context-Free and Noncontext-Free Languages Chapter 13.
Formal Languages, Automata and Models of Computation
G. Pullaiah College of Engineering and Technology
Deterministic FA/ PDA Sequential Machine Theory Prof. K. J. Hintz
CSE 105 theory of computation
PDAs Accept Context-Free Languages
Chapter 7 PUSHDOWN AUTOMATA.
CHAPTER 2 Context-Free Languages
Chapter 2 Context-Free Language - 01
… NPDAs continued.
Presentation transcript:

CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American

Chapter Two Context-free Languages

A Short Overview We know that is not regular Can we design a grammar to generate L?  Answer: S  a S b S  e The grammar is (V, , R, S)  V = { a, b, S}   = {a, b}  S  R: S  a S b, S  e The above grammar is context-free.

Context-free Grammars Definition. A context-free grammar G is a quadruple (V, , R, S) where  V is an alphabet    V is the set of terminals  S  V -  is the start symbol  R is the set of production rules R  (V -  ) x V*  V -  is the set of non–terminals

Production Rules Let (A, u)  R be a production rule. We can rewrite it as  A  u A  u means that from a nonterminal symbol A, we derive a string u. Or, we say A implies u. Or, A derives u. Or, A generates u.

Understand the Derivation Relation Let G= (V, , R, S) be a context-free grammar. if and only if The relation has a reflexive and transitive closure denoted by Understand

Context-free Languages The language generated by a CF G= (V, , R, S):  A string is generated by G if  The language generated by G is A language is CF, if it is generated by a CF grammar.

Arithmetic Expressions Ex’s: The language of arithmetic expressions is CF. This language is generated by the CF grammar G  G = (V, , R, E)  V = { E, (, ), +, *, -, /, id, T, F}   = {(, ), +, *, /, id}  E  R: E  E + T  T  T * F  E – T  T / F T  T * F  T / F  F F  (E) F  id Ex’s of derivation?

CF Language Examples Ex:  is context-free.  show this is true in class Ex:  is context-free.  show this is true in class.

Theorem. Every regular language is context-free. Proof:Let L = L(M) be regular language recognized by a FA M = ( K, , , s, F).  Construct a context-free grammar to simulate M.  Idea of construction

Example of Construction Construct the CF grammar for the following FA: a bb b a a

Parse Trees Given a CF grammar G = ( V, , S, R),    L (G), the derivation procedure S  *  can be described by a tree. We call such a tree as the parse tree of . Importance of parse tree  analysis of the syntax of .

Parse Tree Examples Consider arithmetic expressions generated by G = ( V, , R, E), where  V = { E, T, F, (, ), +, *, -, /, id}   = {(, ), +, *, -, /, id}  R: E  E + T | E – T | T | T * F | T / F T  T * F | T / F | F F  (E) | id Construct a parse tree for  id*(id+id)

Rightmost Derivations Given a context-free grammar G = ( V, , S, R) for any    *, a right-most derivation for  is such a derivation that at each step the right-most non-terminal is used to do the derivation.

Ex’s of Rightmost Derivations Ex: G = ( V, , S, R), where  V = { E, T, (, ), id, +, *, -, / }   = {(, ), id, +, *, -, /}  R: E  E + T | E – T | E * T | E / T T  (E) | id Find the rightmost derivation for  ( id + id ) * ( id – id * id )

Leftmost Derivations Similar to rightmost derivations, at each step the left-most non-terminal symbol is used to do derivation. Ex Find the leftmost derivation for  ( id + id ) * ( id – id * id )

Theorem Let G = ( V, , S, R) be a context-free grammar, and let A  V- , and    *. Then the following statements are equivalent:  (a) A  *   (b) There is a parse tree with root A and yield .  (c) There is a leftmost derivation A  *   (d) There is a rightmost derivation A  *  Proof  by induction on the length of .  Prove (a)  (b)  (c)  (d)  (d) L R

Ambiguity A context-free grammar G = ( V, , S, R) is ambiguous if there is a    * such the  has two distinct parse trees.  That is, there are different meanings or interpretations for , or  The semantics of  is ambiguous

Ambiguity Examples Ex. E  E + E | E * E | (E) | id Note. Can you see different meanings of id+id*id?

Ambiguous Languages A language is inherently ambiguous if any context-free grammar generating it is ambiguous. Why ambiguity is not good?

Pushdown Automata (PA)

Definition of PA A pushdown automata is a sextuple M = ( K, , , , S, F )  K is a finite set of states.   is the input alphabet.   is the stack alphabet.  S  K is the initial state.  F  K is the set of final states.   is the transition relation  : K x (   { e} ) x  *  K x  *

Understand the Transition Relation  Understand    (p, a,  ) = ( q,  ) p: the current state a: the current input symbol  : the top string on the current stack q: the new state  : replace the top string on the current stack with 

Configurations of PA Configurations of a pushdown automaton are tuples in  K x  * x  * Given a configuration  ( p, , u )  understand it: p: the current state  : the remaining tape content u: the current stack content

Yield Relations of PA Yield relation |  Given two configurations ( p, x,  ) and (q, y,  ),  ( p, x,  ) |  (q, y,  )  If x = a y,  =  ,  =  ,  (p, a,  ) = ( q,  ) Define |  * as the reflexive transitive closure of |  Understand |  and |  *

The Language Accepted by a PA Give    *, a PA M accepts  if and only if (s, , e) ⊢ * (p, e, e) for some p  F. The language accepted by M is  L (M) = {    * : (s, , e) ⊢ * (p, e, e) for some p  F}

PA Examples EX. Design a pushdown automaton accepting  L = {  c  :   {a, b}* } M = ( K, , , , S, F )  K = { s, f },  = { a, b, c}   = { a, b }, F = { f }   : ( s, a, e )  ( s, a) ( s, b, e )  ( s, b) ( s, c, e )  ( f, e) ( f, a, a )  ( f, e) ( f, b, b )  ( f, e) R

PA Examples EX. Design a pushdown automaton accepting  L = {   :   {a, b}* }  ( s, a, e )  ( s, a)  ( s, b, e )  ( s, b)  ( s, e, e )  ( f, e)  ( f, a, a )  ( f, e)  ( f, b, b )  ( f, e) R

PA vs. CF Languages Theorem 3.4.1: the class of languages accepted by pushdown automata is exactly the class of context-free languages.

Proof. Part 1 Each CF language is accepted by some PA. Let G = ( V, , R, S ) be a CF grammar. Want to construct a PA M such that L (G) = L (M). The idea of constructing of M?  Push the start symbol S of the CF G onto the stack  Simulate derivation on the stack  Match terminals symbols in stack top with the current input symbols

Constructing of the PA for CF G  M = ( {p,q}, , V, , p, {q} )   : ( p, e, e )  ( q, S) ( q, e, A )  ( q, x), if A  x  R ( q, a, a )  ( q, e),  a  

Example EX Construct a PA M for G = ( V, , R, S )  V = { s, a, b, c },   = { a, b, c },  R : S  a S a, S  b S b, S  c

The PA M is M = ( {p,q}, , V, , p, {q} )   : ( p, e, e )  ( q, S) ( q, e, S )  ( q, a S a) ( q, e, S)  ( q, b S b) ( q, e, S)  ( q, c) ( q, a, a)  ( q, e) ( q, b, b)  ( q, e) ( q, c, c)  ( q, e)

Operation on abbcbba StateUnread InputStack pabbcbbae qabbcbbaS qabbcbbaaSa q bbcbba Sa q bbcbba bSba q bcbba Sba q bcbba bSbba q cbba Sbba q cbba cbba q bba bba q ba ba q a a q e e

Now, we need to prove L (M) = L (G) Claim Let    *,   ( V -  ) V*  {e}. Then S  *   if and only if (q, , S) ⊢ * (q, e,  ) Proof of Claim.  (if – part) suppose S  *  , where    *,   ( V -  ) V*  {e}.  We prove (q, , S) ⊢ * (q, e,  )  By induction on the length of leftmost.  Basis step. The length is 0, i.e.  = e,  = S L L

Induction hypothesis :  Assume if S  *   by a derivation of length n or less, n  0, then (q, , S) ⊢ * (q, e,  ) Induction step.  Let Be a leftmost derivation if   from S. Let A be the leftmost nonterminal symbol, then where    *, ,   V*, A    R L

(only-if part) Suppose (q, , S) ⊢ *(q, e,  ) with    *,   ( V -  ) V*  {e}. We show S  *  . By induction on the number of transitions of type 2 in the computation by M. L

Part 2. If a language is accepted by a pushdown automaton then it is a context- free language. We consider simple pushdown automaton:  Whenever (q, ,  )  (p,  ) is a transition and q is not the start state, then   , and |  |  2.  Note Any pushdown automaton can be simulated by a simple pushdown automaton.

Construction of context-free grammar G = ( V, , R, S )   is the same  S is the new initial state  V is the set of S plus all the states below,  q, p  K,  A    {e, Z}

Explain represents any portion of the input string that might be read between a point in time when M is in state q with A on the top of its stack, and a point in time when M removes A from the stack and enters state p.

R: (1) S , where s is the start state of the original PA M, and f’ is the new final state (2) For each (q, a, B)  ( r, e)  where q, r  K, a    {e}, B, C    {e}  and for each p  K, Add rule   a

(3) For each (q, a, B)  ( r, C1 C2)  Where q, r  K, a    {e}, B    {e}  and C1, C2   and for each p, p’  K Add rule   a (4) For each q  K, add   e

Claim  q, p  K, A    {e}, and x   *,   * x if and only if  (q, x, A) ⊢ * (p, e, e)

Closure Properties. Theorem CF languages are closed under union, concatenation and kleene star. Proof. Given  G1 = ( V1,  1, R1, S1 )  G2 = ( V2,  2, R2, S2 )  Union: Want G = ( V, , R, S ) such that L(G)=L(G1)  L(G2) Construction of G  V = V1  V2  { S }  R = R1  R2  {S  S1, S  S2}

Closure Properties  Concatenation: want G = ( V, , R, S ) such that  L (G) = L (G1) L (G2) Construction of G  V = V1  V2  { S }  R = R1  R2  {S  S1 S2}

Closure Properties  Kleene star: Want G = ( V, , R, S ) such that  L (G) = L* (G1), where G1 = ( V, , R, S1 ) Construction of G  V = V1  { S }  R = R1  {S  e, S  S S1}

Intersection with Regular Languages Theorem The intersection of a CF language with a regular language is CF. Proof: Given L1 = L (M1), L2 = L (M2)  M1 is a pushdown automaton  M1 = ( K1, ,  1,  1, S1, F1 )  M2 is a finite automaton (M2 is deterministic)  M2 = ( K2, , , S2, F2).  Want a pushdown automaton M = ( K, , , , S, F ) such that L (M) = L (M1)  L (M2).

Proof (continued). Idea Use M3 to do parallel simulation of M1 and M2 Construction:  K = K1 x K2   =  1  S = (S1, S2 )  F = F1 x F2   : If (q1, a,  )  (p1,  )   1 for each q2  K2, define ((q1, q2), a,  )  ((p1,  (q2, a)),  ). If (q1, e,  )  (p1,  )   1, for each q2  K2, define ((q1, q2), e,  )  ((p1, q2),  ).

A Technical Lemma Let G = (V, , R, S) be a CF grammar. Let  (G) denote the largest number of symbols on the right-hand side of any rule in R.   (G) indicates the largest number of children a node in a parse tree of G may have. Lemma The yield of any parse tree of G of height h has length at most (  (G)). Proof. Estimate the tree size. h

The Pumping Theorem Theorem 3.5.3Let G = (V, , R, S) be a CF grammar. Then any string  L(G) of length greater than can be written as such that either v or y is nonempty and for every n  0. Furthermore,

Proof of the Pumping Theorem

Non-CF Languages: Applications of the Pumping Theorem Consider   L3 = {   { a b c}* :  has a equal number of a’s, b’s and c’s}

Non-CF Languages: Applications of the Pumping Theorem Proofs for L1 and L2 are direct applications of the pumping theorem  Consider several cases for L1  Note that we can choose n=q+1+r to make nq+r=(q+1)(r+2) to be a composite n umber Note: L1 = L3  a* b* c*  Proof for L3 is done by contradiction and indirect application of the pumping theorem. Demonstrate proofs in class to show that these three languages are not CF.

A New Picture of Languages CF Languages Regular Languages

Theorem CF languages are not closed under intersection or complementation. ProofBy contradiction. Given two CF languages  Suppose CF languages are closed under intersection or complementation, then must be CF. Unfortunately, this is not true (by the Pumping Theorem).

Algorithms for CF Grammars Theorem  (a) There is a polynomial algorithm which, given a CF grammar, constructs an equivalent pushdown automaton.  (b) There is a polynomial algorithm which, given a pushdown automaton, constructs an equivalent CF grammar.  (c) There is a polynomial algorithm which, given a CF grammar G and a string x decides whether x  L (G). (c) is not easy to see. However, (a) and (b) are straightforward  See the proof about the equivalence of CF grammars and PA. The constructions were given in the proof.

Proof of (c) Two major steps:  Convert a CF grammar to an equivalent Chomsky Normal Form CF grammar.  Decide the membership problem for the Chomsky Normal Form CF grammar. Use dynamic programming technique.

Chomsky Normal Form A CF grammar G = ( V, , R, S) is in Chomsky Normal Form if R  ( V -  ) x V Understand R A Chomsky Normal Form grammar cannot generate symbols in   {e} 2

Conversion to Chomsky Normal Formal Convert G = (V, , R, S) to a Chomsky Normal Form CF grammar G’ such that L(G’) = L(G) – (   {e} ) Step 1. Get rid of longer rules  Ex A  B1 B2 B3 B4 B5 Will be replaced by A  B1 A1 A1  B2 A2 A2  B3 A3 A3 = B4 B5 where A1, A2 and A3 are new nonterminals.  The time complexity for this step is O(n).

Step 2 : Get Rid of e–Rules Find the set of erasable nonterminals  E = { A  ( V -  ) : A  * e } Algorithm to find erasable nonterminals: E =  While  A   with   E* and A  E  add A to E Delete from G the e-rules A  e, and Short rules:  For any rule A  B C or A  C B with B  E and C  V Add A  C Time Complexity of the above steps is O (n ) 2

Step 3 : Get Rid of Shorter Rules For each A  V compute D(A)  D (A) = { B  V : A  * B } Algorithm:  D (A) = { A }  While  B  C with B  D (A) and C  D(A) add C to D(A) Delete shorter rules like A  B For each rule A  B C replace it by A  B’ C’ for every B’  D(B), C’  D(C) Finally, for S  B C, add A  B C for every A  D(S) Time complexity of the above is O (n ) 2

Decide the Membership for the Chomsky Normal Form Idea: Dynamic programming  For any decide by analyzing substrings of x   1  i  i + s  n, define N [i, i+s] to be the set of all symbols in V that can derive in G the string  Use dynamic programming to compute N [i, i+s], and hence N[1,n] if and only

Find N[i, i+s] Algorithm  For i = 1 to n do N [i, i] = { }; let all other N [ i, j ] =   For s =1 to n-1 do for i = 1 to n - s do  for k = i to i + s – 1 do  if  a rule A  B C  R with B  N [ i, k] and C  N [ k + 1, i + s]  add A to N [ i, i + s]  Accept x if