189
1 Module 28 • Context Free Grammars – Definition of a grammar G – Deriving strings and defining L(G) • Context-Free Language definition

1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

  • View
    230

  • Download
    1

Embed Size (px)

Citation preview

Page 1: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

1

Module 28

• Context Free Grammars– Definition of a grammar G– Deriving strings and defining L(G)

• Context-Free Language definition

Page 2: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

2

Context-Free Grammars

Definition

Page 3: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

3

Definition

• A context-free grammar G = (V, , S, P)– V: finite set of variables (nonterminals)

– : finite set of characters (terminals)

– S: start variable• element of V

• role is similar to that of q0 for an FSA or NFA

– P: finite set of grammar rules or production rules• Syntax of a production

• variable --> string of variables and terminals

Page 4: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

4

English Context-Free Grammar

• ECFG = (V, , S, P)– V = {<sentence>, <noun phrase>, <verb phrase>, ... }

• people sometimes use < > to delimit variables

• In this course, we generally will use capital letters to denote variables

– = {a, b, c, ..., z, ;, ,, ., ...}

– S = <sentence>

– P = { <sentence> --> <noun phrase> <verb phrase> <pct>, <noun phrase> --> <article> <adj> <noun>, ...}

Page 5: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

5

{aibi | i>0} CFG

• ABG = (V, , S, P)– V = {S}

– = {a, b}

– S = S

– P = {S --> aSb, S --> ab} or S --> aSb | ab• second format saves some space

Page 6: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

6

Context-Free Grammars

Deriving strings, defining L(G), and defining context-free languages

Page 7: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

7

Defining -->, ==> notation• First: --> notation

– This is used to define the productions of a grammar• S --> aSb | ab

• Second: ==>G notation– This is used to denote the application of a production rule

from a grammar G• S ==>ABG aSb ==>ABG aaSbb ==>ABG aaabbb

– We say that string S derives string aSb (in one step)– We say that string aSb derives string aaSbb (in one step)– We say that string aaSbb derives string aaabbb (in one step)

• We often omit the grammar subscript when the intended grammar is unambiguous

Page 8: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

8

Defining ==> continued

• Third: ==>kG notation

– This is used to denote k applications of production rules from a grammar G

• S ==>2ABG aaSbb

– We say that string S derives string aaSbb in two steps

• aSb ==>2ABG aaabbb

– We say that string aSb derives string aaabbb in two steps

• We often omit the grammar subscript when the intended grammar is unambiguous

Page 9: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

9

• Fourth: ==>*G notation

– This is used to denote 0 or more applications of production rules from a grammar G

• S ==>*ABG S

– We say that string S derives string S in 0 or more steps

• S ==>*ABG aaSbb

– We say that string S derives string aaSbb in 0 or more steps

• aSb ==>*ABG aaSbb

– We say that string aSb derives string aaSbb in 0 or more steps

• aSb ==>*ABG aaabbb

– We say that string aSb derives string aaabbb in 0 or more steps

• We often omit the grammar subscript when the intended grammar is unambiguous

Defining ==> continued

Page 10: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

10

• Derivation of a string x– The complete step by step derivation of a string x from

the start variable S

– Key fact: each step in a derivation makes only one application of a production rule from G

– Example: Derivation of string aaabbb using ABG• S ==>ABG aSb ==>ABG aaSbb ==>ABG aaabbb

– Example 2: AG= (V, , S, P) where P = S -->SS | a• Deriving string aaa

• S ==> SS ==> Sa ==> SSa ==> aSa ==> aaa

Defining derivations *

Page 11: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

11

• Generating strings– If S ==>G

* x, then grammar G generates string x• Note G generates strings which contain terminals and nonterminals

– aSb contains nonterminals and terminals– S contains only nonterminals– aaabbb contains only terminals

• L(G)– The set of strings over generated by grammar G

• Note we only consider terminal strings generated by G

– {aibi | i > 0} = L(ABG)– {ai | i > 0} = L(AG)

Defining L(G) *

Page 12: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

12

• Context-Free Languages– A language L is a context-free language (CFL) iff there

exists a CFG G such that L(G) = L

• Results so far– {ai | i > 0} is a CFL

• One CFG G such that L(G) = this language is AG

• Note this language is also regular

– {aibi | i > 0} is a CFL• One CFG G such that L(G) = this language is ABG

• Note this language is NOT regular

Context-Free Languages *

Page 13: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

13

Example *

• Let BAL = the set of strings over {(,)} in which the parentheses are balanced

• Prove that BAL is a CFL– To prove this, you need to come up with a CFG BALG

such that L(BALG) = BAL• BALG = (V, , S, P)

– V = {S}– = {(, )}– S = S– P = ?

• Give derivations of ((( ))) and ( )(( )) with your grammar

Page 14: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

14

Module 29

• Parse/Derivation Trees– Leftmost derivations, rightmost derivations

• Ambiguous Grammars– Examples

• Arithmetic expressions

• If-then-else Statements

– Inherently ambiguous CFL’s

Page 15: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

15

Context-Free Grammars

Parse Trees

Leftmost/rightmost derivations

Ambiguous grammars

Page 16: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

16

Parse Tree

• Parse/derivation trees are structured derivations– The structure graphically illustrates semantic

information about the string

• Formalization of concept we encountered in regular languages unit– Note, what we saw before were not exactly parse

trees as we define them now, but they were close

Page 17: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

17

Parse Tree Example

• Parse tree for string ( )(( )) and grammar BALG– BALG = (V, , S, P)

• V = {S}, = {(, )}, S = S

• P = S --> SS | (S) | – One derivation of ( )(( ))

• S ==> SS ==> (S)S ==> ( )S ==> ( )(S) ==> ( )((S)) ==> ( )(( ))

– Parse tree

S

S S

SS( ( ))

S( )

Page 18: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

18

Comments about Example *

• Syntax: – draw a unique arrow from each

variable to each character that is a direct child of that variable

• A line instead of an arrow is ok

– The derived string can be read in a left to right traversal of the leaves

• Semantics– The tree graphically illustrates the

nesting structure of the string of parentheses

S

S S

SS( ( ))

S( )

Page 19: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

19

Leftmost/Rightmost Derivations

• There is more than one derivation of the string ( )(( )).– S ==> SS ==> (S)S ==>( )S ==> ( )(S)

==> ( )((S)) ==> ( )(( ))– S ==> SS ==> (S)S ==> (S)(S) ==> ( )(S)

==> ( )((S)) ==> ( )(( ))– S ==> SS ==> S(S) ==> S((S)) ==> S(( ))

==> (S)(( )) ==>( )(( ))

• Leftmost derivation– Leftmost variable is always expanded– Which one of the above is leftmost?

• Rightmost derivation– Rightmost variable is always expanded– Which one of the above is rightmost?

S

S S

SS( ( ))

S( )

Page 20: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

20

Comments

• Fix a string and a grammar– Any derivation corresponds to a

unique parse tree

– Any parse tree can correspond to many different derivations

• Example– The one parse tree corresponds to all

three derivations

• Unique mappings– For any parse tree, there is a unique

leftmost/rightmost derivation that it corresponds to

S

S S

SS( ( ))

S( )

– S ==> SS ==> (S)S ==>( )S ==> ( )(S)

==> ( )((S)) ==> ( )(( ))

– S ==> SS ==> (S)S ==> (S)(S) ==> ( )(S)

==> ( )((S)) ==> ( )(( ))

– S ==> SS ==> S(S) ==> S((S)) ==> S(( ))

==> (S)(( )) ==>( )(( ))

Page 21: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

21

Example *

• S ==> SS ==> SSS ==> (S)SS ==> ( )SS ==> ( )S ==> ( )– The above is a leftmost derivation of the string ( ) from the

grammar BALG

– Draw the corresponding parse tree

– Draw the corresponding rightmost derivation

• S ==> (S) ==> (SS) ==> (S(S)) ==> (S( )) ==> (( ))– The above is a rightmost derivation of the string (( )) from the

grammar BALG

– Draw the corresponding parse tree

– Draw the corresponding leftmost derivation

Page 22: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

22

Ambiguous Grammars

Examples:

Arithmetic Expressions

If-then-else statements

Inherently ambiguous grammars

Page 23: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

23

Ambiguous Grammars

• A grammar G is ambiguous if there exists a string x in L(G) with two or more distinct parse trees– (2 or more distinct leftmost/rightmost derivations)

• Example– Grammar AG is ambiguous

• String aaa in L(AG) has 2 rightmost derivations– S ==> SS ==> SSS ==> SSa ==> Saa ==> aaa

– S ==> SS ==> Sa ==> SSa ==> Saa ==> aaa

Page 24: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

24

2 Simple Examples

• Grammar BALG is ambiguous– String ( ) in L(BALG) has >1 leftmost

derivation• S ==> (S) ==> ( )• S ==> (S) ==> (SS) ==>(S) ==>( )• Give another leftmost derivation of ( ) from BALG

• Grammar ABG is NOT ambiguous– Consider any string x in {aibi | i > 0}

• There is a unique parse tree for x

Page 25: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

25

Legal Arithmetic Expressions

• Develop a grammar MATHG = (V, , S, P) for the language of legal arithmetic expressions– = {0, 1, +, *, -, /, (, )}

– Strings in the language include• 0

• 10

• 10*11111+100

• 10*(11111+100)

– Strings not in the language include• 10+

• 11++101

• )(

Page 26: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

26

Grammar MATHG1

• V = {E, N}• = {0, 1, +, *, -, /, (, )}• S = E• P:

– E --> N | E+E | E*E | E/E | E-E | (E)

– N --> N0 | N1 | 0 | 1

Page 27: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

27

MATHG1 is ambiguous

• Come up with two distinct leftmost derivations of the string 11+0*11– E ==> E+E ==> N+E ==> N1+E ==> 11+E ==>

11+E*E ==> 11+N*E ==> 11+0*E ==> 11+0*N ==> 11+0*N1 ==> 11+0*11

– E ==> E*E ==> E+E*E ==> N+E*E ==> N1+E*E ==> 11+E*E ==> 11+N*E ==> 11+0*E ==> 11+0*N ==> 11+0*N1 ==>11+0*11

• Draw the corresponding parse trees

E --> N | E+E | E*E | E/E | E-E | (E)N --> N0 | N1 | 0 | 1

Page 28: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

28

Corresponding Parse Trees

• E ==> E+E ==> N+E ==> N1+E ==> 11+E ==> 11+E*E ==> 11+N*E ==> 11+0*E ==> 11+0*N ==> 11+0*N1 ==> 11+0*11

• E ==> E*E ==> E+E*E ==> N+E*E ==> N1+E*E ==> 11+E*E ==> 11+N*E ==> 11+0*E ==> 11+0*N ==> 11+0*N1 ==>11+0*11

E

E E+

E E*N

N

1

1 N

0

N

N 1

1

E

N

N

1

1

* EE

N

0

+ EE

N

N 1

1

Page 29: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

29

Parse Tree Meanings

E

E E+

E E*N

N

1

1 N

0

N

N 1

1

E

N

N

1

1

* EE

N

0

+ EE

N

N 1

1

Note how the parse trees captures the semantic meaning of string 11+0*11.More specifically, what number does the first parse tree represent?

What number does the second parse tree represent?

Page 30: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

30

Implications

• Two interpretations of string 11+0*11– 11+(0*11) = 11– (11+0)*11 = 1001

• What if a line in a program is– MSU_Tuition = 11+0*11;– What is MSU_Tuition?

• Depends on how the expression 11+0*11 is parsed.• This is not good.• Ambiguity in grammars is undesirable, particularly if the grammar is

used to develop a compiler for a programming language like C++.

• In this case, there is an unambiguous grammar for the language of arithmetic expressions

Page 31: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

31

If-Then-Else Statements

• A grammar ITEG = (V, , S, P) for the language of legal If-Then-Else statements– V = (S, BOOL)

– = {adv<80, adv>50, grade=3.5, grade=3.0, if, then, else}

– S = S

– P: • S --> if BOOL then S else S | if BOOL then S |grade=3.5 |

grade=3.0

• BOOL --> adv<80 | adv>50

Page 32: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

32

ITEG is ambiguous

• Come up with two distinct leftmost derivations of the string – if adv<80 then if adv>50 then grade=3.5 else grade=3.0– S ==>if BOOL then S else S ==> if adv<80 then S else S ==> if adv<80

then if BOOL then S else S ==> if adv<80 then if adv>50 then S else S ==> if adv<80 then if adv>50 then grade=3.5 else S ==> if adv<80 then if adv>50 then grade=3.5 else grade=3.0

– S ==>if BOOL then S ==> if adv<80 then S ==> if adv<80 then if BOOL then S else S ==> if adv<80 then if adv>50 then S else S ==> if adv<80 then if adv>50 then grade=3.5 else S ==> if adv<80 then if adv>50 then grade=3.5 else grade=3.0

• Draw the corresponding parse trees

S --> if BOOL then S |grade=3.5 | grade=3.0 | if BOOL then S else S BOOL --> adv<80 | adv>50

Page 33: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

33

Corresponding Parse Trees• S ==>if BOOL then S else S ==> if adv<80 then

S else S ==> if adv<80 then if BOOL then S else S ==> if adv<80 then if adv>50 then S else S ==> if adv<80 then if adv>50 then grade=3.5 else S ==> if adv<80 then if adv>50 then grade=3.5 else grade=3.0

• S ==>if BOOL then S ==> if adv<80 then S ==> if adv<80 then if BOOL then S else S ==> if adv<80 then if adv>50 then S else S ==> if adv<80 then if adv>50 then grade=3.5 else S ==> if adv<80 then if adv>50 then grade=3.5 else grade=3.0

S S

if B then S else S

adv<80 if B then S grade=3.0

adv>50 grade=3.5

if B then S

adv<80 else Sif B then S

adv>50 grade=3.5 grade=3.0

Page 34: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

34

Parse Tree Meanings

S

if B then S else S

if B then Sadv<80

adv>50 grade=3.5

grade=3.0

S

if B then S

else Sif B then Sadv<80

adv>50 grade=3.5 grade=3.0

If you receive a 90 on advanced points, what is your grade?By parse tree 1

By parse tree 2

Page 35: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

35

Implications

• Two interpretations of string – if adv<80 then if adv>50 then grade=3.5 else grade=3.0

– Issue is which if-then does the last ELSE attach to?

– This phenomenon is known as the “dangling else”

– Answer: Typically, else binds to NEAREST if-then

• In this case, there is an unambiguous grammar for handling if-then’s as well as if-then-else’s

Page 36: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

36

Inherently ambiguous CFL’s

• A CFL L is inherently ambiguous iff for all CFG’s G such that L(G) = L, G is ambiguous

• Examples so far– None of the CFL’s we’ve seen so far are inherently ambiguous

– While the CFG’s we’ve seen ambiguous, there do exist unambiguous CFG’s for those CFL’s.

• Later result– There exist inherently ambiguous CFL’s

– Example: {aibjck | i=j or j=k or i=j=k}• Note i=j=k is unnecessary, but I added it here for clarity

Page 37: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

37

Summary

• Parse trees illustrate “semantic” information about strings

• Ambiguous grammars are undesirable– This means there are multiple parse trees for some string

– These strings can be interpreted in multiple ways

• There are some heuristics people use for taking an ambiguous grammar and making it unambiguous, but this is not the focus of this course

• There are some inherently ambiguous CFL’s– Thus, the above heuristics do not always work

Page 38: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

38

Module 30

• EQUAL language– Designing a CFG– Proving the CFG is correct

Page 39: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

39

EQUAL language

Designing a CFG

Page 40: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

40

EQUAL

• EQUAL is the set of strings over {a,b} with an equal number of a’s and b’s

• Strings in EQUAL include– aabbab– bbbaaa– abba

• Strings in {a,b}* not in EQUAL include– aaa– bbb– aab– ababa

Page 41: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

41

Designing a CFG for EQUAL

• Think recursively• Base Case

– What is the shortest possible string in EQUAL?

– Production Rule:

Page 42: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

42

Recursive Case

• Recursive Case– Now consider a longer string x in EQUAL

– Since x has length > 0, x must have a first character• This must be a or b

– Two possibilities for what x looks like• x = ay

– What must be true about relative number of a’s and b’s in y?

• x = bz – What must be true about relative number of a’s and b’s in z?

Page 43: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

43

Case 1: x=ay

• x = ay where y has one extra b– What must y look like?

• Some examples– b– babba– aabbbab– aaabbbb

• Is there a general pattern that applies to all of the above examples?

• More specifically, show how we can decompose all of the above strings y into 3 pieces, two of which belong to EQUAL.

– Some of these pieces might be the empty string

Page 44: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

44

Decomposing y

• y has one extra b– Possible examples

• b, babba, aabbbab, aaabbbb

– Decomposition• y = ubv where

– u and v both have an equal number of a’s and b’s

• Decompose the 4 strings above into u, b, v– b, aabbbab, babba, aaabbbb

Page 45: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

45

Implication

• Case 1: x=ay – y has one extra b

• Case 1 refined: x=aubv– u, v belong to EQUAL

• Production rule for this case?

Page 46: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

46

Case 2: x=bz

• Case 2: x=bz – z has one extra a

• Case 2 refined: x=buav– u, v belong to EQUAL

• Production rule for this case?

Page 47: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

47

Final Grammar

• EG = (V, , S, P)– V = {S}

– = {a,b}

– S = S

– P:

Page 48: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

48

EQUAL language

Proving CFG is correct

Page 49: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

49

Is our grammar correct?

• How do we prove our grammar is correct?– Informal

• Test some strings

• Review logic behind program (CFG) design

– Formal• First, show every string derived by EG belongs to EQUAL

– That is, show L(EG) is a subset of EQUAL

• Second, show every string in EQUAL can be derived by EG– That is, show EQUAL is a subset of L(EG)

• Both proofs will be inductive proofs– Inductive proofs and recursive algorithms go well together

Page 50: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

50

L(EG) subset of EQUAL

• Let x be an arbitrary string in L(EG)• What does this mean?

– S ==>*EG x

• Follows from definition of x in L(EG)

– We will prove the following• If S ==>1

EG x, then x is in EQUAL

• If S ==>2EG x, then x is in EQUAL

• If S ==>3EG x, then x is in EQUAL

• If S ==>4EG x, then x is in EQUAL

• ...

Page 51: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

51

Base Case

• Statement to be proven:– For all n >= 1, if S ==>n

EG x, then x is in EQUAL

– Prove this by induction on n

• Base Case:– n = 1

– What is the set of strings {x | S ==>1EG x}?

– What do we need to prove about this set of strings?

Page 52: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

52

Inductive Case

• Inductive Hypothesis:– For 1 <= j <= n, if S ==>j

EG x, then x is in EQUAL• Note, this is a “strong” induction hypothesis

• Traditional inductive hypothesis would take form:– For some n >= 1, if S ==>n

EG x, then x is in EQUAL

• The difference is we assume the basic hypothesis for all integers between 1 and n, not just n

• Statement to be Proven in Inductive Case:– If S ==>n+1

EG x, then x is in EQUAL

Page 53: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

53

• Infinite Set of Facts– Fact 1

– Fact 2

– Fact 3

– Fact 4

– Fact 5

– Fact 6

– …

• Base Case– Prove fact 1

• Regular inductive case– For n >= 1,

• Fact n --> Fact n+1

• Strong inductive case– For n >= 1,

• Fact 1 to Fact n --> Fact n+1

“Regular” induction vs Strong induction

Page 54: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

54

Visualization of InductionRegular Induction

Fact 1Fact 2Fact 3Fact 4Fact 5Fact 6Fact 7Fact 8Fact 9

Strong Induction

Fact 1Fact 2Fact 3Fact 4Fact 5Fact 6Fact 7Fact 8Fact 9

… …

Page 55: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

55

Proving Inductive Case

• If S ==>n+1EG x, then x is in EQUAL

– Let x be an arbitrary string such that S ==>n+1EG x

– Examining EG, what are the three possible first derivation steps

• Case 1: S ==> ==>nEG x

• Case 2: S ==> ==>nEG x

• Case 3: S ==> ==>nEG x

– One of the cases is impossible. Which one and why?

Page 56: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

56

Case 2: S ==> ==>nEG x

• This means x has the form aubv where– What can we conclude about u (don’t apply IH)?

– What can we conclude about v (don’t apply IH)?

• Apply the inductive hypothesis– u and v belong to EQUAL– Why do we need the strong inductive hypothesis?

• Conclude x belongs to EQUAL– x = aubv where u and v belong to EQUAL

• Clearly the number of a’s in x equals the number of b’s in x

Page 57: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

57

Case 3: S ==> ==>nEG x

• This means x has the form buav where– What can we conclude about u (no IH)?

– What can we conclude about v (no IH)

• Apply the inductive hypothesis– u and v belong to EQUAL– Why do we need the strong inductive hypothesis?

• Conclude x belongs to EQUAL– x = buav where u and v belong to EQUAL

• Clearly the number of a’s in x equals the number of b’s in x

Page 58: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

58

L(EG) subset of EQUAL

• Wrapping up inductive case– In all possible derivations of x, we have shown that x

belongs to EQUAL

– Thus, we have proven the inductive case

• Conclusion– By the principle of mathematical induction, we have

shown that L(EG) is a subset of EQUAL

Page 59: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

59

EQUAL subset of L(EG)

• Let x be an arbitrary string in EQUAL• What does this mean?

• We will prove the following• If |x| = 0 and x is in EQUAL, then x is in L(G)• If |x| = 1 and x is in EQUAL, then x is in L(G)• If |x| = 2 and x is in EQUAL, then x is in L(G)• If |x| = 3 and x is in EQUAL, then x is in L(G)• ...

Page 60: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

60

• Statement to be proven:– For all n >= 0, if |x| = n and x is in EQUAL, then x is in

L(EG)– Prove this by induction on n

• Base Case:– n = 0– What is the only string x such that |x|=0 and x is in

EQUAL?

– Prove this string belongs to L(EG)

EQUAL subset of L(EG)

Page 61: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

61

Inductive Case

• Inductive Hypothesis:– For 0 <= j <= n, if |x| =j and x is in EQUAL, then x is

in L(EG)• Again, this is a “strong” induction hypothesis

• Statement to be Proven in Inductive Case:– For n >= 0,

– if |x| = n+1 and x is in EQUAL, then x is in L(EG)

Page 62: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

62

Proving Inductive Case

• If |x|=n+1 and x is in EQUAL, then x is in L(EG)– Let x be an arbitrary string such that |x|=n+1 and x is in

L(EG)– Examining , what are the two possibilities for the first

character in x?• Case 1: first character in x is • Case 2: first character in x is

– In each case, what can we say about the remainder of x?• Case 1: the remainder of x• Case 2: the remainder of x

Page 63: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

63

Case 1: x = ay• What can we say about y in this case?

• This means x has the form aubv where– u is in EQUAL and has length <= n– v is in EQUAL and has length <= n– Proving this statement true

• Consider all the prefixes of string y– length 0: – length 1: y1

– length 2: y1y2

– …– length n: y1y2 … yn = y

Page 64: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

64

Case 1: x = ay• Consider all the prefixes of string y

– length 0: – length 1: y1

– length 2: y1y2

– …

– length n: y1y2 … yn = y

• The first prefix has the same number of a’s as b’s

• The last prefix y has one extra b

• The relative number of a’s and b’s changes in the length i prefix differs by only one from the length i-1 prefix

• Thus, there must be a first prefix t of y where t has one extra b

• Furthermore, the last character of t must be b– Otherwise, t would not be the FIRST prefix of y with one extra b

• Break t into u and b and let the remainder of y be v

• The statement follows

Page 65: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

65

Case 1: x = aubv *• x = aubv

– u is in EQUAL and has length <= n

– v is in EQUAL and has length <= n

• Apply the induction hypothesis– What can we conclude from applying the IH?

– Why did we need a strong inductive hypothesis?

• Conclude x is in L(EG) by constructing a derivation– S ==> aSbS ==>*

EG aubS ==>*EG aubv

Page 66: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

66

Case 2: x = buav• x = buav

– u is in EQUAL and has length <= n

– v is in EQUAL and has length <= n

• Apply the induction hypothesis– What can we conclude about u and v?

• Conclude x is in L(EG) by constructing a derivation– S ==> bSaS ==>*

EG buaS ==>*EG buav

• Justify each of the steps in this derivation

Page 67: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

67

EQUAL subset of L(EG)

• Wrapping up inductive case– For all possible first characters of x, we have shown

that x belongs to L(EG)

– Thus, we have proven the inductive case

• Conclusion– By the principle of mathematical induction, we have

shown that EQUAL is a subset of L(EG)

Page 68: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

68

Module 31

• Closure Properties for CFL’s– Kleene Closure

• construction• examples• proof of correctness

– Others covered less thoroughly in lecture• union, concatenation

• CFL’s versus regular languages– regular languages subset of CFL

Page 69: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

69

Closure Properties for CFL’s

Kleene Closure

Page 70: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

70

CFL closed under Kleene Closure

• Let L be an arbitrary CFL

• Let G1 be a CFG s.t. L(G1) = L

– G1 exists by definition of L1 in CFL

• Construct CFG G2 from CFG G1

• Argue L(G2) = L*

• There exists CFG G2 s.t. L(G2) = L*

• L* is a CFL

Page 71: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

71

Visualization

L*L

CFL

CFG’s

• Let L be an arbitrary CFL

• Let G1 be a CFG s.t. L(G1) = L

– G1 exists by definition of L1 in CFL

• Construct CFG G2 from CFG G1

• Argue L(G2) = L*

• There exists CFG G2 s.t. L(G2) = L*

• L* is a CFLG1 G2

Page 72: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

72

Algorithm Specification

• Input– CFG G1

• Output– CFG G2 such that L(G2) = (L(G1))*

CFG G1 CFG G2A

Page 73: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

73

Construction

• Input– CFG G1 = (V1, , S1, P1)

• Output– CFG G2 = (V2, , S2, P2)

• V2 = V1 union {T}

– T is a new symbol not in V1 or

• S2 = T

• P2 = P1 union ??

Page 74: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

74

Closure Properties for CFL’s

Kleene Closure Examples

Page 75: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

75

• Input grammar:– V = {S}

– = {a,b}

– S = S

– P: S --> aa | ab | ba | bb

• Output grammar– V = {S, T}

– = {a,b}

– Start symbol is

– P:

Example 1V2 = V1 union {T} T is a new symbol not in V1 or S2 = TP2 = P1 union {T --> ST | }

Page 76: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

76

• Input grammar:– V = {S, T}

– = {a,b}

– Start symbol is T

– P:T --> ST | S --> aa | ab | ba | bb

• Output grammar– V = {S, T, U}

– = {a,b}

– Start symbol is

– P:

Example 2V2 = V1 union {T} T is a new symbol not in V1 or S2 = TP2 = P1 union {T --> ST | }

Page 77: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

77

Closure Properties for CFL’s

Kleene Closure Proof of Correctness

Page 78: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

78

Is our construction correct?

• How do we prove our construction is correct?– Informal

• Test some strings

• Review logic behind construction

– Formal• First, show every string derived by G2 belongs to (L(G1))*

– That is, show L(G2) is a subset of (L(G1))*

• Second, show every string in (L(G1))* can be derived by G2

– That is, show (L(G1))* is a subset of L(G2)

• Both proofs will be inductive proofs– Inductive proofs and recursive algorithms go well together

Page 79: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

79

L(G2) is a subset of (L(G1))*

• We want to prove the following– If x in L(G2), then x is in (L(G1))*

• This is equivalent to the following– If T ==>*

G2 x, then x is in (L(G1))*

– The two statements are equivalent because

• x in L(G2) means that T ==>*G2

x

• We break the second statement down as follows:– If T ==>1

G2 x, then x is in (L(G1))*

– If T ==>2G2

x, then x is in (L(G1))*

– If T ==>3G2

x, then x is in (L(G1))*

– ...

Page 80: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

80

L(G2) is a subset of (L(G1))*

• Statement to be proven:– For all n >= 1, if T ==>n

G2 x, then x is in (L(G1))*

– Prove this by induction on n

• Base Case:– n = 1

– Examining G2, what is the only string x such that T ==>1

G2 x ?

– Prove this string is in (L(G1))*

Page 81: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

81

Inductive Case

• Inductive Hypothesis:– For 1 <= j <= n, if T ==>j

G2 x, then x is in (L(G1))*

• Note, this is a “strong” induction hypothesis

• Statement to be Proven in Inductive Case:– For n above, if T ==>n+1

G2 x, then x is in (L(G1))*

• Proving this statement– Let x be an arbitrary string such that T ==>n+1

G2 x

– Examining G2, what are the two possible first derivation steps?

• Case 1: T ==>G2 ==>n

G2 x

• Case 2: T ==>G2 ==>n

G2 x

Page 82: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

82

Case Analysis

• Case 1: T ==>G2 n x is not possible

– Why not?

• Case 2: T ==>G2 ==>n

G2 x

– This means x has the form uv where• What can we say about u (no IH)?

• What can we say about v (no IH)?

– Applying the inductive hypothesis, what can we conclude?

Page 83: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

83

Concluding Case 2: T ==>G2

==>nG2

x

– Concluding string u belongs to L(G1)• Follows from S ==>*

G2 u and

• Our construction insures that all strings derived from S in L(G2) are also in L(G1)

– How do we conclude that x belongs to (L(G1))*

• Wrapping up inductive case– In all possible derivations of x, we have shown that x belongs to (L(G1))* – Thus, we have proven the inductive case

• Conclusion– By the principle of mathematical induction, we have shown that L(G2) is

a subset of (L(G1))*

Page 84: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

84

(L(G1))* is a subset of L(G2)

• We want to prove the following– If x is in (L(G1))*, then x is in L(G2)

• This is equivalent to the following– If x is in (L(G1))*, then T ==>*

G2 x

– The two statements are equivalent because

• x in L(G2) means that T ==>*G2

x

• We break the second statement down as follows:– If x is in (L(G1))0, then T ==>*

G2 x

– If x is in (L(G1))1, then T ==>*G2

x

– If x is in (L(G1))2, then T ==>*G2

x

– ...

Page 85: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

85

(L(G1))* is a subset of L(G2)

• Statement to be proven:– For all n >= 0, if x is in (L(G1))n, then x is in L(G2)

– Prove this by induction on n

• Base Case:– n = 0

– What is the only string x in (L(G1))0?

– Show this string belongs to L(G2)

Page 86: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

86

Inductive Case

• Inductive Hypothesis:– For n>=0, if x is in (L(G1))j, then T ==>*

G2 x

• Note, this is a “normal” induction hypothesis

• Statement to be Proven in Inductive Case:– For n>=0, if x is in (L(G1))n+1, then T ==>*

G2 x

• Proving this statement– Let x be an arbitrary string in (L(G1))n+1

– This means x = uv where• u in L(G1)• What can we say about v?

Page 87: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

87

Deriving x– x = uv where

• u is a string in L(G1)• v is a string in

– Justify all the steps in the following derivation

– T ==> G2 ST ==>*

G2 Sv ==>*

G2 uv = x

• First step:• Second step:• Third step:

– Thus T ==>* G2

x

• The inductive case follows• The result is proven by the principle of mathematical

induction

Page 88: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

88

Construction for Set Union

• Input– CFG G1 = (V1, , S1, P1)

– CFG G2 = (V2, , S2, P2)

• Output– CFG G3 = (V3, , S3, P3)

• V3 = V1 union V2 union {T}

– Variable renaming to insure no names shared between V1 and V2

– T is a new symbol not in V1 or V2 or

• S3 = T

• P3 = P1 union P2 union {T --> }

Page 89: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

89

Construction for Set Concatenation

• Input– CFG G1 = (V1, , S1, P1)

– CFG G2 = (V2, , S2, P2)

• Output– CFG G3 = (V3, , S3, P3)

• V3 = V1 union V2 union {T}

– Variable renaming to insure no names shared between V1 and V2

– T is a new symbol not in V1 or V2 or

• S3 = T

• P3 = P1 union P2 union {T --> }

Page 90: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

90

CFL’s and regular languages

Page 91: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

91

CFL Closure Properties

• CFL’s are closed under Kleene closure– Just proven, proof also in book

• CFL’s are closed under set union– Proof in book

• CFL’s are closed under set concatenation– Proof in book

• What can we conclude from these 3 results?– It follows that regular languages are a subset of CFL’s

Page 92: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

92

Regular languages subset of CFL

• Recursive definition of regular languages– Base Case:

• {}, {}, {a}, {b} are regular languages over {a,b}

• P={}, P={S --> }, P={S --> a}, P={S --> b}

– Inductive Case:• If L1 and L2 are are regular languages, then L1*,

L1L2, L1 union L2 are regular languages

• Use previous constructions to see that these resulting languages are also context-free

Page 93: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

93

Other CFL Closure Properties

• We will show that CFL’s are NOT closed under many other set operations

• Examples include– set complement

– set intersection

– set difference

Page 94: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

94

Language class hierarchy

All languages over alphabet

RE

REG

HH

EqualCFLREC

?

Page 95: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

95

Module 32

• Pushdown Automata (PDA’s)– definition– Example

• We define configurations and computations of PDA’s

• We define L(M) for PDA’s

Page 96: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

96

Pushdown Automata

Definition and Motivating Example

Page 97: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

97

Pushdown Automata (PDA)

• In this presentation we introduce the PDA model of computation (programming language).– The key addition to a PDA (from an NFA-/\) is

the addition of external memory in the form of an infinite capacity stack

• The word “pushdown” comes from the stacks of trays in cafeterias where you have to pushdown on the stack to add a tray to it.

Page 98: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

98

NFA for {ambn | m,n >= 0}

• What strings end up in each state of the above NFA?– I:

– B:

– C:

• Consider the language {anbn | n >= 0}.

• This NFA can recognize strings which have the correct form, – a’s followed by b’s.

• However, the NFA cannot remember the relative number of a’s and b’s seen at any point in time.

/\ /\

a b

I B C

Page 99: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

99

PDA for {anbn | n >=0 } *

/\ /\

a b

I B C

NFA

Imagine we now have memory in the form of a stack which we can use to help remember how many a’s we have seen by pushing onto and popping from the stack

When we see an a in state I, we do the following two actions:1) We push an a on the stack.2) We stay in state I.

When we see a b in state B, we do the following two actions:1) We pop an a from the stack.2) We stay in state B.

From state B, we allow a /\-transition to state C only if1) The stack is empty.

Finally, when we begin, the stack should be empty.

I B C

b;popa;push a

/\;only if stack is empty

PDA

/\

Initialize stack to empty

Page 100: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

100

Formal PDA definition

• PDA M = (Q, , , q0, Z, A, )

• Modified elements– is the stack alphabet

• Z is a special character that is initially on the stack

• Often used to represent an empty stack

– is modified as follows• Pop to read the top character on the stack

• Stack update action– What to push back on the stack

– If we push /\, then the net result of the action is a pop

Page 101: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

101

Example PDAQ = {I, B, C} = {a,b} = {Z, a}q0 = IZ is the initial stack characterA = {C}:

S a TopSt NS stack update

I a a I push aaI a Z I push aZI /\ a B push aI /\ Z B push ZB b a B push /\B /\ Z C push Z

I B C

b;a;/\a;a; aaa;Z; aZ

/\;Z;Z/\;a;a /\;Z;Z

Example PDA

Initialize stack toonly contain Z

Page 102: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

102

Computing with PDA’s *

• Configurations change compared with NFA-/\’s– Configuration components:

• current state• remaining input to be processed• stack contents

• Computations are essentially the same as with NFA-/\’s given the modified configurations– Determining which transitions of a PDA can be

applied to a given configuration is more complicated though

Page 103: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

103

Computation Graph of PDA

Computation graph for this PDA on the input string aabb

(I,aabb,Z)

(C,aabb,Z)

(B,bb,aaZ)

(B,abb,aZ)(I,bb,aaZ)

(I,abb,aZ) (B,aabb,Z)

(B,b,aZ)

(B,/\,Z)

(C,/\,Z)

Q = {I, B, C} = {a,b} = {Z, a}q0 = IZ is the initial stack characterA = {C}:

S a TopSt NS stack updateI a a I push aaI a Z I push aZI /\ a B push aI /\ Z B push ZB b a B push /\B /\ Z C push Z

Page 104: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

104

Definition of |- Input string aabb

(I, aabb, Z) |- (I,abb,aZ)

(I, aabb, Z) |- (B, aabb, Z)

(I, aabb, Z) |-2 (C, aabb, Z)

(I, aabb, Z) |-3 (B, bb, aaZ)

(I, aabb, Z) |-* (B, abb, aZ)

(I, aabb, Z) |-* (B, /\, Z)

(I, aabb, Z) |-* (C, /\, Z)

I B C

b;a;/\a;a; aaa;Z; aZ

/\;Z;Z/\;a;a /\;Z;Z

(I,aabb,Z)

(C,aabb,Z)

(B,bb,aaZ)

(B,abb,aZ)(I,bb,aaZ)

(I,abb,aZ) (B,aabb,Z)

(B,b,aZ)

(B,/\,Z)

(C,/\,Z)

Page 105: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

105

Acceptance and Rejection

M accepts string x if one of the configurations reached is an accepting configuration

(q0, x, Z) |-* (f, /\),f in A, in *

Stack contents can be anything

M rejects string x if all configurations reached are either not halting configurations or are rejecting configurations

I B C

b;a;/\a;a; aaa;Z; aZ

/\;Z;Z/\;a;a /\;Z;Z

Input string aabb

(I,aabb,Z)

(C,aabb,Z)

(B,bb,aaZ)

(B,abb,aZ)(I,bb,aaZ)

(I,abb,aZ) (B,aabb,Z)

(B,b,aZ)

(B,/\,Z)

(C,/\,Z)

Not an accepting configuration since input not completely processed

Not an accepting configuration since state is not accepting

An accepting configuration

Page 106: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

106

Defining L(M) and LPDA

• L(M) (or Y(M)) – The set of strings ?

• N(M)– The set of strings ?

• LPDA– Language L is in language

class LPDA iff ?

M accepts string x if one of the configurations reached is an accepting configuration

(q0, x, Z) |-* (f, /\),f in A, in *

Stack contents can be anything

M rejects string x if all configurations reached are either not halting configurations or are rejecting configurations

Page 107: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

107

Deterministic PDA’s

• A PDA is deterministic if its transition function satisfies both of the following properties– For all q in Q, a in union {/\}, and X in ,

• the set (q,a,X) has at most one element

– For all q in Q and X in G,• if (q, /\, X) < > { }, then (q,a,X) = { } for all a in

• A computation graph is now just a path again• Our default assumption is that PDA’s are

nondeterministic

Page 108: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

108

Two forms of nondeterminism

Trans Current Input Top of Next Stack# State Char. Stack State Update-------------------------------------------------------1 q0 a Z q0 aZ2 q0 a Z q0 aa

3 q0 /\ Z q0 aZ4 q0 a Z q0 aa

Page 109: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

109

LPDA and DCFL

• A language L is in language class LPDA if and only if there exists a PDA M such that L(M) = L

• A language L is in language class DCFL (Deterministic Context-Free Languages) if and only if there exists a deterministic PDA M such that L(M) = L

• To be proven– LPDA = CFL – CFL is a proper superset of DCFL

Page 110: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

110

PDA Comments

• Note, we can use the stack for much more than just a counter

• See examples in chapter 7 for some details

Page 111: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

111

Module 33

• Pushdown Automata (PDA’s)– Another example

Page 112: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

112

Palindromes

• Let PAL be the set of palindromes over {a,b}– Let PAL1 be the following related language:

• {wcwr | w consists only of a’s and b’s}– we add c to the input alphabet as a special “marker” character

– Strings in PAL1

» aca, bcb, abcba, aabcbaa, c

– strings not in PAL1

» aaca, aaccaa, abccba, abcb, abba

– Let PAL2 be the set of even length palindromes• {wwr | w consists only of a’s and b’s}

Page 113: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

113

PAL1

• Lets first construct a PDA for PAL1

• Basic ideas– Have one state remember first “half” of string– Have one state “match” second half of string to

first half– Transition between these two states when the

first c is encountered

Page 114: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

114

PDA for PAL1

• M = (Q, , , q0, Z, A, )

– Q = {q0, qm, qf}

– = {a, b, c}– = {Z, a, b}

– q0 = q0

– Z = Z

– A = {qf}

Page 115: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

115

Transition FunctionTrans Current Input Top of Next Stack# State Char. Stack State Update-------------------------------------------------------1 q0 a Z q0 aZ2 q0 a a q0 aa3 q0 a b q0 ab

4 q0 b Z q0 bZ5 q0 b a q0 ba6 q0 b b q0 bb

7 q0 c Z qm Z8 q0 c a qm a9 q0 c b qm b

10 qm a a qm 11 qm b b qm 12 qm Z qf Z

First three transitions push a on top of the stack

Second three transitions push b on the stack

Third three transitions switch state q0 to qm

No change to stack

Transitions 10 and 11 “match” characters from first and last half of input string

Page 116: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

116

Notation comment

We might represent transition 1 in two other ways

(q0,a,Z) = (q0, aZ)

(q0, a, Z, q0, aZ)

•Question

•Is this PDA deterministic?

Trans Current Input Top of Next Stack# State Char. Stack State Update-------------------------------------------------------1 q0 a Z q0 aZ2 q0 a a q0 aa3 q0 a b q0 ab

4 q0 b Z q0 bZ5 q0 b a q0 ba6 q0 b b q0 bb

7 q0 c Z qm Z8 q0 c a qm a9 q0 c b qm b

10 qm a a qm 11 qm b b qm 12 qm Z qf Z

Page 117: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

117

Computation Graph 1

(q0, abcba, Z)

(q0, bcba, aZ)

(q0, cba, baZ)

(qm, ba, baZ)

(qm, a, aZ)

(qm, , Z)

(qf, , Z)

Trans Current Input Top of Next Stack# State Char. Stack State Update-------------------------------------------------------1 q0 a Z q0 aZ2 q0 a a q0 aa3 q0 a b q0 ab

4 q0 b Z q0 bZ5 q0 b a q0 ba6 q0 b b q0 bb

7 q0 c Z qm Z8 q0 c a qm a9 q0 c b qm b

10 qm a a qm 11 qm b b qm 12 qm Z qf Z

Page 118: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

118

Computation Graph 2

(q0, abcab, Z)

(q0, bcab, aZ)

(q0, cab, baZ)

(qm, ab, baZ)

Trans Current Input Top of Next Stack# State Char. Stack State Update-------------------------------------------------------1 q0 a Z q0 aZ2 q0 a a q0 aa3 q0 a b q0 ab

4 q0 b Z q0 bZ5 q0 b a q0 ba6 q0 b b q0 bb

7 q0 c Z qm Z8 q0 c a qm a9 q0 c b qm b

10 qm a a qm 11 qm b b qm 12 qm Z qf Z

Page 119: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

119

Computation Graph 3

(q0, acab, Z)

(q0, cab, aZ)

(qm, ab, aZ)

(qm, b, Z)

(qf, b, Z)

Trans Current Input Top of Next Stack# State Char. Stack State Update-------------------------------------------------------1 q0 a Z q0 aZ2 q0 a a q0 aa3 q0 a b q0 ab

4 q0 b Z q0 bZ5 q0 b a q0 ba6 q0 b b q0 bb

7 q0 c Z qm Z8 q0 c a qm a9 q0 c b qm b

10 qm a a qm 11 qm b b qm 12 qm Z qf Z

Page 120: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

120

PAL2

• Lets now construct a PDA for PAL• What is harder this time?

– When do we switch from putting strings on the stack to matching?

– Example• After seeing aab, should we switch to match mode or

stay in stack mode?

– Solution• Do both using nondeterminism

Page 121: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

121

PDA for PAL2

• M = (Q, , , q0, Z, A, )

– Q = {q0, qm, qf}

– = {a, b}– = {Z, a, b}

– q0 = q0

– Z = Z

– A = {qf}

Page 122: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

122

Transition RelationTrans Current Input Top of Next Stack# State Char. Stack State Update-------------------------------------------------------1 q0 a Z q0 aZ2 q0 a a q0 aa3 q0 a b q0 ab

4 q0 b Z q0 bZ5 q0 b a q0 ba6 q0 b b q0 bb

7 q0 Z qm Z8 q0 a qm a9 q0 b qm b

10 qm a a qm 11 qm b b qm 12 qm Z qf Z

First three transitions push a on top of the stack

Second three transitions push b on the stack

Third three transitions switch state q0 to qm

Is the PDA deterministic or nondeterministic?

Page 123: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

123

Computation Graph 1

(q0, abba, Z)

(q0, bba, aZ) (qm, abba, Z)

(q0, ba, baZ) (qm, bba, aZ) (qf, abba, Z)

(q0, a, bbaZ) (qm, ba, baZ)

(q0, , abbaZ)(qm, a, bbaZ)

(qm, a, aZ)

(qm, , abbaZ) (qm, , Z)

(qf, , Z)

Trans Current Input Top of Next Stack# State Char. Stack State Update-------------------------------------------------------1 q0 a Z q0 aZ2 q0 a a q0 aa3 q0 a b q0 ab

4 q0 b Z q0 bZ5 q0 b a q0 ba6 q0 b b q0 bb

7 q0 Z qm Z8 q0 a qm a9 q0 b qm b

10 qm a a qm 11 qm b b qm 12 qm Z qf Z

Page 124: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

124

Computation Graph 2

(q0, aba, Z)

(q0, ba, aZ) (qm, aba, Z)

(q0, a, baZ) (qm, ba, aZ) (qf, aba, Z)

(q0, , abaZ) (qm, a, baZ)

(qm, , abaZ)

Trans Current Input Top of Next Stack# State Char. Stack State Update-------------------------------------------------------1 q0 a Z q0 aZ2 q0 a a q0 aa3 q0 a b q0 ab

4 q0 b Z q0 bZ5 q0 b a q0 ba6 q0 b b q0 bb

7 q0 Z qm Z8 q0 a qm a9 q0 b qm b

10 qm a a qm 11 qm b b qm 12 qm Z qf Z

Page 125: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

125

PAL

• Challenge– Construct a PDA for PAL– First step

• Construct a PDA for odd length palindromes

– Then • Combine PDA’s for odd length and even length

palindromes

Page 126: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

126

Module 34

• CFG --> PDA construction– Shows that for any CFL L, there exists a PDA

M such that L(M) = L– The reverse is true as well, but we do not prove

that here

Page 127: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

127

CFL subset LPDA

• Let L be an arbitrary CFL

• Let G be the CFG such that L(G) = L– G exists by definition of L is CF

• Construct a PDA M such that L(M) = L– M is constructed from CFG G

• Argue L(M) = L

• There exists a PDA M such that L(M) = L

• L is in LPDA– By definition of L in LPDA

Page 128: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

128

Visualization

CFLLPDA

CFG’s PDA’s

L L

G M

•Let L be an arbitrary CFL•Let G be the CFG such that L(G) = L

•G exists by definition of L is CF•Construct a PDA M such that L(M) = L

•M is constructed from CFG G•Argue L(M) = L•There exists a PDA M such that L(M) = L•L is in LPDA

•By definition of L in LPDA

Page 129: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

129

Algorithm Specification

• Input– CFG G

• Output– PDA M such that L(M) = L(G)

CFG G PDA MA

Page 130: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

130

Construction Idea

• The basic idea is to have a 2-phase PDA– Phase 1:

• Derive all strings in L(G) on the stack nondeterministically

• Do not process any input while we are deriving the string on the stack

– Phase 2: • Match the input string against the derived string on the stack

– This is a deterministic process

• Move to an accepting state only when the stack is empty

Page 131: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

131

Illustration

• Input Grammar G– V = {S}– = {a,b}– S = S– P:

S --> aSb |

• What is L(G)?

1. Derive all strings in L(G) on the stack2. Match the derived string against input

(q0, aabb, Z)/* put S on stack */

(q1, aabb, SZ)/* derive aabb on stack */

(q1, aabb, aSbZ)(q1, aabb, aaSbbZ)(q1, aabb, aabbZ)

/* match stack vs input */(q2, aabb, aabbZ)(q2, abb, abbZ)(q2, bb, bbZ)(q2, b, bZ)(q2,, Z)

(q3, , Z)

Illustration of how the PDA might work,though not completely accurate.

Page 132: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

132

Difficulty 1. Derive all strings in L(G) on the stack2. Match the derived string against input

(q0, aabb, Z)/* put S on stack */

(q1, aabb, SZ)/* derive aabb on stack */

(q1, aabb, aSbZ)(q1, aabb, aaSbbZ)(q1, aabb, aabbZ)

/* match stack vs input */(q2, aabb, aabbZ)(q2, abb, abbZ)(q2, bb, bbZ)(q2, b, bZ)(q2,, Z)

(q3, , Z)

What is illegal with the computation graph on the left?

Page 133: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

133

Construction• Input Grammar

– G=(V,, S, P)

• Output PDA – M=(Q, , , q0, Z, F, )

– Q = {q0, q1, q2}

– = – = V union union {Z}

– Z = Z

– q0 = q0

– F = {q2}

• :– (q0, , Z) = (q1, SZ)

– (q1, , Z) = (q2, Z)

– For all productions A --> • q1, , A) = (q1, )

– For all a in • q1, a, a) = (q1, )

Page 134: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

134

Examples

Page 135: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

135

Palindromes• PALG:

– V = {S}

– = {a,b}

– S = S

– P:• S --> aSa | bSb | a | b |

• Output PDA M=(Q,,q0,Z,F,)

– Q = {q0, q1, q2}

– = {a,b,S,Z}

– q0 = q0

– Z = Z

– F = {q2}

• :

– (q0, , Z) = (q1, SZ)

– (q1, , Z) = (q2, Z)

– Production Transitions• q1, , S) = (q1, aSa)

• q1, , S) = (q1, bSb)

• q1, , S) = (q1, a)

• q1, , S) = (q1, b)

• q1, , S) = (q1, )

– Matching transitions• q1, a, a) = (q1, )

• q1, b, b) = (q1, )

Page 136: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

136

Palindrome Transition Table

Transition Current Input Top of Next StackNumber State Symbol Stack State Update--------------------------------------------------------------------------------- 1 q0 Z q1 SZ 2 q1 Z q2 Z 3 q1 S q1 aSa 4 q1 S q1 bSb 5 q1 S q1 a 6 q1 S q1 b 7 q1 S q1 8 q1 a a q1 9 q1 b b q1

Page 137: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

137

Partial Computation Graph(q0, aba, Z) (q1, aba, SZ) (q1, aba, aSaZ) (other branches not shown)(q1, ba, SaZ)(q1, ba, baZ) (other branches not shown)(q1, a, aZ)(q1, , Z)(q2, , Z)

On your own, draw computation trees for other stringsnot in the language and see that they are not accepted.

Page 138: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

138

{anbn | n >= 0}• Grammar G:

– V = {S}

– = {a,b}

– S = S

– P:• S --> aSb |

• Output PDA M=(Q,,q0,Z,F,)

– Q = {q0, q1, q2}

– = {a,b,S,Z}

– q0 = q0

– Z = Z

– F = {q2}

• :

– (q0, , Z) = (q1, SZ)

– (q1, , Z) = (q2, Z)

– Production Transitions•

– Matching transitions

Page 139: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

139

{anbn | n >= 0} Transition Table

Transition Current Input Top of Next StackNumber State Symbol Stack State Update--------------------------------------------------------------------------------- 1 q0 Z 2 q1 Z 3 q1 S 4 q1 S 5 q1 a a 6 q1 b b

Page 140: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

140

Partial Computation Graph

(q0, aabb, Z) (q1, aabb, SZ) (q1, aabb, aSbZ) (other branch not shown)(q1, abb, SbZ)(q1, abb, aSbbZ) (other branch not shown)(q1, bb, SbbZ)(q1, bb, bbZ) (other branch not shown)(q1, b, bZ)(q1, , Z) (q2, , Z)

Page 141: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

141

{aibj | i = j or i = 2j}• Grammar G:

– V = {S,T,U}

– = {a,b}

– S = S

– P:• S --> T | U

• T --> aTb | • U --> aaUb |

• Output PDA M=(Q,,q0,Z,F,)

– Q = {q0, q1, q2}

– = {a,b,S,T,U,Z}

– q0 = q0

– Z = Z

– F = {q2}

• – (q0, , Z) = (q1, SZ)

– (q1, , Z) = (q2, Z)

– Production Transitions• q1, , S) = (q1, T)

• q1, , S) = (q1, U)

• q1, , T) = (q1, aTb)

• q1, , T) = (q1, )

• q1, , U) = (q1, aaUb)

• q1, , U) = (q1, )

– Matching transitions• q1, a, a) = (q1, )

• q1, b, b) = (q1, )

Page 142: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

142

{aibj | i = j or i = 2j} Transition Table

Transition Current Input Top of Next StackNumber State Symbol Stack State Update--------------------------------------------------------------------------------- 1 q0 Z q1 SZ 2 q1 Z q2 Z 3 q1 S q1 T 4 q1 S q1 U 5 q1 T q1 aTb 6 q1 T q1 7 q1 U q1 aaUb 8 q1 U q1 9 q1 a a q1 10 q1 b b q1

Page 143: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

143

Partial Computation Graph

(q0, aab, Z) (q1, aab, SZ) (q1, aab, UZ) (other branch not shown)(q1, aab, aaUbZ) (other branch not shown)(q1, ab, aUbZ)(q1, b, UbZ)(q1, b, bZ) (other branch not shown)(q1, , Z) (q2, , Z)

Page 144: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

144

Things you should be able to do

• You should be able to execute this algorithm– Given any CFG, construct an equivalent PDA

• You should understand the idea behind this algorithm– Derive string on stack and then match it against input

• You should understand how this construction can help you design PDA’s

• You should understand that it can be used in answer-preserving input transformations between decision problems about CFL’s.

Page 145: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

145

Module 35

• Attempt to prove that CFL’s are closed under intersection– Review previous constructions– Translate previous constructions to current

setting– Prove modified result

Page 146: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

146

High Level Overview

Page 147: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

147

CFL closed under set intersection

• Let L1 and L2 be arbitrary CFL’s

• Let M1 and M2 be PDA’s s.t. L(M1) = L1, L(M2) = L2

– M1 and M2 exist by definition of L1 and L2 are CFL’s and the fact that for every CFG, there is an equivalent PDA

• Construct PDA M3 from PDA’s M1 and M2

• Argue L(M3) = L1 intersect L2

• There exists a PDA M3 s.t. L(M3) = L1 intersect L2

• L1 intersect L2 is a CFL

Page 148: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

148

Visualization

•Let L1 and L2 be arbitrary CFL’s•Let M1 and M2 be PDA’s s.t. L(M1) = L1, L(M2) = L2

•M1 and M2 exist by definition of L1 and L2 are CFL’s and the fact that for every CFG, there is an equivalent PDA

•Construct PDA M3 from PDA’s M1 and M2

•Argue L(M3) = L1 intersect L2

•There exists a PDA M3 s.t. L(M3) = L1 intersect L2

•L1 intersect L2 is a CFL

L1 intersect L2

L1

L2

CFL

M3

M1

M2

PDA’s

Page 149: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

149

Algorithm Specification

• Input– Two PDA’s M1 and M2

• Output– PDA M3 such that L(M3) = L(M1) intersection L(M2)

PDA M1

PDA M2

PDA M3A

Page 150: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

150

Review Previous Results

Page 151: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

151

Underlying Idea

• Previous Results– recursive languages are closed under set intersection

– r.e. languages are closed under set intersection

– regular languages are closed under set intersection

• What is the idea underlying the constructions used to prove these previous results?

Page 152: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

152

Implementation with FSA’s *

• Given the basic idea underlying these constructions, how was this idea implemented in when dealing with FSA’s?

• That is, restate the construction used to prove that the regular languages are closed under set intersection.– Specify the output FSA in terms of the input FSA’s

Page 153: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

153

Applying previous approach to PDA’s

Page 154: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

154

Applying approach to PDA’s *

• Given the basic idea underlying these constructions, try and implement this idea in a construction working with PDA’s rather than FSA’s.

• That is, give a construction specifying how the output PDA is built out of the input PDA’s

Page 155: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

155

Problem

• Describe what goes wrong when applying this idea to PDA’s instead of FSA’s.

• Does this prove that CFL’s are NOT closed under set intersection?

Page 156: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

156

Modified Result *

• What happens if the inputs are 1 FSA and 1 PDA?

• What modified result does the resulting construction prove?

Page 157: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

157

Module 36

• Non context-free languages– Examples and Intuition

• Pumping lemma for CFL’s– Pumping condition– No proof of pumping lemma– Applying pumping lemma to prove that some

languages are not CFL’s

Page 158: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

158

Examples and Intuition

Page 159: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

159

Examples

• What are some examples of nonregular languages?

• Can we build on any of these languages to create a non context-free language?

Page 160: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

160

Intuition

• Try and prove that these languages are CFL’s and identify the stumbling blocks– Why can’t we construct a CFG to generate this language?

– Why can’t we construct a PDA to accept this language?

– Compare to similar CFL languages to try and identify differences.

Page 161: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

161

Pumping Lemma for CFL’s

Page 162: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

162

Comparison to regular language pumping lemma/condition

Page 163: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

163

What’s different about CFL’s than regular languages? *

• In regular languages, a single substring “pumps”– Consider the language of even length strings over {a,b}– We can identify a single substring which can be pumped

• In CFL’s, multiple substrings can “pump”– Consider the language {anbn | n > 0}– No single substring can be pumped and allow us to stay in the

language– However, there do exist pairs of substrings which can be

pumped resulting in strings which stay in the language

• This results in a modified pumping condition

Page 164: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

164

Modified Pumping Condition

• A language L satisfies the regular language pumping

condition if: – there exists an integer n > 0

such that

– for all strings x in L of length at least n

– there exist strings u, v, w such that

• x = uvw and

• |uv| <= n and

• |v| >= 1 and

• For all k >= 0, uvkw is in L

• A language L satisfies the CFL

pumping condition if: – there exists an integer n > 0

such that

– for all strings x in L of length at least n

– there exist strings u, v, w, y, z such that

• x = uvwyz and

• |vwy| <= n and

• |vy| >= 1 and

• For all k >= 0, uvkwykz is in L

Page 165: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

165

Pumping Lemma

• All CFL’s satisfy the CFL pumping condition

All languages over {a,b}

CFL’s

“Pumping Languages”

Page 166: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

166

Implications

• We can use the pumping lemma to prove a language L is not a CFL– Show L does not satisfy the CFL pumping

condition

• We cannot use the pumping lemma to prove a language is context-free– Showing L satisfies the pumping condition does

not guarantee that L is context-free

CFL

Pumping

Page 167: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

167

Pumping Lemma

What does it mean?

Page 168: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

168

Pumping Condition

• A language L satisfies the CFL pumping condition if: – there exists an integer n > 0 such that

– for all strings x in L of length at least n

– there exist strings u, v, w, y, z such that• x = uvwyz and

• |vwy| <= n and

• |vy| >= 1 and

• For all k >= 0, uvkwykz is in L

Page 169: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

169

v and y can be pumped

• Let x = abcdefg be in L• Then there exist 2 substrings v and y in x such that v and y can

be repeated (pumped) in place any number of times and the resulting string is still in L– uvkwykz is in L for all k >= 0

• For example– v = cd and y = f

• uv0wy0z = uwz = abeg is in L• uv1wy1z = uvwyz = abcdefg is in L• uv2wy2z = uvvwyyz = abcdcdeffg is in L• uv3wy3z = uvvvwyyyz = abcdcdcdefffg is in L • …

1) x in L2) x = uvwyz3) For all k >= 0, uvkwykz is in L

Page 170: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

170

What the other parts mean• A language L satisfies the CFL pumping condition if:

– there exists an integer n > 0 such that• Since we skip this proof, we will not see what n really means

– for all strings x in L of length at least n• x must be in L and have sufficient length

– there exist strings u, v, w, y, z such that• x = uvwyz and

• |vwy| <= n and– v and y are contained within n characters of x– Note: these are NOT necessarily the first n characters of x

• |vy| >= 1 and– v and y cannot both be – One of them might be , but not both

• For all k >= 0, uvkwykz is in L

Page 171: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

171

Example

• Let L be the set of palindromes over {a,b}– Let x = aabaa

– Let n = 3

– What are the possibilities for v and y ignoring the pumping constraint?

– Which ones satisfy the pumping lemma?

Page 172: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

172

Pumping Lemma

Applying it to prove a specific language L is not context-free

Page 173: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

173

How we use the Pumping Lemma

• We choose a specific language L– For example, {ajbjcj | j > 0}

• We show that L does not satisfy the pumping condition

• We conclude that L is not context-free

Page 174: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

174

Showing L “does not pump”

• A language L satisfies the CFL

pumping condition if: – there exists an integer n > 0

such that

– for all strings x in L of length at least n

– there exist strings u, v, w, y, z such that

• x = uvwyz and

• |vwy| <= n and

• |vy| >= 1 and

• For all k >= 0, uvkwykz is in L

• A language L does not satisfy

the CFL pumping condition if: – for all integers n of sufficient

size

– there exists a string x in L of length at least n such that

– for all strings u, v, w, y, z such that

• x = uvwyz and

• |vwy| <= n and

• |vy| >= 1

– There exists a k >= 0 such that uvkwykz is not in L

Page 175: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

175

Example Proof

• A language L does not satisfy

the CFL pumping condition if: – for all integers n of sufficient

size

– there exists a string x in L of length at least n such that

– for all strings u, v, w, y, z such that

• x = uvwyz and

• |vwy| <= n and

• |vy| >= 1

– There exists a k >= 0 such that uvkwykz is not in L

• Proof that L = {aibici | i>0} does not satisfy the CFL pumping condition

• Let n be the integer from the pumping lemma

• Choose x = anbncn

• Consider all strings u, v, w, y, z s.t.• x = uvwyz and

• |vwy| <= n and

• |vy| >= 1

• Argue that uvkwykz is not in L for some k >= 0

– Argument must apply to all possible u,v,w,y,z

– Continued on next slide

Page 176: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

176

Example Proof Continued• Proof that L = {aibici | i>0} does not

satisfy the CFL pumping condition

• Let n be the integer from the pumping lemma

• Choose x = anbncn

• Consider all strings u, v, w, y, z s.t.• x = uvwyz and

• |vwy| <= n and

• |vy| >= 1

• Argue that uvkwykz is not in L for some k >= 0– Argument must apply to all possible

u,v,w,y,z

– Continued next column

• Identify possible cases for vwy• What is impossible for vwy?

• Case 1– vwy contains no a’s

• Case 2– vwy contains no c’s

• Must argue uvkwykz is not in L for both cases described above– Can use different values of k– Continued on next slide

Page 177: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

177

Example Proof Continued

• Identify possible cases for vwy• What is impossible for vwy?

• Case 1– vwy contains no a’s

• Case 2– vwy contains no c’s

• Must argue uvkwykz is not in L for both cases described above– Can use different values of k– Continued next column

• Case 1: vwy contains no a’s– vy contains at least 1 b or c

• follows from – vwy contains no a’s and– |vy| >= 1

– uwz is not in L• uwz has n a’s

– follows from fact vwy contains no a’s and x originally had n a’s

• uwz has fewer than n b’s or fewer than n c’s

– follows from vy contains at least 1 b or c and x originally only had n b’s and n c’s

• Continued next slide

Page 178: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

178

Example Proof Continued

• Case 1: vwy contains no a’s– vy contains at least 1 b or c

• follows from – vwy contains no a’s and– |vy| >= 1

– uwz is not in L• uwz has n a’s

– follows from fact vwy contains no a’s and x originally had n a’s

• uwz has fewer than n b’s or fewer than n c’s

– follows from vy contains at least 1 b or c and x originally only had n b’s and n c’s

• Continued next column

• Case 2: vwy contains no c’s– vy contains at least

– uv2wy2z is not in L• uv2wy2z has n c’s

– follows from fact vwy contains no c’s and x originally had n c’s

• uv2wy2z has more than n a’s or more than n b’s

– follows from vy contains at least 1 a or b and x originally has n a’s and n b’s

• Continued next slide

Page 179: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

179

Example Proof Completed

• For all possible u, v, w, y, z, we have shown there exists a k>=0 such that– uvkwykz is not in L

• Note, we used a different value of k for each case (though we didn’t have to)

• Therefore L does not satisfy the CFL pumping condition

• There L is not a CFL

• Case 2: vwy contains no c’s– vy contains at least

– uv2wy2z is not in L• uv2wy2z has n c’s

– follows from fact vwy contains no c’s and x originally had n c’s

• uv2wy2z has more than n a’s or more than n b’s

– follows from vy contains at least 1 a or b and x originally has n a’s and n b’s

• Continued next column

Page 180: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

180

Other example languages

• TWOCOPIES = {ww | w is in {a,b}* }– abbabb is in TWOCOPIES but abaabb is not

• EQUAL3 = the set of strings over {a, b, c} such that the number of a’s equals the number of b’s equals the number of c’s

• {aibjck | i < j < k}

Page 181: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

181

Pumping Lemma

Two rules of thumb

Page 182: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

182

Two Rules of Thumb

• Try to use blocks of at least n characters in x– For TWOCOPIES, choose x = anbnanbn rather than

anbanb• Guarantees v and y cannot be in more than 2 blocks of x

• Try k=0 or k=2– k=0

• This reduces number of occurrences of v and y

– k=2• This increases number of occurrences of v and y

Page 183: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

183

Summary

• We use the Pumping Lemma to prove a language is not a CFL– Note, does not work for all non CFL languages– Can be strengthened to Ogden’s Lemma

• In book

• Choosing a good string x is first key step

• Choosing a good k is second key step

• Typically have several cases for v, w, y

Page 184: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

184

Module 37

• Showing CFL’s not closed under set intersection and set complement

Page 185: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

185

Nonclosure Properties for CFL’s

Page 186: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

186

CFL’s not closed under set intersection

• How can we prove that CFL’s are not closed under set intersection?

Page 187: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

187

Counterexample

• What is a possible L1 intersect L2?

– What non-CFL languages do we know?

• What could L1 and L2 be?

– L1 =

– L2 =

– How can we prove that L1 and L2 are context-free?

Page 188: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

188

CFL’s not closed under complement

• How can we prove that CFL’s are not closed under complement?

– Another way• Use fact that any language class which is closed

under union and complement must also be closed under intersection

Page 189: 1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition

189

Language class hierarchy

All languages over alphabet

RE

REG

HH

EqualCFLREC

Equal-3