BU CS 332 Theory of Computation Lecture 6: Reading: NFAs > - - PowerPoint PPT Presentation

bu cs 332 theory of computation
SMART_READER_LITE
LIVE PREVIEW

BU CS 332 Theory of Computation Lecture 6: Reading: NFAs > - - PowerPoint PPT Presentation

BU CS 332 Theory of Computation Lecture 6: Reading: NFAs > Regular expressions Sipser Ch 1.3, Context free grammars 2.1, 2.3 Pumping lemma for CFLs Mark Bun February 10, 2020 Regular Expressions Syntax A regular


slide-1
SLIDE 1

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 Bun February 10, 2020

slide-2
SLIDE 2

Regular Expressions – Syntax

A 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

slide-3
SLIDE 3

Regular Expressions – Semantics

the language a regular expression describes 1. 2. 3. for every

  • 6.

Example:

∗ ∗

  • 2/10/2020

CS332 ‐ Theory of Computation 3

slide-4
SLIDE 4

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

slide-5
SLIDE 5

NFA ‐> Regular expression

Theorem 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

1 01*0

slide-6
SLIDE 6

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

𝑡 𝑏

slide-7
SLIDE 7

Generalized NFA Example

2/10/2020 CS332 ‐ Theory of Computation 7

𝑡 𝑏

𝑡 𝑏

slide-8
SLIDE 8

NFA ‐> Regular expression

2/10/2020 CS332 ‐ Theory of Computation 8

NFA GNFA GNFA GNFA Regex 𝑙 states 𝑙 2 states 𝑙 1 states 2 states …

slide-9
SLIDE 9

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.
slide-10
SLIDE 10

GNFA ‐> Regular expression

Idea: While the machine has more than 2 states, rip one

  • ut and relabel the arrows with regexes to account for the

missing state

2/10/2020 CS332 ‐ Theory of Computation 10

1 3 2 1 3

slide-11
SLIDE 11

GNFA ‐> Regular expression

Idea: While the machine has more than 2 states, rip one

  • ut and relabel the arrows with regexes to account for the

missing state

2/10/2020 CS332 ‐ Theory of Computation 11

1 3 2 1 3

slide-12
SLIDE 12

GNFA ‐> Regular expression

Idea: While the machine has more than 2 states, rip one

  • ut and relabel the arrows with regexes to account for the

missing state

2/10/2020 CS332 ‐ Theory of Computation 12

1 3 2 1 3

slide-13
SLIDE 13

GNFA ‐> Regular expression

Idea: While the machine has more than 2 states, rip one

  • ut and relabel the arrows with regexes to account for the

missing state

2/10/2020 CS332 ‐ Theory of Computation 13

1 3 2 1 2 3 4 1 3

slide-14
SLIDE 14

Example

2/10/2020 CS332 ‐ Theory of Computation 14

slide-15
SLIDE 15

2/10/2020 CS332 ‐ Theory of Computation 15

slide-16
SLIDE 16

2/10/2020 CS332 ‐ Theory of Computation 16

slide-17
SLIDE 17

Context‐Free Grammars

2/10/2020 CS332 ‐ Theory of Computation 17

slide-18
SLIDE 18

Some History

An abstract model for two distinct problems Rules for parsing natural languages

2/10/2020 CS332 ‐ Theory of Computation 18

slide-19
SLIDE 19

Some History

An 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.

slide-20
SLIDE 20

Context‐Free Grammar (Informal)

Example Grammar Derivation

2/10/2020 CS332 ‐ Theory of Computation 20

slide-21
SLIDE 21

Context‐Free Grammar (Informal)

Example Grammar 𝐻 𝐹 → 𝐹 𝑈 𝐹 → 𝑈 𝑈 → 𝑈 𝐺 𝑈 → 𝐺 𝐺 → 𝐹 𝐺 → 𝑏 𝐺 → 𝑐 Derivation 𝑀𝐻

2/10/2020 CS332 ‐ Theory of Computation 21

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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

  • r there exists a

sequence such that

  • Language of the grammar:

∗ ⇒ ∗

Example: where

  • 2/10/2020

CS332 ‐ Theory of Computation 25

slide-26
SLIDE 26

CFG Examples

Give 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

slide-27
SLIDE 27

Pumping Lemma II: Pump Harder

2/10/2020 CS332 ‐ Theory of Computation 27

slide-28
SLIDE 28

Non context‐free languages?

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

2/10/2020 CS332 ‐ Theory of Computation 28

slide-29
SLIDE 29

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:

slide-30
SLIDE 30

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:

slide-31
SLIDE 31

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:

slide-32
SLIDE 32

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

slide-33
SLIDE 33

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 pumped If = with | | , then…