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

Computer Language Theory - Fordhamstorm.cis.fordham.edu/~gweiss/classes/cisc4090/slides/Chapter-2...Computer Language Theory ... methods for describing a language: Finite Automata

Embed Size (px)

Citation preview

1

Computer Language Computer Language

TheoryTheory

Chapter 2: ContextChapter 2: Context--Free LanguagesFree Languages

2

OverviewOverview

In Chapter 1 we introduced two equivalent In Chapter 1 we introduced two equivalent methods for describing a language: Finite methods for describing a language: Finite Automata and Regular ExpressionsAutomata and Regular Expressions

In this chapter we do something analogousIn this chapter we do something analogous

We introduce context free grammars (CFGs)We introduce context free grammars (CFGs)

We introduce pushWe introduce push--down automata (PDA)down automata (PDA)

PDAs recognize CFGsPDAs recognize CFGs

In my view the order is reversed from before since the In my view the order is reversed from before since the PDA is introduced secondPDA is introduced second

We even have another pumping lemma (Yeah!)We even have another pumping lemma (Yeah!)

3

Why Context Free GrammarsWhy Context Free Grammars

They were first used to study human languagesThey were first used to study human languages

You may have even seen something like them beforeYou may have even seen something like them before

They are definitely used for “real” computer They are definitely used for “real” computer

languages (C, C++, etc.)languages (C, C++, etc.)

They define the languageThey define the language

A parser uses the grammar to parse the inputA parser uses the grammar to parse the input

Of course you can also parse EnglishOf course you can also parse English

4

Section 2.1Section 2.1

ContextContext--Free GrammarsFree Grammars

5

A ContextA Context--Free GrammarFree Grammar

Here is an example grammar G1Here is an example grammar G1 A A 0A10A1

A A BB

B B ##

A grammar has substitution rules or productionsA grammar has substitution rules or productions

Each rule has a variable and arrow and a combination of Each rule has a variable and arrow and a combination of

variables and terminal symbolsvariables and terminal symbols

We will capitalize symbols but not terminalsWe will capitalize symbols but not terminals

A special variable is the start variableA special variable is the start variable

Usually on the leftUsually on the left--hand side of topmost rulehand 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 GrammarUsing the Grammar

Use the grammar to generate a language by Use the grammar to generate a language by replacing variables using the rules in the replacing variables using the rules in the grammargrammar

Start with the start variableStart with the start variable

Give me some strings that grammar G1 generates?Give me some strings that grammar G1 generates?

One answer: 000#111One answer: 000#111

The sequence of steps is the derivationThe sequence of steps is the derivation

For this example the derivation is:For this example the derivation is:

A A 0A1 0A1 00A11 00A11 000A111 000A111 000B111 000B111 000#111000#111

You can also represent this with a parse treeYou can also represent this with a parse tree

7

The Language of Grammar G1The Language of Grammar G1

All strings generated by G1 form the languageAll 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) = {0L(G1) = {0nn#1#1nn| n ≥0}| n ≥0}

This should look familiar. Can we generate this with This should look familiar. Can we generate this with

a FA?a FA?

8

An Example English GrammarAn Example English Grammar

Page 101 of the text has a simplified English Page 101 of the text has a simplified English

grammargrammar

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 CFGFormal Definition of a CFG

A CFG is a 4A CFG is a 4--tuple (V, tuple (V, , R, S) where, R, S) where

1.1. V is a finite set called the variablesV is a finite set called the variables

2.2. is a finite set, disjoint from V, called the is a finite set, disjoint from V, called the

terminalsterminals

3.3. R is a finite set of rules, with each rule being a R is a finite set of rules, with each rule being a

variable and a string of variables and terminals, andvariable and a string of variables and terminals, and

4.4. S S V is the start variableV is the start variable

10

ExampleExample

Grammar G3 = ({S}, {a,b}, R, S), where the set Grammar G3 = ({S}, {a,b}, R, S), where the set

of rules R is:of rules R is:

S S aSb | SS | aSb | SS | εε

What does this generate:What does this generate:

abab, aaabbb, aababbabab, aaabbb, aababb

If you view a as “(“ and b as “)” then you get all strings of If you view a as “(“ and b as “)” then you get all strings of

properly nested parenthesesproperly nested parentheses

Note they consider ()() to be okayNote they consider ()() to be okay

I think the key property here is that at any point in the string you 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’shave at least as many a’s to the left of it as b’s

11

Another ExampleAnother Example

Consider example 2.4 in text on page 103Consider example 2.4 in text on page 103

12

Designing CFGsDesigning CFGs

Like designing FA, some creativity is requiredLike designing FA, some creativity is required

It is probably even harder with CFGs since they are more It is probably even harder with CFGs since they are more expressive than FA (we will show that soon)expressive than FA (we will show that soon)

Here are some guidelinesHere are some guidelines If the CFL is the union of simpler CFLs, design grammars for the If the CFL is the union of simpler CFLs, design grammars for the

simpler ones and then combinesimpler ones and then combine

For example, S For example, S G1 | G2 | G3G1 | G2 | G3

If the language is regular, then can design a CFG that mimics a DFAIf the language is regular, then can design a CFG that mimics a DFA

Make a variable Ri for every state qiMake a variable Ri for every state qi

If If δδ(qi, a) = qj, then add Ri (qi, a) = qj, then add Ri aRjaRj

Add Ri Add Ri εε if i is an accept stateif i is an accept state

Make R0 the start variable where q0 is the start state of the DFAMake 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 continuedDesigning CFGs continued

A final guideline: A final guideline:

Certain CFLs contain strings that are linked in the Certain CFLs contain strings that are linked in the

sense that a machine for recognizing this language sense that a machine for recognizing this language

would need to remember an unbounded amount of would need to remember an unbounded amount of

information about one substring to “verify” the information about one substring to “verify” the

other substring.other substring.

This is sometimes trivial with a CFGThis is sometimes trivial with a CFG

Example: 0Example: 0nn11nn

S S 0S1 | 0S1 | εε

14

AmbiguityAmbiguity

Sometimes a grammar can generate the same Sometimes a grammar can generate the same

string in multiple waysstring in multiple ways

If a grammar generates even a single string in multiple If a grammar generates even a single string in multiple

ways the grammar is ambiguousways the grammar is ambiguous

Example:Example:

EXPR EXPR EXPR + EXPR | EXPR EXPR + EXPR | EXPR ×× EXPR |(EXPR) | aEXPR |(EXPR) | a

This generates the string a+a This generates the string a+a ×× a ambiguouslya ambiguously

Try it: generate two parse treesTry it: generate two parse trees

Using your extensive knowledge of arithmetic, insert Using your extensive knowledge of arithmetic, insert

parentheses to shows what each parse tree really representsparentheses to shows what each parse tree really represents

15

An English ExampleAn English Example

Grammar G2 on page 101 ambiguously Grammar G2 on page 101 ambiguously

generates generates the girl touches the boy with the flowerthe girl touches the boy with the flower

Using your extensive knowledge of English, Using your extensive knowledge of English,

what are the two meanings of this phrasewhat are the two meanings of this phrase

16

Definition of AmbiguityDefinition of Ambiguity

A grammar generates a string ambiguously if A grammar generates a string ambiguously if there are two different parse treesthere are two different parse trees

Two derivations may differ in the order that the Two derivations may differ in the order that the rules are applied, but if they generate the same parse rules are applied, but if they generate the same parse tree, it is not really ambiguoustree, it is not really ambiguous

Definitions: Definitions:

A derivation is a A derivation is a leftmost derivationleftmost derivation if at every step the if at every step the leftmost remaining variable is replacedleftmost remaining variable is replaced

A string A string ww is derived is derived ambiguouslyambiguously in a CFG G if it has in a CFG G if it has two or more different leftmost derivations.two or more different leftmost derivations.

17

Chomsky Normal FormChomsky Normal Form

It is often convenient to convert a CFG into a It is often convenient to convert a CFG into a simplified formsimplified form

A CFG is in Chomsky normal form if every rule A CFG is in Chomsky normal form if every rule is of the form:is of the form:

A A BCBC

A A aa

Where a is any terminal and A, B, and C are any variablesWhere a is any terminal and A, B, and C are any variables––except B and C may not be the start variable. The start except B and C may not be the start variable. The start variable can also go to variable can also go to εε

Any CFL can be generated by a CFG in Chomsky Any CFL can be generated by a CFG in Chomsky normal formnormal form

18

Converting CFG to Chomsky Converting CFG to Chomsky

Normal FormNormal Form

Here are the steps:Here are the steps:

Add rule Add rule SS00 S, where S was original start variableS, where S was original start variable

Remove Remove εε--rules. Remove A rules. Remove A εε and for each occurrence of A and for each occurrence of A

add a new rule with A deleted.add a new rule with A deleted.

If we have R If we have R uAvAw, we get:uAvAw, we get:

R R uvAw | uAvw | uvwuvAw | uAvw | uvw

Handle all unit rulesHandle all unit rules

If we had A If we had A B, then whenever a rule B B, then whenever a rule B u exists, we add A u exists, we add A u.u.

Replace rules A Replace rules A uu11uu22uu33… u… ukk with:with:

A A uu11AA11, A, A11 uu22AA22, A, A22 uu33AA33 … A… Akk--22 uukk--11uukk

You will have a HW question like thisYou 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.2Section 2.2

Pushdown AutomataPushdown Automata

20

Pushdown Automata (PDA)Pushdown Automata (PDA)

Similar to NFAs but have an extra component Similar to NFAs but have an extra component called a called a stackstack

The stack provides extra memory that is separate The stack provides extra memory that is separate from the controlfrom the control

Allows PDA to recognize nonAllows PDA to recognize non--regular languagesregular languages

Equivalent in power/expressiveness to a CFGEquivalent in power/expressiveness to a CFG

Some languages easily described by generators Some languages easily described by generators others by recognizersothers by recognizers

Nondeterministic PDA’s Nondeterministic PDA’s notnot equivalent to equivalent to deterministic ones but NPDA = CFGdeterministic ones but NPDA = CFG

21

Schematic of a FASchematic of a FA

The state control represents the states and The state control represents the states and transition functiontransition function

Tape contains the input stringTape contains the input string

Arrow represents the input head and points to Arrow represents the input head and points to the next symbol to be readthe next symbol to be read

State

control a a b b

22

Schematic of a PDASchematic of a PDA

The PDA adds a stackThe PDA adds a stack

Can write to the stack and read them back laterCan write to the stack and read them back later

Write to the top (push) and rest “push down” orWrite to the top (push) and rest “push down” or

Can remove from the top (pop) and other symbols move upCan remove from the top (pop) and other symbols move up

A stack is a LIFO (Last In First Out) and size is A stack is a LIFO (Last In First Out) and size is notnot boundedbounded

State

control a a b b

x

y

z

23

PDA and Language 0PDA and Language 0nn11nn

Can a PDA recognize this?Can a PDA recognize this?

Yes, because size of stack is not boundedYes, because size of stack is not bounded

Describe the PDA that recognizes this languageDescribe 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 1As 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 If finish reading the input and have no 0’s on stack, then

accept the input stringaccept the input string

If stack is empty and 1s remain or if stack becomes empty If stack is empty and 1s remain or if stack becomes empty

and still 1’s in string, rejectand still 1’s in string, reject

If at any time see a 0 after seeing a 1, then rejectIf at any time see a 0 after seeing a 1, then reject

24

Formal Definition of a PDAFormal Definition of a PDA

The formal definition of a PDA is similar to that The formal definition of a PDA is similar to that

of a FA but now we have a stackof a FA but now we have a stack

Stack alphabet may be different from input alphabetStack alphabet may be different from input alphabet

Stack alphabet represented by Stack alphabet represented by ΓΓ

Transition function key part of definitionTransition 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 The current state, next input symbol and top stack symbol

determine the next movedetermine the next move

25

Definition of PDADefinition of PDA

A pushdown automata is a 6A pushdown automata is a 6--tuple (Q, tuple (Q, , , ΓΓ, , δδ, , qq00, F), where Q, , F), where Q, , , ΓΓ, and F are finite sets, and F are finite sets

1.1. Q is the set of statesQ is the set of states

2.2. is the input alphabetis the input alphabet

3.3. ΓΓ is the stack alphabetis the stack alphabet

4.4. δ δ : Q : Q ×× εε ×× ΓΓεε P(Q P(Q ×× ΓΓεε) is transition function ) is transition function

5.5. qq00 Q is the start state, andQ is the start state, and

6.6. F F Q is the set of accept statesQ is the set of accept states

Note that at any step the PDA may enter a new state Note that at any step the PDA may enter a new state and possibly write a symbol on top of the stackand possibly write a symbol on top of the stack

This definition allows nondeterminism since This definition allows nondeterminism since δδ can return can return a seta set

26

How Does a PDA Compute?How Does a PDA Compute?

The following 3 conditions must be satisfied for The following 3 conditions must be satisfied for

a string to be accepted:a string to be accepted:

1.1. M must start in the start state with an empty stackM must start in the start state with an empty stack

2.2. M must move according to the transition functionM must move according to the transition function

3.3. At the end of the input, M must be in an accept stateAt the end of the input, M must be in an accept state

To make it easy to test for an empty stack, a $ is To make it easy to test for an empty stack, a $ is

initially pushed onto the stackinitially pushed onto the stack

If you see a $ at the top of the stack, you know it is emptyIf you see a $ at the top of the stack, you know it is empty

27

NotationNotation

We write a,b We write a,b c to mean:c to mean:

when the machine is reading an a from the inputwhen the machine is reading an a from the input

it may replace the b on the top of the stack with cit 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 If a is εε then can make stack change without reading an then can make stack change without reading an

input symbolinput symbol

If b is If b is εε then no need to pop a symbol (just push c)then no need to pop a symbol (just push c)

If c is If c is εε then no new symbol is written (just pop b)then no new symbol is written (just pop b)

28

0, ε 0

1, 0 ε

ε, ε $

ε, $ ε

Example 1: a PDA for 0Example 1: a PDA for 0nn11nn

Formally describe PDA that accepts {0Formally describe PDA that accepts {0nn11nn|n ≥0}|n ≥0}

Let M1 be (Q, Let M1 be (Q, , , ΓΓ, , δδ, q, q00, F), where, F), where

Q = {qQ = {q11, q, q22, q, q33, q, q44} } = {0, 1}= {0, 1}

ΓΓ = {0, $} F = {q= {0, $} F = {q11, q, q44}}

q1

q4

q2

q3

1, 0 ε

29

Example 2: PDA for aExample 2: PDA for aiibbjjcckk, i=j or i=k, i=j or i=k

Come up with a PDA that recognizes the Come up with a PDA that recognizes the

language {alanguage {aiibbjjcckk| i, j, k ≥0 and i=j or i=k}| i, j, k ≥0 and i=j or i=k}

Come up with an informal description, as we did Come up with an informal description, as we did

initially for 0initially for 0nn11nn

Can you do it without using nonCan you do it without using non--determinism?determinism?

NoNo

With nonWith non--determinism?determinism?

Easy, similar to 0Easy, similar to 0nn11n n except that guess whether to match a’s with except that guess whether to match a’s with

b’s or c’s. See Figure 2.17 page 114b’s or c’s. See Figure 2.17 page 114

Since NPDA ≠ PDA, create a PDA if possibleSince NPDA ≠ PDA, create a PDA if possible

30

Example 3: PDA for {wwExample 3: PDA for {wwRR}}

Come up with a PDA for the following Come up with a PDA for the following language: {wwlanguage: {wwRR| w | w {0,1}* }{0,1}* }

Recall that wRecall that wRR is the reverse of w so this is the is the reverse of w so this is the language of palindromeslanguage of palindromes

Can you informally describe the PDA? Can you Can you informally describe the PDA? Can you come up with a deterministic one?come up with a deterministic one?

NoNo

Can you come up with a nonCan you come up with a non--deterministic one?deterministic one?

Yes, push symbols that are read onto the stack and at Yes, push symbols that are read onto the stack and at some point nondeterministically guess that you are in the some point nondeterministically guess that you are in the middle of the string and then pop off stack value as they middle of the string and then pop off stack value as they match the input (if no match, then reject)match the input (if no match, then reject)

31

Diagram of PDA for {wwDiagram of PDA for {wwRR}}

0, ε 0

1, ε 1

0, 0 ε

1, 1 ε

ε, ε $

ε, $ ε

q1

q4

q2

q3

ε, ε ε

32

Equivalence with CFGsEquivalence with CFGs

Theorem: A language is context free if and only if some pushdown Theorem: A language is context free if and only if some pushdown automaton recognizes itautomaton recognizes it Lemma: if a language L is context free then some PDA recognizes it (we Lemma: if a language L is context free then some PDA recognizes it (we

won’t bother doing other direction)won’t bother doing other direction)

Proof idea: We show how to take a CFG that generates L and convert it into Proof idea: We show how to take a CFG that generates L and convert it into an equivalent PDA Pan equivalent PDA P

Thus P accepts a string only if the CFG can derive itThus 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 CFGEach main step of the PDA involves an application of one rule in the CFG

The stack contains the intermediate strings generated by the CFGThe 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 nonSince the CFG may have a choice of rules to apply, the PDA must use its non--determinismdeterminism

One issue: since the PDA can only access the top of the stack, any terminal 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 symbols pushed onto the top of the stack must be checked against the input string immediately.immediately.

If the terminal symbol matches the next input character, then advance input stringIf the terminal symbol matches the next input character, then advance input string

If the terminal symbol does not match, then terminate that pathIf the terminal symbol does not match, then terminate that path

33

Informal Description of P Informal Description of P

Place marker symbol $ and the start variable on the stackPlace marker symbol $ and the start variable on the stack

Repeat foreverRepeat forever

If the top of the stack is a variable A, nondeterministically 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 select one of the rules for A and substitute A by the string on

the rightthe right--hand side of the rulehand side of the rule

If the top of stack is a terminal symbol a, read the next symbol 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 from the input and compare it to a. If they match, repeat. If

they do not match, reject this branch.they do not match, reject this branch.

If the top of stack is the symbol $, enter the accept state. If the top of stack is the symbol $, enter the accept state.

Doing so accepts the input if it has all been read. Doing so accepts the input if it has all been read.

34

ExampleExample

Look at Example 2.25 on page 118 for an example of Look at Example 2.25 on page 118 for an example of

constructing a PDA P1 from a CFG Gconstructing a PDA P1 from a CFG G

Note that the top path that branches to the right will take S and Note that the top path that branches to the right will take S and

replace it with aTbreplace it with aTb

It first pushes b, then T, then aIt first pushes b, then T, then a

Note the path below that replaces T with TaNote the path below that replaces T with Ta

It replaces T with a then pops T on top of thatIt 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 Show how this PDA accepts the string aab, which has the following

derivation:derivation:

S S aTb aTb aTab aTab aabaab

35

Example continuedExample continued

In the following, the left is the top of stackIn 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 We take the top branch to the right and we get the following as we go thru each state:following as we go thru each state:

S$ S$ b$ b$ Tb$ Tb$ aTb$aTb$

We read a and use rule a,a We read a and use rule a,a εε to pop it to get Tb$to pop it to get Tb$

We next take the 2We next take the 2ndnd branch going to right:branch going to right:

Tb$ Tb$ ab$ ab$ Tab$Tab$

We next use rule We next use rule εε,T ,T εε to pop T to get ab$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 acceptEverything read so accept

36

Relationship of Regular Languages & CFLsRelationship of Regular Languages & CFLs

We know that CFGs define CFLsWe know that CFGs define CFLs

We now know that a PDA recognizes the same class of We now know that a PDA recognizes the same class of languages and hence recognizes CFLslanguages and hence recognizes CFLs

We know that every PDA is a FA that just ignores the We know that every PDA is a FA that just ignores the stackstack

Thus PDAs recognize regular languagesThus PDAs recognize regular languages

Thus the class of CFLs contains regular languagesThus the class of CFLs contains regular languages

But since we know that a FA is not as powerful as a But since we know that a FA is not as powerful as a PDA (e.g., 0PDA (e.g., 0nn11nn) we can say more) we can say more

CFLs and regular languages are not equivalentCFLs and regular languages are not equivalent

37

Relationship between CFLs and Relationship between CFLs and

Regular LanguagesRegular Languages

Regular

languages

Context Free

Languages

38

Section 2.3Section 2.3

NonNon--Context Free LanguagesContext Free Languages

39

Non Context Free LanguagesNon Context Free Languages

Just like there are languages that are not regular, Just like there are languages that are not regular,

there are languages that are not context freethere are languages that are not context free

This means that they cannot be generated by a CFGThis means that they cannot be generated by a CFG

This means that they cannot be generated by a PDAThis means that they cannot be generated by a PDA

Just your luck! There is also a pumping lemma Just your luck! There is also a pumping lemma

to prove that a language is not context free!to prove that a language is not context free!

40

Pumping Lemma for CFGsPumping Lemma for CFGs

If A is a contextIf A is a context--free language then there is a free language then there is a

pumping length p where, if s is any string in A pumping length p where, if s is any string in A

with length ≥ p, then s may be divided into with length ≥ p, then s may be divided into

five pieces x = uvxyz satisfying 3 conditions:five pieces x = uvxyz satisfying 3 conditions:

1.1. For each i ≥0, uvFor each i ≥0, uviixyxyiiz z AA

2.2. |vy| > 0, and|vy| > 0, and

3.3. |vxy| ≤ p|vxy| ≤ p

41

Proof IdeaProof Idea

For regular languages we applied the pigeonhole For regular languages we applied the pigeonhole

principle to the number of states to show that a state principle to the number of states to show that a state

had to be repeated.had to be repeated.

Here we apply the same principle to the number of variables Here we apply the same principle to the number of variables

in the CFG to show that some variable will need to be in the CFG to show that some variable will need to be

repeated given a sufficiently long stringrepeated given a sufficiently long string

We will call this variable R and assume it can derive XWe 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 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 various texts suggest. However, if you first put the CFG into

a specific form, then it is a bit clearer.a specific form, then it is a bit clearer.

Mainly just be sure you can apply the pumping lemmaMainly just be sure you can apply the pumping lemma

42

Proof Idea ContinuedProof Idea Continued

T T uRz uRz R R x R x R vRyvRy Since we can keep applying rule RSince we can keep applying rule R vRy, we can derive vRy, we can derive uvuviixyxyiiz z

which therefore must also belong to the languageswhich therefore must also belong to the languages

T

R

R

u v x y z

43

Example IExample I

Use the pumping lemma to show that the language B = {aUse the pumping lemma to show that the language B = {annbbnnccnn | n ≥ 0} (Ex 2.36)| n ≥ 0} (Ex 2.36) What string should we pick?What string should we pick?

Select the string s = aSelect the string s = appbbppccpp. S . S B and |s| > pB and |s| > p

Condition 2 says that v and y cannot both be emptyCondition 2 says that v and y cannot both be empty

Using the books reasoning we can break things into two cases (note that Using the books reasoning we can break things into two cases (note that |vxy| ≤ p). What should they be or how would you proceed?|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)

uvuv22xyxy22z cannot contain equal number of a’s, b’s and c’sz cannot contain equal number of a’s, b’s and c’s

Either v or y contains more than one type of symbolEither v or y contains more than one type of symbol Pumping will violate the separation of a’s, b’s and c’sPumping will violate the separation of a’s, b’s and c’s

We used this reasoning before for regular languagesWe used this reasoning before for regular languages

Using my reasoningUsing my reasoning Since |vxy| ≤ p v and y contain at most 2 symbols and hence at least one is 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 left out when pump up (technically we should say at least one symbols is in v or y so that pumping break the equality)or y so that pumping break the equality)

44

Example IIExample II Let D = {ww | w Let D = {ww | w {0,1}*} (Ex 2.38){0,1}*} (Ex 2.38)

What string should we pick?What string should we pick?

A possible choice is to choose s = {0A possible choice is to choose s = {0pp1010pp1}1} But this can be pumpedBut this can be pumped–– try generating uvxyz so it can be pumpedtry generating uvxyz so it can be pumped

Hint: we need to straddle the middle for this to workHint: we need to straddle the middle for this to work

Solution: u=0Solution: u=0pp--11, v=0, x=1, y=0, z=0, v=0, x=1, y=0, z=0pp--1111

Check it. Does uvCheck it. Does uv22xyxy22z z D? Does uwz D? Does uwz D? D?

Choose s = {0Choose s = {0pp11pp00pp11pp}} First note that vxy must straddle midpoint. Otherwise pumping makes 1st First note that vxy must straddle midpoint. Otherwise pumping makes 1st

half ≠ 2nd halfhalf ≠ 2nd half

Book says another way: if vxy on left, pumping it moves a 1 into second 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 halfand 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 casesince |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 If does straddle midpoint, if pump down, then not of form ww since neither 1’s or 0’s match in each halfneither 1’s or 0’s match in each half

45

Summary of Pumping Lemma for CFGsSummary of Pumping Lemma for CFGs

Just remember the basics and the conditionsJust remember the basics and the conditions

You should memorize them for both pumping You should memorize them for both pumping

lemmas, but think about what they meanlemmas, but think about what they mean

I may give you the pumping lemma definitions on I may give you the pumping lemma definitions on

the exam, but perhaps not. You should remember the exam, but perhaps not. You should remember

both pumping lemmas and the conditions both pumping lemmas and the conditions

They should not be too hard to rememberThey should not be too hard to remember