BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3,...

Post on 31-Oct-2020

5 views 0 download

Transcript of BU CS 332 of … · • Context‐free grammars • Pumping lemma for CFLs Reading: Sipser Ch 1.3,...

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

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

Regular Expressions – Semantics the language a regular expression describes

1.2.3. for every 

6. ∗ ∗

Example: ∗ ∗

2/10/2020 CS332 ‐ Theory of Computation 3

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

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

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

𝑠 𝑎

Generalized NFA Example

2/10/2020 CS332 ‐ Theory of Computation 7

𝑠

𝑎

𝑠 𝑎

NFA ‐> Regular expression

2/10/2020 CS332 ‐ Theory of Computation 8

NFA GNFA

GNFA

GNFA

Regex

𝑘 states

𝑘 2 states

𝑘 1 states

2 states

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.

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

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

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

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

Example

2/10/2020 CS332 ‐ Theory of Computation 14

2/10/2020 CS332 ‐ Theory of Computation 15

2/10/2020 CS332 ‐ Theory of Computation 16

Context‐Free Grammars

2/10/2020 CS332 ‐ Theory of Computation 17

Some HistoryAn abstract model for two distinct problems

Rules for parsing natural languages

2/10/2020 CS332 ‐ Theory of Computation 18

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.

Context‐Free Grammar (Informal)Example Grammar 

Derivation

2/10/2020 CS332 ‐ Theory of Computation 20

Context‐Free Grammar (Informal)Example Grammar 𝐺

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

Derivation

𝐿 𝐺

2/10/2020 CS332 ‐ Theory of Computation 21

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> → $#@!

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 | $#@!

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

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

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

Pumping Lemma II: Pump Harder

2/10/2020 CS332 ‐ Theory of Computation 27

Non context‐free languages?

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

2/10/2020 CS332 ‐ Theory of Computation 28

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:

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: ∗

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: ∗

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

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…