1 Computer Language Theory Chapter 2: Context-Free Languages.

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

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,
Pushdown Automata Chapter 12. Recognizing Context-Free Languages We need a device similar to an FSM except that it needs more power. The insight: Precisely.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
CS 3240: Languages and Computation Properties of Context-Free Languages.
Pushdown Automata Part II: PDAs and CFG Chapter 12.
CS21 Decidability and Tractability
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
CFGs and PDAs Sipser 2 (pages ). Last time…
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
Lecture 15UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 15.
CS5371 Theory of Computation
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
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 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 9, 2006.
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 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 PROGAMS CSci 4011.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2010.
Pushdown Automata.
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 (PDAs)
Context-free Languages
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.
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.
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.
CSCI 2670 Introduction to Theory of Computing September 23, 2004.
Chapter 8 Properties of Context-free Languages These class notes are based on material from our textbook, An Introduction to Formal Languages and Automata,
CSCI 2670 Introduction to Theory of Computing October 13, 2005.
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
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,
1 Chapter Pushdown Automata. 2 Section 12.2 Pushdown Automata A pushdown automaton (PDA) is a finite automaton with a stack that has stack operations.
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.
Lecture 11  2004 SDU Lecture7 Pushdown Automaton.
Pushdown Automata.
Context-Free Grammars: an overview
CSE 105 theory of computation
Context-free Languages
CHAPTER 2 Context-Free Languages
Context-Free Grammars
فصل دوم Context-Free Languages
Context-Free Languages
Chapter 2 Context-Free Language - 01
CSE 105 theory of computation
CS21 Decidability and Tractability
Computer Language Theory
CSE 105 theory of computation
CSE 105 theory of computation
Presentation transcript:

1 Computer Language Theory Chapter 2: Context-Free Languages

2 Overview In Chapter 1 we introduced two equivalent methods for describing a language: Finite Automata and Regular Expressions In Chapter 1 we introduced two equivalent methods for describing a language: Finite Automata and Regular Expressions In this chapter we do something analogous In this chapter we do something analogous We introduce context free grammars (CFGs) We introduce context free grammars (CFGs) We introduce push-down automata (PDA) We introduce push-down automata (PDA) PDAs recognize CFGs PDAs recognize CFGs In my view the order is reversed from before since the PDA is introduced second In my view the order is reversed from before since the PDA is introduced second We even have another pumping lemma (Yeah!) We even have another pumping lemma (Yeah!)

3 Why Context Free Grammars They were first used to study human languages They were first used to study human languages You may have even seen something like them before You may have even seen something like them before They are definitely used for “real” computer languages (C, C++, etc.) They are definitely used for “real” computer languages (C, C++, etc.) They define the language They define the language A parser uses the grammar to parse the input A parser uses the grammar to parse the input Of course you can also parse English Of course you can also parse English

4 Section 2.1 Context-Free Grammars

5 A Context-Free Grammar Here is an example grammar G1 Here is an example grammar G1 A  0A1 A  B B  # A grammar has substitution rules or productions A grammar has substitution rules or productions Each rule has a variable and arrow and a combination of variables and terminal symbols Each rule has a variable and arrow and a combination of variables and terminal symbols We will capitalize symbols but not terminals We will capitalize symbols but not terminals A special variable is the start variable A special variable is the start variable Usually on the left-hand side of topmost rule Usually on the left-hand side of topmost rule Here the variables are A and B and the terminals are 0, 1, # Here the variables are A and B and the terminals are 0, 1, #

6 Using the Grammar Use the grammar to generate a language by replacing variables using the rules in the grammar Use the grammar to generate a language by replacing variables using the rules in the grammar Start with the start variable Start with the start variable Give me some strings that grammar G1 generates? Give me some strings that grammar G1 generates? One answer: 000#111 One answer: 000#111 The sequence of steps is the derivation The sequence of steps is the derivation For this example the derivation is: For this example the derivation is: A  0A1  00A11  000A111  000B111  000#111 A  0A1  00A11  000A111  000B111  000#111 You can also represent this with a parse tree You can also represent this with a parse tree

7 The Language of Grammar G1 All strings generated by G1 form the language All strings generated by G1 form the language We write it L(G1) We write it L(G1) What is the language of G1? What is the language of G1? L(G1) = {0 n #1 n | n ≥0} L(G1) = {0 n #1 n | n ≥0} This should look familiar. Can we generate this with a FA? This should look familiar. Can we generate this with a FA?

8 An Example English Grammar Page 101 of the text has a simplified English grammar Page 101 of the text has a simplified English grammar Follow the derivation for “a boy sees” Follow the derivation for “a boy sees” Can you do this without looking at the solution? Can you do this without looking at the solution?

9 Formal Definition of a CFG A CFG is a 4-tuple (V, , R, S) where A CFG is a 4-tuple (V, , R, S) where 1. V is a finite set called the variables 2.  is a finite set, disjoint from V, called the terminals 3. R is a finite set of rules, with each rule being a variable and a string of variables and terminals, and 4. S  V is the start variable

10 Example Grammar G3 = ({S}, {a,b}, R, S), where the set of rules R is: Grammar G3 = ({S}, {a,b}, R, S), where the set of rules R is: S  aSb | SS | ε What does this generate: What does this generate: abab, aaabbb, aababb abab, aaabbb, aababb If you view a as “(“ and b as “)” then you get all strings of properly nested parentheses If you view a as “(“ and b as “)” then you get all strings of properly nested parentheses Note they consider ()() to be okay Note they consider ()() to be okay I think the key property here is that at any point in the string you have at least as many a’s to the left of it as b’s I think the key property here is that at any point in the string you have at least as many a’s to the left of it as b’s

11 Another Example Consider example 2.4 in text on page 103 Consider example 2.4 in text on page 103

12 Designing CFGs Like designing FA, some creativity is required Like designing FA, some creativity is required It is probably even harder with CFGs since they are more expressive than FA (we will show that soon) It is probably even harder with CFGs since they are more expressive than FA (we will show that soon) Here are some guidelines Here are some guidelines If the CFL is the union of simpler CFLs, design grammars for the simpler ones and then combine If the CFL is the union of simpler CFLs, design grammars for the simpler ones and then combine For example, S  G1 | G2 | G3 For example, S  G1 | G2 | G3 If the language is regular, then can design a CFG that mimics a DFA If the language is regular, then can design a CFG that mimics a DFA Make a variable Ri for every state qi Make a variable Ri for every state qi If δ(qi, a) = qj, then add Ri  aRj If δ(qi, a) = qj, then add Ri  aRj Add Ri  ε if i is an accept state Add Ri  ε if i is an accept state Make R0 the start variable where q0 is the start state of the DFA Make R0 the start variable where q0 is the start state of the DFA Assuming this really works, what did we just show? Assuming this really works, what did we just show?

13 Designing CFGs continued A final guideline: A final guideline: Certain CFLs contain strings that are linked in the sense that a machine for recognizing this language would need to remember an unbounded amount of information about one substring to “verify” the other substring. Certain CFLs contain strings that are linked in the sense that a machine for recognizing this language would need to remember an unbounded amount of information about one substring to “verify” the other substring. This is sometimes trivial with a CFG This is sometimes trivial with a CFG Example: 0 n 1 n Example: 0 n 1 n S  0S1 | ε S  0S1 | ε

14 Ambiguity Sometimes a grammar can generate the same string in multiple ways Sometimes a grammar can generate the same string in multiple ways If a grammar generates even a single string in multiple ways the grammar is ambiguous If a grammar generates even a single string in multiple ways the grammar is ambiguous Example: Example: EXPR  EXPR + EXPR | EXPR × EXPR |(EXPR) | a This generates the string a+a × a ambiguously This generates the string a+a × a ambiguously Try it: generate two parse trees Try it: generate two parse trees Using your extensive knowledge of arithmetic, insert parentheses to shows what each parse tree really represents Using your extensive knowledge of arithmetic, insert parentheses to shows what each parse tree really represents

15 An English Example Grammar G2 on page 101 ambiguously generates the girl touches the boy with the flower Grammar G2 on page 101 ambiguously generates the girl touches the boy with the flower Using your extensive knowledge of English, what are the two meanings of this phrase Using your extensive knowledge of English, what are the two meanings of this phrase

16 Definition of Ambiguity A grammar generates a string ambiguously if there are two different parse trees A grammar generates a string ambiguously if there are two different parse trees Two derivations may differ in the order that the rules are applied, but if they generate the same parse tree, it is not really ambiguous Two derivations may differ in the order that the rules are applied, but if they generate the same parse tree, it is not really ambiguous Definitions: Definitions: A derivation is a leftmost derivation if at every step the leftmost remaining variable is replaced A derivation is a leftmost derivation if at every step the leftmost remaining variable is replaced A string w is derived ambiguously in a CFG G if it has two or more different leftmost derivations. A string w is derived ambiguously in a CFG G if it has two or more different leftmost derivations.

17 Chomsky Normal Form It is often convenient to convert a CFG into a simplified form It is often convenient to convert a CFG into a simplified form A CFG is in Chomsky normal form if every rule is of the form: A CFG is in Chomsky normal form if every rule is of the form: A  BC A  a Where a is any terminal and A, B, and C are any variables– except B and C may not be the start variable. The start variable can also go to ε Any CFL can be generated by a CFG in Chomsky normal form Any CFL can be generated by a CFG in Chomsky normal form

18 Converting CFG to Chomsky Normal Form Here are the steps: Here are the steps: Add rule S 0  S, where S was original start variable Add rule S 0  S, where S was original start variable Remove ε-rules. Remove A  ε and for each occurrence of A add a new rule with A deleted. Remove ε-rules. Remove A  ε and for each occurrence of A add a new rule with A deleted. If we have R  uAvAw, we get: If we have R  uAvAw, we get: R  uvAw | uAvw | uvw R  uvAw | uAvw | uvw Handle all unit rules Handle all unit rules If we had A  B, then whenever a rule B  u exists, we add A  u. If we had A  B, then whenever a rule B  u exists, we add A  u. Replace rules A  u 1 u 2 u 3 … u k with: Replace rules A  u 1 u 2 u 3 … u k with: A  u 1 A 1, A 1  u 2 A 2, A 2  u 3 A 3 … A k-2  u k-1 u k A  u 1 A 1, A 1  u 2 A 2, A 2  u 3 A 3 … A k-2  u k-1 u k You will have a HW question like this You will have a HW question like this Prior to doing it, go over example 2.10 in the textbook (page 108) Prior to doing it, go over example 2.10 in the textbook (page 108)

19 Section 2.2 Pushdown Automata

20 Pushdown Automata (PDA) Similar to NFAs but have an extra component called a stack Similar to NFAs but have an extra component called a stack The stack provides extra memory that is separate from the control The stack provides extra memory that is separate from the control Allows PDA to recognize non-regular languages Allows PDA to recognize non-regular languages Equivalent in power/expressiveness to a CFG Equivalent in power/expressiveness to a CFG Some languages easily described by generators others by recognizers Some languages easily described by generators others by recognizers Nondeterministic PDA’s not equivalent to deterministic ones but NPDA = CFG Nondeterministic PDA’s not equivalent to deterministic ones but NPDA = CFG

21 Schematic of a FA The state control represents the states and transition function The state control represents the states and transition function Tape contains the input string Tape contains the input string Arrow represents the input head and points to the next symbol to be read Arrow represents the input head and points to the next symbol to be read State control aabb

22 Schematic of a PDA The PDA adds a stack The PDA adds a stack Can write to the stack and read them back later Can write to the stack and read them back later Write to the top (push) and rest “push down” or Write to the top (push) and rest “push down” or Can remove from the top (pop) and other symbols move up Can remove from the top (pop) and other symbols move up A stack is a LIFO (Last In First Out) and size is not bounded A stack is a LIFO (Last In First Out) and size is not bounded State control aabb x y z

23 PDA and Language 0 n 1 n Can a PDA recognize this? Can a PDA recognize this? Yes, because size of stack is not bounded Yes, because size of stack is not bounded Describe the PDA that recognizes this language Describe the PDA that recognizes this language Read symbols from input. Push each 0 onto the stack. Read symbols from input. Push each 0 onto the stack. As soon as a 1’s are seen, starting popping one 0 for each 1 As soon as a 1’s are seen, starting popping one 0 for each 1 If finish reading the input and have no 0’s on stack, then accept the input string If finish reading the input and have no 0’s on stack, then accept the input string If stack is empty and 1s remain or if stack becomes empty and still 1’s in string, reject If stack is empty and 1s remain or if stack becomes empty and still 1’s in string, reject If at any time see a 0 after seeing a 1, then reject If at any time see a 0 after seeing a 1, then reject

24 Formal Definition of a PDA The formal definition of a PDA is similar to that of a FA but now we have a stack The formal definition of a PDA is similar to that of a FA but now we have a stack Stack alphabet may be different from input alphabet Stack alphabet may be different from input alphabet Stack alphabet represented by Γ Stack alphabet represented by Γ Transition function key part of definition Transition function key part of definition Domain of transition function is Q ×  ε × Γε Domain of transition function is Q ×  ε × Γε The current state, next input symbol and top stack symbol determine the next move The current state, next input symbol and top stack symbol determine the next move

25 Definition of PDA A pushdown automata is a 6-tuple (Q, , Γ, δ, q 0, F), where Q, , Γ, and F are finite sets A pushdown automata is a 6-tuple (Q, , Γ, δ, q 0, F), where Q, , Γ, and F are finite sets 1. Q is the set of states 2.  is the input alphabet 3. Γ is the stack alphabet 4. δ : Q ×  ε × Γε  P(Q × Γε) is transition function 5. q 0  Q is the start state, and 6. F  Q is the set of accept states Note that at any step the PDA may enter a new state and possibly write a symbol on top of the stack Note that at any step the PDA may enter a new state and possibly write a symbol on top of the stack This definition allows nondeterminism since δ can return a set This definition allows nondeterminism since δ can return a set

26 How Does a PDA Compute? The following 3 conditions must be satisfied for a string to be accepted: The following 3 conditions must be satisfied for a string to be accepted: 1. M must start in the start state with an empty stack 2. M must move according to the transition function 3. At the end of the input, M must be in an accept state To make it easy to test for an empty stack, a $ is initially pushed onto the stack To make it easy to test for an empty stack, a $ is initially pushed onto the stack If you see a $ at the top of the stack, you know it is empty If you see a $ at the top of the stack, you know it is empty

27 Notation We write a,b  c to mean: We write a,b  c to mean: when the machine is reading an a from the input when the machine is reading an a from the input it may replace the b on the top of the stack with c it may replace the b on the top of the stack with c Any of a, b, or c can be ε Any of a, b, or c can be ε If a is ε then can make stack change without reading an input symbol If a is ε then can make stack change without reading an input symbol If b is ε then no need to pop a symbol (just push c) If b is ε then no need to pop a symbol (just push c) If c is ε then no new symbol is written (just pop b) If c is ε then no new symbol is written (just pop b)

28 0, ε  0 1, 0  ε ε, ε  $ ε, $  ε Example 1: a PDA for 0 n 1 n Formally describe PDA that accepts {0 n 1 n |n ≥0} Formally describe PDA that accepts {0 n 1 n |n ≥0} Let M1 be (Q, , Γ, δ, q 0, F), where Let M1 be (Q, , Γ, δ, q 0, F), where Q = {q 1, q 2, q 3, q 4 }  = {0, 1} Q = {q 1, q 2, q 3, q 4 }  = {0, 1} Γ = {0, $} F = {q 1, q 4 } Γ = {0, $} F = {q 1, q 4 } q1 q4 q2 q3 1, 0  ε

29 Example 2: PDA for a i b j c k, i=j or i=k Come up with a PDA that recognizes the language {a i b j c k | i, j, k ≥0 and i=j or i=k} Come up with a PDA that recognizes the language {a i b j c k | i, j, k ≥0 and i=j or i=k} Come up with an informal description, as we did initially for 0 n 1 n Come up with an informal description, as we did initially for 0 n 1 n Can you do it without using non-determinism? Can you do it without using non-determinism? No No With non-determinism? With non-determinism? Easy, similar to 0 n 1 n except that guess whether to match a’s with b’s or c’s. See Figure 2.17 page 114 Easy, similar to 0 n 1 n except that guess whether to match a’s with b’s or c’s. See Figure 2.17 page 114 Since NPDA ≠ PDA, create a PDA if possible Since NPDA ≠ PDA, create a PDA if possible

30 Example 3: PDA for {ww R } Come up with a PDA for the following language: {ww R | w  {0,1}* } Come up with a PDA for the following language: {ww R | w  {0,1}* } Recall that w R is the reverse of w so this is the language of palindromes Recall that w R is the reverse of w so this is the language of palindromes Can you informally describe the PDA? Can you come up with a deterministic one? Can you informally describe the PDA? Can you come up with a deterministic one? No No Can you come up with a non-deterministic one? Can you come up with a non-deterministic one? Yes, push symbols that are read onto the stack and at some point nondeterministically guess that you are in the middle of the string and then pop off stack value as they match the input (if no match, then reject) Yes, push symbols that are read onto the stack and at some point nondeterministically guess that you are in the middle of the string and then pop off stack value as they match the input (if no match, then reject)

31 Diagram of PDA for {ww R } 0, ε  0 1, ε  1 0, 0  ε 1, 1  ε ε, ε  $ ε, $  ε q1 q4 q2 q3 ε, ε  ε

32 Equivalence with CFGs Theorem: A language is context free if and only if some pushdown automaton recognizes it Theorem: A language is context free if and only if some pushdown automaton recognizes it Lemma: if a language L is context free then some PDA recognizes it (we won’t bother doing other direction) Lemma: if a language L is context free then some PDA recognizes it (we won’t bother doing other direction) Proof idea: We show how to take a CFG that generates L and convert it into an equivalent PDA P Proof idea: We show how to take a CFG that generates L and convert it into an equivalent PDA P Thus P accepts a string only if the CFG can derive it Thus P accepts a string only if the CFG can derive it Each main step of the PDA involves an application of one rule in the CFG Each main step of the PDA involves an application of one rule in the CFG The stack contains the intermediate strings generated by the CFG The stack contains the intermediate strings generated by the CFG Since the CFG may have a choice of rules to apply, the PDA must use its non- determinism Since the CFG may have a choice of rules to apply, the PDA must use its non- determinism One issue: since the PDA can only access the top of the stack, any terminal symbols pushed onto the top of the stack must be checked against the input string immediately. One issue: since the PDA can only access the top of the stack, any terminal symbols pushed onto the top of the stack must be checked against the input string immediately. If the terminal symbol matches the next input character, then advance input string If the terminal symbol matches the next input character, then advance input string If the terminal symbol does not match, then terminate that path If the terminal symbol does not match, then terminate that path

33 Informal Description of P Place marker symbol $ and the start variable on the stack Place marker symbol $ and the start variable on the stack Repeat forever Repeat forever If the top of the stack is a variable A, nondeterministically select one of the rules for A and substitute A by the string on the right-hand side of the rule If the top of the stack is a variable A, nondeterministically select one of the rules for A and substitute A by the string on the right-hand side of the rule If the top of stack is a terminal symbol a, read the next symbol from the input and compare it to a. If they match, repeat. If they do not match, reject this branch. If the top of stack is a terminal symbol a, read the next symbol from the input and compare it to a. If they match, repeat. If they do not match, reject this branch. If the top of stack is the symbol $, enter the accept state. Doing so accepts the input if it has all been read. If the top of stack is the symbol $, enter the accept state. Doing so accepts the input if it has all been read.

34 Example Look at Example 2.25 on page 118 for an example of constructing a PDA P1 from a CFG G Look at Example 2.25 on page 118 for an example of constructing a PDA P1 from a CFG G Note that the top path that branches to the right will take S and replace it with aTb Note that the top path that branches to the right will take S and replace it with aTb It first pushes b, then T, then a It first pushes b, then T, then a Note the path below that replaces T with Ta Note the path below that replaces T with Ta It replaces T with a then pops T on top of that It replaces T with a then pops T on top of that Your task: Your task: Show how this PDA accepts the string aab, which has the following derivation: Show how this PDA accepts the string aab, which has the following derivation: S  aTb  aTab  aab S  aTb  aTab  aab

35 Example continued In the following, the left is the top of stack In the following, the left is the top of stack We start with S$ We start with S$ We take the top branch to the right and we get the following as we go thru each state: We take the top branch to the right and we get the following as we go thru each state: S$  b$  Tb$  aTb$ S$  b$  Tb$  aTb$ We read a and use rule a,a  ε to pop it to get Tb$ We read a and use rule a,a  ε to pop it to get Tb$ We next take the 2 nd branch going to right: We next take the 2 nd branch going to right: Tb$  ab$  Tab$ Tb$  ab$  Tab$ We next use rule ε,T  ε to pop T to get ab$ We next use rule ε,T  ε to pop T to get ab$ Then we pop a then pop b at which point we have $ Then we pop a then pop b at which point we have $ Everything read so accept Everything read so accept

36 Relationship of Regular Languages & CFLs We know that CFGs define CFLs We know that CFGs define CFLs We now know that a PDA recognizes the same class of languages and hence recognizes CFLs We now know that a PDA recognizes the same class of languages and hence recognizes CFLs We know that every PDA is a FA that just ignores the stack We know that every PDA is a FA that just ignores the stack Thus PDAs recognize regular languages Thus PDAs recognize regular languages Thus the class of CFLs contains regular languages Thus the class of CFLs contains regular languages But since we know that a FA is not as powerful as a PDA (e.g., 0 n 1 n ) we can say more But since we know that a FA is not as powerful as a PDA (e.g., 0 n 1 n ) we can say more CFLs and regular languages are not equivalent CFLs and regular languages are not equivalent

37 Relationship between CFLs and Regular Languages Regular languages Context Free Languages

38 Section 2.3 Non-Context Free Languages

39 Non Context Free Languages Just like there are languages that are not regular, there are languages that are not context free Just like there are languages that are not regular, there are languages that are not context free This means that they cannot be generated by a CFG This means that they cannot be generated by a CFG This means that they cannot be generated by a PDA This means that they cannot be generated by a PDA Just your luck! There is also a pumping lemma to prove that a language is not context free! Just your luck! There is also a pumping lemma to prove that a language is not context free!

40 Pumping Lemma for CFGs If A is a context-free language then there is a pumping length p where, if s is any string in A with length ≥ p, then s may be divided into five pieces x = uvxyz satisfying 3 conditions: If A is a context-free language then there is a pumping length p where, if s is any string in A with length ≥ p, then s may be divided into five pieces x = uvxyz satisfying 3 conditions: 1. For each i ≥0, uv i xy i z  A 2. |vy| > 0, and 3. |vxy| ≤ p

41 Proof Idea For regular languages we applied the pigeonhole principle to the number of states to show that a state had to be repeated. For regular languages we applied the pigeonhole principle to the number of states to show that a state had to be repeated. Here we apply the same principle to the number of variables in the CFG to show that some variable will need to be repeated given a sufficiently long string Here we apply the same principle to the number of variables in the CFG to show that some variable will need to be repeated given a sufficiently long string We will call this variable R and assume it can derive X We will call this variable R and assume it can derive X I don’t find the diagram on the next slide as obvious as the various texts suggest. However, if you first put the CFG into a specific form, then it is a bit clearer. I don’t find the diagram on the next slide as obvious as the various texts suggest. However, if you first put the CFG into a specific form, then it is a bit clearer. Mainly just be sure you can apply the pumping lemma Mainly just be sure you can apply the pumping lemma

42 Proof Idea Continued T  uRz R  x R  vRy Since we can keep applying rule R  vRy, we can derive uv i xy i z which therefore must also belong to the languages T R R uvx yz

43 Example I Use the pumping lemma to show that the language B = {a n b n c n | n ≥ 0} (Ex 2.36) Use the pumping lemma to show that the language B = {a n b n c n | n ≥ 0} (Ex 2.36) What string should we pick? What string should we pick? Select the string s = a p b p c p. S  B and |s| > p Select the string s = a p b p c p. S  B and |s| > p Condition 2 says that v and y cannot both be empty Condition 2 says that v and y cannot both be empty Using the books reasoning we can break things into two cases (note that |vxy| ≤ p). What should they be or how would you proceed? Using the books reasoning we can break things into two cases (note that |vxy| ≤ p). What should they be or how would you proceed? v and y each only contain one type of symbol (one symbol is left out) v and y each only contain one type of symbol (one symbol is left out) uv 2 xy 2 z cannot contain equal number of a’s, b’s and c’s uv 2 xy 2 z cannot contain equal number of a’s, b’s and c’s Either v or y contains more than one type of symbol Either v or y contains more than one type of symbol Pumping will violate the separation of a’s, b’s and c’s Pumping will violate the separation of a’s, b’s and c’s We used this reasoning before for regular languages We used this reasoning before for regular languages Using my reasoning Using my reasoning Since |vxy| ≤ p v and y contain at most 2 symbols and hence at least one is left out when pump up (technically we should say at least one symbols is in v or y so that pumping break the equality) Since |vxy| ≤ p v and y contain at most 2 symbols and hence at least one is left out when pump up (technically we should say at least one symbols is in v or y so that pumping break the equality)

44 Example II Let D = {ww | w  {0,1}*} (Ex 2.38) Let D = {ww | w  {0,1}*} (Ex 2.38) What string should we pick? What string should we pick? A possible choice is to choose s = {0 p 10 p 1} A possible choice is to choose s = {0 p 10 p 1} But this can be pumped– try generating uvxyz so it can be pumped But this can be pumped– try generating uvxyz so it can be pumped Hint: we need to straddle the middle for this to work Hint: we need to straddle the middle for this to work Solution: u=0 p-1, v=0, x=1, y=0, z=0 p-1 1 Solution: u=0 p-1, v=0, x=1, y=0, z=0 p-1 1 Check it. Does uv 2 xy 2 z  D? Does uwz  D? Check it. Does uv 2 xy 2 z  D? Does uwz  D? Choose s = {0 p 1 p 0 p 1 p } Choose s = {0 p 1 p 0 p 1 p } First note that vxy must straddle midpoint. Otherwise pumping makes 1st half ≠ 2nd half First note that vxy must straddle midpoint. Otherwise pumping makes 1st half ≠ 2nd half Book says another way: if vxy on left, pumping it moves a 1 into second half and if on right, moves 0 into first half Book says another way: if vxy on left, pumping it moves a 1 into second half and if on right, moves 0 into first half since |vxy| < p, it is all 1’s in left case and all 0’s in right case since |vxy| < p, it is all 1’s in left case and all 0’s in right case If does straddle midpoint, if pump down, then not of form ww since neither 1’s or 0’s match in each half If does straddle midpoint, if pump down, then not of form ww since neither 1’s or 0’s match in each half

45 Summary of Pumping Lemma for CFGs Just remember the basics and the conditions Just remember the basics and the conditions You should memorize them for both pumping lemmas, but think about what they mean You should memorize them for both pumping lemmas, but think about what they mean I may give you the pumping lemma definitions on the exam, but perhaps not. You should remember both pumping lemmas and the conditions I may give you the pumping lemma definitions on the exam, but perhaps not. You should remember both pumping lemmas and the conditions They should not be too hard to remember They should not be too hard to remember