bu cs 332 of … · • context‐free grammars • pumping lemma for cfls reading: sipser ch 1.3,...

33
BU CS 332 – Theory of Computation Lecture 6: NFAs > Regular expressions Contextfree grammars Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020

Upload: others

Post on 31-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

BU CS 332 – Theory of Computation

Lecture 6:• NFAs ‐> Regular expressions• Context‐free grammars• Pumping lemma for CFLs

Reading:Sipser Ch 1.3, 2.1, 2.3

Mark BunFebruary 10, 2020

Page 2: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Regular Expressions – SyntaxA regular expression  is defined recursively using the following rules:

1. ,  , and  are regular expressions for every 

2. If  and  are regular expressions, then so are,  , and ∗

Examples: (over  )∗ ∗ ∗ ∗

2/10/2020 CS332 ‐ Theory of Computation 2

Page 3: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Regular Expressions – Semantics the language a regular expression describes

1.2.3. for every 

6. ∗ ∗

Example: ∗ ∗

2/10/2020 CS332 ‐ Theory of Computation 3

Page 4: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Regular Expressions Describe Regular Languages

Theorem: A language  is regular if and only if it is described by a regular expression

Theorem 1: Every regular expression has an equivalent NFA

Theorem 2: Every NFA has an equivalent regular expression

2/10/2020 CS332 ‐ Theory of Computation 4

Page 5: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

NFA ‐> Regular expressionTheorem 2: Every NFA has an equivalent regex

Proof idea: Simplify NFA by “ripping out” states one at a time and replacing with regexes

2/10/2020 CS332 ‐ Theory of Computation 5

0

1

001*0

Page 6: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Generalized NFAs• Every transition is labeled by a regex• One start state with only outgoing transitions• Only one accept state with only incoming transitions• Start state and accept state are distinct

2/10/2020 CS332 ‐ Theory of Computation 6

𝑠 𝑎

Page 7: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Generalized NFA Example

2/10/2020 CS332 ‐ Theory of Computation 7

𝑠

𝑎

𝑠 𝑎

Page 8: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

NFA ‐> Regular expression

2/10/2020 CS332 ‐ Theory of Computation 8

NFA GNFA

GNFA

GNFA

Regex

𝑘 states

𝑘 2 states

𝑘 1 states

2 states

Page 9: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

NFA ‐> GNFA

2/10/2020 CS332 ‐ Theory of Computation 9

NFAεε

ε

ε

• Add a new start state with no incoming arrows.• Make a unique accept state with no outgoing arrows.

Page 10: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

GNFA ‐> Regular expressionIdea: While the machine has more than 2 states, rip one out and relabel the arrows with regexes to account for the missing state

2/10/2020 CS332 ‐ Theory of Computation 10

1 32

1 3

Page 11: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

GNFA ‐> Regular expressionIdea: While the machine has more than 2 states, rip one out and relabel the arrows with regexes to account for the missing state

2/10/2020 CS332 ‐ Theory of Computation 11

1 32

1 3

Page 12: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

GNFA ‐> Regular expressionIdea: While the machine has more than 2 states, rip one out and relabel the arrows with regexes to account for the missing state

2/10/2020 CS332 ‐ Theory of Computation 12

1 32

1 3

Page 13: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

GNFA ‐> Regular expressionIdea: While the machine has more than 2 states, rip one out and relabel the arrows with regexes to account for the missing state

2/10/2020 CS332 ‐ Theory of Computation 13

1 321

2

3

4

1 3

Page 14: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Example

2/10/2020 CS332 ‐ Theory of Computation 14

Page 15: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

2/10/2020 CS332 ‐ Theory of Computation 15

Page 16: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

2/10/2020 CS332 ‐ Theory of Computation 16

Page 17: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Context‐Free Grammars

2/10/2020 CS332 ‐ Theory of Computation 17

Page 18: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Some HistoryAn abstract model for two distinct problems

Rules for parsing natural languages

2/10/2020 CS332 ‐ Theory of Computation 18

Page 19: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Some HistoryAn abstract model for two distinct problems

Specification of syntax and compilation for programming languages

2/10/2020 CS332 ‐ Theory of Computation 19

1977 ACM Turing Award citation(John Backus)

For profound, influential, and lasting contributions to the design of practical high‐level programming systems, notably through 

his work on FORTRAN, and for seminal publication of formal procedures for the specification of programming languages.

Page 20: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Context‐Free Grammar (Informal)Example Grammar 

Derivation

2/10/2020 CS332 ‐ Theory of Computation 20

Page 21: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Context‐Free Grammar (Informal)Example Grammar 𝐺

𝐸 → 𝐸 𝑇𝐸 → 𝑇𝑇 → 𝑇 𝐹𝑇 → 𝐹𝐹 → 𝐸𝐹 → 𝑎𝐹 → 𝑏

Derivation

𝐿 𝐺

2/10/2020 CS332 ‐ Theory of Computation 21

Page 22: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Socially Awkward Professor Grammar

2/10/2020 CS332 ‐ Theory of Computation 22

<PHRASE> → <START><END>

<PHRASE> → <FILLER><PHRASE>

<FILLER> → LIKE

<FILLER> → UMM

<START> → YOU KNOW

<END> → WHOOPS

<START> → ε

<END> → SORRY

<END> → $#@!

Page 23: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Socially Awkward Professor Grammar

2/10/2020 CS332 ‐ Theory of Computation 23

<PHRASE> → <FILLER><PHRASE> | <START><END>

<FILLER> → LIKE | UMM

<START> → YOU KNOW | ε

<END> → WHOOPS | SORRY | $#@!

Page 24: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Context‐Free Grammar (Formal)A CFG is a 4‐tuple • is a finite set of variables• is a finite set of terminal symbols (disjoint from  )• is a finite set of production rules of the form  , where  and    ∗

• is the start symbol

Example:  where 

2/10/2020 CS332 ‐ Theory of Computation 24

Page 25: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Context‐Free Grammar (Formal)A CFG is a 4‐tuple = variables       = terminals         = rules          = start

• We say  (“ yields ”) if  is a rule of the grammar• We say  ⇒

∗ (“ derives  ”) if  or there exists a sequence such that • Language of the grammar:  ∗

⇒∗

Example:  where 

2/10/2020 CS332 ‐ Theory of Computation 25

Page 26: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

CFG ExamplesGive context‐free grammars for the following languages

1. The empty language

2. Strings of properly nested parentheses

3. Strings with equal # of  ’s and  ’s

2/10/2020 CS332 ‐ Theory of Computation 26

Page 27: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Pumping Lemma II: Pump Harder

2/10/2020 CS332 ‐ Theory of Computation 27

Page 28: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Non context‐free languages?

• Could it be the case that every language is context‐free?

2/10/2020 CS332 ‐ Theory of Computation 28

Page 29: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Pumping Lemma for regular languages

2/10/2020 CS332 ‐ Theory of Computation 29

Let  be a regular language.

Then there exists a “pumping length”  such that 

1.  2.  3.   𝑖 for all 

For every   where  ,can be split into three parts  where:

Page 30: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Pumping Lemma for context‐free languages

2/10/2020 CS332 ‐ Theory of Computation 30

Let  be a context‐free language.

Then there exists a “pumping length”  such that 

1.  2.  3.   𝑖 𝑖 for all 

For every   where  ,can be split into five parts  where:

Example: ∗

Page 31: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Pumping Lemma for context‐free languages

2/10/2020 CS332 ‐ Theory of Computation 31

Let  be a context‐free language.

Then there exists a “pumping length”  such that 

1.  2.  3.   𝑖 𝑖 for all 

For every   where  ,can be split into five parts  where:

Example: ∗

Page 32: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Pumping Lemma as a game

2/10/2020 CS332 ‐ Theory of Computation 32

1. YOU pick the language 𝐿 to be proved non context‐free.2. ADVERSARY picks a possible pumping length 𝑝.3. YOU pick 𝑤 of length at least 𝑝.4. ADVERSARY divides 𝑤 into 𝑢, 𝑣, 𝑥,𝑦, 𝑧, obeying rules of the 

Pumping Lemma:        |𝑣𝑦| 0 and      |𝑣𝑥𝑦| 𝑝. 5. YOU win by finding 𝑖 0, for which 𝑢𝑣𝑖𝑥𝑦𝑖𝑧 is not in 𝐿.

If regardless of how the ADVERSARY plays this game, you can always win, then  is non context‐free

Page 33: BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3, 2.1, 2.3 Mark Bun February 10, 2020. Regular Expressions –Syntax A regular expression

Pumping Lemma example

2/10/2020 CS332 ‐ Theory of Computation 33

Claim: is not regular

Proof: Assume  is regular with pumping length 1. Find  with 2. Show that  cannot be pumpedIf  =  with      | |  , then…